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 /
This question was closed Jul 17, 2018 at 04:40 PM by YoloJero for the following reason:

Problem is not reproducible or outdated

avatar image
1
Question by YoloJero · May 03, 2018 at 09:06 AM · listserializationinheritancecustom editorserializedproperty

How to insert an element of specific type to a list as serializedProperty?

I' writing on a custom editor for a class OtherClass containing a List<MyBaseClass>.


I'm deriving various other classes e.g. MyIntClass, MyListClass, etc from MyBaseClass.


Later I allow to add all those classes to the list in order to get them serialized in XML like

 [XmlArrayItem(Type = typeof(MyIntClass), ElementName = "Int")]
 [XmlArrayItem(Type = typeof(MyListClass), ElementName = "List")]
 public List<MyBaseClass> Items = new List<MyBaseClass>();


Adding elements via script just works like e.g.

 Items.Add(new MyIntClass(5));
 Items.Add(new MyListClass(someList));

But now I want to add items from the inspector, so I'm writing a custom editor script. The goal is that you can choose what type shall be added to the list, set some values and hit ADD.


But I'm stuck on the point where elements with different types shall be added.

I know usually you can do something like

 SerializedProperty _list = serializedObject.FindProperty("Items");
 _list.InsertArrayElementAtIndex(_list.arraySize);


But this will allways add an element of type MyBaseClass.


(How) Can I make the added element have a specific type like e.g. MyIntClass?

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

  • Sort: 
avatar image
1

Answer by Bunny83 · May 03, 2018 at 09:25 AM

In short: You can't with normal serializable classes as Unity's serialization system does not support polymorphism for custom serializable classes.


Make sure you read the script serialization documentation page carefully.


Serializable structs and classes are actually serialized "inline" in either a MonoBehaviour or a ScriptableObject. Only those two base classes support inheritance / polymorphism. If you haven't set the asset serialization mode to "force text" yet you may try that and have a look at how the serialized data actually looks like. If you're not familiar with the YAML format, have a look at Unity's documentation and at the example file. All serialized fields of a component / ScriptableObject are serialized / deserialized based on the field type. The only exception are serialized references which only work for UnityEngine.Object derived classes (and MonoBehaviour and ScriptableObject are the only two base classes for your own classes).

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

Follow this Question

Answers Answers and Comments

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

Related Questions

Unable to serialize my list in a Unity Custom Editor script. 1 Answer

A node in a childnode? 1 Answer

Finding property with serializedObject on script with a generic 0 Answers

Using Generic List with serializedProperty Inspector 3 Answers

Why would pressing the play button delete or recreate a list elements? 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