• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by janzdott · Apr 09, 2013 at 04:02 PM · inspectorclass

Inspector variable to hold a type of inherited class

Hey everyone. Im working on a minecraft style engine for Unity. I already wrote one, but im rewriting it to be faster. I have a class called TerrainGenerator, which is obviously for generating terrain. But i want the user to be able to write their own TerrainGenerator. They can make a class that inherits from TerrainGenerator, and use that class for generating the terrain. I'm trying to make this all very simple for the user. So I want to be able to set this in the inspector of my World class.

Heres an example of my base class

 public class TerrainGenerator {
     public static TerrainGenerator instance;
 
     public virtual void GenerateTerrain() {}
 }

And a derived class made by the user

 public class FlatTerrainGenerator : TerrainGenerator {
     public override void GenerateTerrain () {
         //Generates flat terrain
     }
 }

And here's what I want to do

 public class World {
     //somehow set TerrainGenerator to FlatTerrainGenerator in inspector
 
     void Awake () {
         //if desired TerrainGenerator is set to FlatTerrainGenerator
         TerrainGenerator.instance = new FlatTerrainGenerator ();
     }
 
     void Start () {
          //generate terrain using desired terrain generator
          TerrainGenerator.instance.GenerateTerrain ();
     }
 }


I can easily make TerrainGenerator inherit from Monobehaviour, then stick an instance of FlatTerrainGenerator on a GameObject. Then drag and drop that component into a field in the World inspector. But that's an ugly solution. I'm looking for a clean and simple way for the user to change the instance of terrainGenerator. Its easy to do in code, but I would like it to be assigned in the editor because of simplicity and the fact that the TerrainGenerator should not be changed during runtime, it only needs to be serialized as a variable in the World component.

Comment

People who like this

0 Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image janzdott · Apr 09, 2013 at 04:05 PM 0
Share

I was thinking maybe I could have the Inspector variable a Type. Then the user could set the type to FlatTerrainGenerator. But I don't know if this would work. I can't test that out until tonight because I'm at work

avatar image Dracorat · Apr 09, 2013 at 04:06 PM 0
Share

You didn't ask a question in your question, but I suspect you want to know how to create a valid plug-in architecture.

If that's the case, here's a great place to start - it's c# in general, not Unity in specific, but it still applies.

http://www.codeproject.com/Articles/6334/Plug-ins-in-C

avatar image janzdott · Apr 09, 2013 at 04:15 PM 0
Share

Haha sorry I thought my question was implied when I explained what I'm trying to do. I'll read up on that plugin tutorial when I get a chance and see if I learn something useful!

avatar image Dracorat · Apr 09, 2013 at 04:21 PM 0
Share

I just realized I had linked a VB .Net article - sorry about that - I just changed the link to a C# article.

avatar image janzdott · Apr 09, 2013 at 05:17 PM 0
Share

Well there's some useful information there, but it doesn't help me solve my problem. I've edited my question to better explain what I'm trying to do

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by janzdott · Apr 10, 2013 at 03:37 AM

Finally figured it out. Its not exactly how I wanted it, but its still easy for the user. I wrote a small custom inspector which uses reflection to set the TerrainGenerator. You must only type the name of the TerrainGenerator inherited class that you would like to use. Then the custom inspector will check if that class exists, and if it inherits from TerrainGenerator. If it does, it creates an instance of that class and adds it to the World. If the class does not exist, or does not inherit from TerrainGenerator, the string becomes empty, and the TerrainGenerator defaults to itself.

 public override void OnInspectorGUI() {
     World world = (World)target;
     DrawDefaultInspector();
     if (GUI.changed) {
         System.Reflection.Assembly assembly = typeof(TerrainGenerator).Assembly;
         System.Type type = assembly.GetType(world.terrainGeneratorName);
         if (type != null && typeof(TerrainGenerator).IsAssignableFrom(type)) {
             world.terrainGenerator = (TerrainGenerator)System.Activator.CreateInstance(type);
         }
         else {
             world.terrainGeneratorName = string.Empty;
             world.terrainGenerator = new TerrainGenerator();
         }
     }
 }
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by Dracorat · Apr 09, 2013 at 05:37 PM

In the class where you have "World" - I'm assuming it's the script that's attached to the object?

If so, make a public member of the base class type:

 public TerrainGenerator ThisGenerator;

This should show up in the inspector. You'd drag the script for FlatTerrainGenerator to that spot on the inspector.

You don't have to know the specific implementation since it's a derived class.

 void Start () {
      //generate terrain using desired terrain generator
      this.ThisGenerator.GenerateTerrain();
 }
Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image janzdott · Apr 09, 2013 at 08:30 PM 0
Share

That only works if TerrainGenerator inherits from Monobehaviour and exists as a component on a GameObject. I mentioned that i already know this is possible, and i would prefer not to do this because its an ugly solution

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Parent Class variables in Child Class's Inspector (C#) 0 Answers

if texture used from class it does not show in game, advice needed? 1 Answer

View script variables within an array of scripts in Inspector 1 Answer

[SOLVED]How to use attributes from array of custom class instances? 1 Answer

C# | public variables in parent class should not show up in child class 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges