• 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
1
Question by Warcrea · Mar 21, 2017 at 01:59 AM · editorlisteditor-scriptinginheritancepolymorphism

CustomEditor for a list of polymorphic items

Hi all, been struggling with this for a while so I hope someone can help.

To schedule cutscenes in my game I have a scriptableobject named Scene:

 public class Scene: ScriptableObject
 {
     public List<ActingDirection> directions = new List<ActingDirection>();
 }

Scene has a list of ActingDirections:

 public class ActingDirection {
     public int actor;   //Who is performing the action
     public float timeStart;   //The time after the previous action this one should play
 }

And there are two classes that extend ActingDirection: ActingDialogue and ActingAnimation, which respectively contain a string of dialogue, or an enum specifying an animation.

I've managed to get a custom editor working for the Dialogues on their own:

 list = new ReorderableList(serializedObject,
                 serializedObject.FindProperty("directions"),
                 true, true, true, true);
 
         list.drawElementCallback =
         (Rect rect, int index, bool isActive, bool isFocused) => {
             var element = list.serializedProperty.GetArrayElementAtIndex(index);
             rect.y += 2;
             EditorGUI.PropertyField(
                 new Rect(rect.x, rect.y, 60, EditorGUIUtility.singleLineHeight),
                 element.FindPropertyRelative("actor"), GUIContent.none);
             EditorGUI.PropertyField(
                 new Rect(rect.x + 60, rect.y, 600, EditorGUIUtility.singleLineHeight),
                 element.FindPropertyRelative("dialogue"), GUIContent.none);
             EditorGUI.PropertyField(
                 new Rect(rect.x + rect.width - 55, rect.y, 40, EditorGUIUtility.singleLineHeight),
                 element.FindPropertyRelative("timeStart"), GUIContent.none);
             EditorGUI.PropertyField(
                 new Rect(rect.x + rect.width - 10, rect.y, 20, EditorGUIUtility.singleLineHeight)

That worked fine for just the dialogue objects but when I try to combine both the list shows up blank and complains about an object reference not set to an instance.

I'd ideally like something like the mockup below: alt text

Is this possible? I don't know enough about how serialization or editor scripting works to figure this one out on my own!

customeditor.png (37.3 kB)
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
3
Best Answer

Answer by Ran-Quan · Mar 21, 2017 at 06:07 AM

Unity's serializer has no support for polymorphism. Read more

Maybe you should unify your multiple class definitions into one single format and draw different UI based on the data. Maybe something like:

 if (directionType == ActingDirectionType.Dialogue) {
     // draw dialogue UI
 } else if (directionType == ActingDirectionType.Animation) {
     // draw animation UI
 }

Comment
Add comment · Show 2 · 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 Bunny83 · Mar 21, 2017 at 06:20 AM 2
Share

Note that the only cases where Unity does support polymorphism is for classes derived from UnityEngine.Object. The only classes you can actually use as base classes are "ScriptableObject" and "$$anonymous$$onoBehaviour". You can't derive a class directly from UnityEngine.Object and most other classes which are derived from UnityEngine.Object are sealed.

To better understand how Unity serializes "normal" serializable classes i suggest to set the asset serialization mode to "Force Text" and open your asset in a text editor. You will see that those custom serializable classes aren't serialized as "objects" but simply as nested properties of your ScriptableObject. No type information is stored. That means when deserializing your custom classes, Unity only relies on the variable type. The deserializer would even create an instance of an abstract class ^^.

avatar image Warcrea · Mar 23, 2017 at 03:53 PM 0
Share

That's a great workaround, feels obvious but once you get it in your head that you want to go with OOP it's hard to think outside that box sometimes. Worked a treat, thanks a lot.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

A node in a childnode? 1 Answer

unity doesn't recognize the derived class 3 Answers

How to add a reorderable list on CUSTOM EDITOR WINDOW? 0 Answers

How to prevent Static List from resetting when i run the game in the editor? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges