Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
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

2 Replies

· 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.

avatar image
0

Answer by $$anonymous$$ · Jan 03 at 07:28 PM

It's possible natively since 2019.3 release via [SerializeReference] attribute https://docs.unity3d.com/ScriptReference/SerializeReference.html

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

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

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

Is it posible to edit MonoScript attributes from editor? 1 Answer

Serialize class that derives from abstract class 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