Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by EMOTION-THEORY · Feb 18, 2014 at 10:01 PM · c#editorinspector

Edit child in parent list [Custom Inspector]

As question suggests,

I have

 [Serializable]
 public class Parent
 {
    public float myFloat;
 }
 
 [Serializable]
 public class Child : Parent
 {
    public string childString;
 }
 
 public class MonoClass : MonoBehavior
 {
    public List<Parent> myList;
 }

I have MonoClass attached to a gameobject. I can edit the list of Parent but if there's a Child in that list, I can't edit the Child (I can't assign values to childString).

If I have a list of Parent objects, how can I basically get every object in the list to show a default editor for each class subtype?

Now... I KNOW this requires a custom editor. I've done my research. However, nowhere have I seen HOW this can be accomplished.

As a stretch goal, I'd like to be able to pick a subtype from a dropdown list to add to the list. That way I can add all eligible types to the list (and then of course, edit them).

Cheers!

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 Loius · Feb 18, 2014 at 10:33 PM

The first issue is that Unity does not keep track of subtypes. You have a list of Parents, Unity will serialize that as a bunch of parents and next time you load the program, all the child data will be lost.

To demonstrate lots of subclasses, let's say we have Letter as the parent class, and A, B, C, D, E... Z all extend Letter. You need a SerializableLetter so that Unity keeps track of things properly. It's extremely ugly!

 [Serializable] public class Letter { }
 [Serializable] public class A : Letter { }
 ...
 [Serializable] public class SerializableLetter {
   // Unity can serialize A, B, C, etc, so let's have one of each!
   public A a=null; public B b=null; public C c=null; public D d=null;
   ...

   // We're almost never going to use a SeriLetter as anything but a Letter, so let's make it easy:
   public static implicit operator Letter(SerializableLetter sl) { 
   if ( null != a ) return a;
   if ( null != b ) return b;
   ...
   if ( null != z ) return z;
   }
 
 }

Your MonoBehaviours can then hold lists of SerializableLetters when they need those lists to be saved in prefabs. You can still have lists of Letters; Unity just won't be able to serialize those into prefabs.

You can't let Unity auto-edit your SerializableLetter either, of course. What I did is I created an editor inside the SerializableLetter class itself:

 public class SerializableLetter {
  ...

  // This is unnecessary, but it ended up being handy to cache this for me.
  [SerializeField] string typeName = "";
  
 
 #if UNITY_EDITOR
   public static void Editor(ref SerializableLetter sl) {
     // The Letter editor is capable of creating the subclass objects we need to store:
     Letter temp = (Letter)sl;
     Letter.Editor(ref temp);
     if ( null == temp ) return;

     // Determine what type it is, store it in the appropriate variable, and null out whatever was not null so that we have only one active subtype:
     typeName = temp.GetType().Name;
     if ( typeName == "A" ) a = temp as A; else a = null;
     ...
     if ( typeName == "Z" ) z = temp as Z; else z = null;
   }
 #endif
 }
 
 public class Letter {
   public static string[] subtypes = new string[]{
     "A","B","C",...,"Z"};
 
 #if UNITY_EDITOR
   public static void Editor(ref Letter l) {
     System.Type old=null, current=null;
     string name = "";
     if ( null != l ) { old = l.GetType(); current = l.GetType(); name = old.Name; }

     // Show a popup list of all available types (I had some nicety functions to create a list of human-readable names to display instead of subtypes; the index is the only important value for the code):
     int curIndex = System.Array.IndexOf(subtypes, name);
     curIndex = EditorGUILayout.Popup(curIndex, subtypes);
     if ( curIndex > -1 ) current = System.Type.GetType(subtypes[curIndex]);
     if ( current != old ) {
     // The type was changed - we can't *always* do this, because it would reset all values constantly. It is also guaranteed that current is never null here, because you can't pick an invalid value in EdGUIL.Popup
       l = (Letter)System.Activator.CreateInstance(current);
     }

     // The various subclasses can override AdditionalEdit to create their own editors.
     if (null != l) l.AdditionalEdit();
   }
   protected virtual void AdditionalEdit() { }
 #endif
 }

This could all be cleaned up a little bit with some better use of lists, but I haven't taken the time to do that yet. The real killer is the massive list of variables in SerializableLetter; I have no idea how to trim that down and still have Unity respect types.

Comment
Add comment · 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 Loius · Feb 27, 2014 at 03:54 AM 0
Share

Hey! By the by, do not do this if your classes are nested recursively - that is, if you have class A that contains a list of Letters. Unity will set aside way too much memory for the object and you'll end up with a hard-to-find bug.

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

19 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

Related Questions

Custom Inspector: Targets & GameObjects 1 Answer

Some public attributes not shown in inspector, default references 3 Answers

Dynamic changing type in ReorderableList 0 Answers

Custom inspector field resetting when compile or play 1 Answer

Update variables with C# properties in customeditor 2 Answers


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