Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by StickFigs · Sep 02, 2015 at 01:54 AM · inspectorreflectioninheritance

Building a Collection of Subclasses and Modifying Their Public Members from the Inspector

I'm trying to figure out a way to have a component script that I can attach to an object in the scene that holds a collection of references to objects of type BaseClass but would allow me to view and edit the public members of subclasses of BaseClass in the same collection.

For example, I have a component script called CommandList that I attach to an object in the scene. It has a member public CommandHolder[]; where CommandHolder looks like this:

 [Serializable]
 public class CommandHolder
 {
     public BaseCommand command;
 }

Where BaseCommand is just some base class that we'll say has no public members.

Now in the Unity Inspector if we look at an object with a CommandList component we'll be able to define an array size for our array of CommandHolder elements. Then I can drag and drop scripts from my Assets into the public BaseCommand command field of the CommandHolder object in the inspector.

So this is where my question comes in. If I have a class called WaitCommand:

 public class WaitCommand : BaseCommand
 {
     public int seconds;
 }

And another called MoveCommand:

 public class MoveCommand : BaseCommand
 {
     public Vector3 direction;
     public float speed;
     public float duration;
 }

Then what I want is if I drag and drop a WaitCommand script into the command field of a CommandHolder element in my CommandHolder array then I want to be able to inspect the seconds public field in the inspector. I understand reason this doesn't happen is because CommandHolder expects a BaseCommand object so its only treating our WaitCommand as its base class and not exposing the public int seconds member that only exists in the subclass. So then how can I get the behavior I want?

I imagine this would involve some reflection, because I don't want the solution to involve a custom inspector class for each possible subclass of BaseCommand, I want it to just work.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by StickFigs · Sep 05, 2015 at 07:31 PM

Well I came to an awkward, easily breakable solution for what I'm trying to do. It involves just giving my base class a variety of array structures holding basic data types my subclasses will probably need then having a string[] for each of these arrays which act as labels for the data object in the same index in the actual data type array. The base class has empty arrays for the string[] label arrays and each subclass overrides the value of the string[] related to the data type array it will need a parameter for. Then I just wrote a custom PropertyDrawer for my BaseAICommand class which shows only property fields for indexes in the real data arrays which have an accompanying label in the string[] that goes with that data type array.

That's an awkward explanation but I think the code will clarify what exactly I'm doing:

AICommandHandler.cs

 using UnityEngine;
 using System.Collections;
 
 // This class attaches to a gameobject in the editor and just holds a list of BaseAICommand
 // objects.
 // Since I can't drag and drop subclasses into the array and it automatically populates
 // Element slots with new BaseAICommand objects then I would eventually have to hide the
 // default array controls and add a custom control for instanstiating subclass types 
 // into the array.
 public class AICommandHandler : MonoBehaviour
 {
     public BaseAICommand[] commands;
 }

BaseAICommand.cs

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class BaseAICommand
 {
     public float[] floats = new float[0];
     public string[] strings = new string[0];
     public Vector3[] vector3s = new Vector3[0];
     public int[] ints = new int[0];
 
     public abstract string[] floatsNames = new string[0];
     public string[] stringsNames = new string[0];
     public string[] vector3sNames = new string[0];
     public string[] intsNames = new string[0];
 }

MoveAICommand.cs

 using UnityEngine;
 using System.Collections;
 
 // An example of what a subclass would look like.
 public class MoveAICommand : BaseAICommand
 {
     private float duration;
 
     public MoveAICommand()
     {
         this.vector3sNames = new string[] { "Direction" };
         this.floatsNames = new string[] { "Speed", "Duration" };
     }
 }

BaseAICommandEditor.cs

 using UnityEditor;
 using UnityEngine;
 
 [CustomPropertyDrawer(typeof(BaseAICommand))]
 public class BaseAICommandEditor : PropertyDrawer
 {
     private int rows = 0;
 
     private int NamesToGUI(SerializedProperty actualArrayProp, SerializedProperty namesArrayProp, Rect position)
     {
         int rowsAdded = 0;
 
         var namesArrayPropLength = namesArrayProp.arraySize;
 
         for (int i = 0; i < namesArrayPropLength; i++)
         {
             var propName = namesArrayProp.GetArrayElementAtIndex(i);
             var propNameString = propName.stringValue;
 
             // Stop enumerating if we hit a blank name
             if (string.IsNullOrEmpty(propNameString))
                 break;
 
             actualArrayProp.InsertArrayElementAtIndex(i);
             EditorGUI.PropertyField(position, actualArrayProp.GetArrayElementAtIndex(i), new GUIContent(propNameString));
             rowsAdded++;
         }
 
         return rowsAdded;
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         rows = 0;
         position.height = 16f;
 
         EditorGUI.BeginProperty(position, label, property);
 
         var floatsProp = property.FindPropertyRelative("floats");
         var stringsProp = property.FindPropertyRelative("strings");
         var vector3sProp = property.FindPropertyRelative("vector3s");
         var intsProp = property.FindPropertyRelative("ints");
 
         var floatsNamesProp = property.FindPropertyRelative("floatsNames");
         var stringsNamesProp = property.FindPropertyRelative("stringsNames");
         var vector3sNamesProp = property.FindPropertyRelative("vector3sNames");
         var intsNamesProp = property.FindPropertyRelative("intsNames");
 
         rows += NamesToGUI(floatsProp, floatsNamesProp, position);
         position.y += 18f;
         rows += NamesToGUI(stringsProp, stringsNamesProp, position);
         position.y += 18f;
         rows += NamesToGUI(vector3sProp, vector3sNamesProp, position);
         position.y += 18f;
         rows += NamesToGUI(intsProp, intsNamesProp, position);        
 
         EditorGUI.EndProperty();
     }
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return 16f + 18f * rows;
     }
 }

This actually seems to work but it has some fatal flaws that prevent it from being a good solution to my problem:

  • AICommandHandler would need a custom Editor inspector view written to handle adding and removing BaseAICommand-derived objects to and from the array.

  • Overriding the string[] values in the constructor method of the subclasses seems like the only place I can set these values. The problem here is that if I ever need to modify these values on a subclass then I would have to go through my entire game and delete and recreate any instances to have them be created with the new defaults. THIS is a showstopper.

  • This solution requires two arrays for each data type class that I might want to be used by a subclass. This could get out of hand very easily.

Thoughts?

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Editor List with child classes 1 Answer

Show variable of class that dont inherit from MonoBehaviour in Inspector 1 Answer

Popup to choose which child class I want in Custom Editor Inspector 0 Answers

Custom Editor Inspector member class with inheritance, values resets after starting Play Mode 1 Answer

Can I have a variable accessed through the inspector and be accessed by a child script without making it public? 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