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
1
Question by ArseneySorokin · Jun 19, 2021 at 11:17 AM · scriptableobjectcustomeditor

Custom Editor for a scriptable object with an abstract list

Hello,

I have a Scriptable Object by the name "AIStrategyBuildingBlock" with a list of objects of a class by the name "AICondition".

AICondition is an abstract class with several classes deriving from it.

What I wish to do, is to create a CustomEditor for "AIStrategyBuildingBlock" which will allow me to add and modify the AICondition's within it. Probably by having a dropdown which chooses the type of AICondition to add before clicking the "add button".

I was able to achieve this without SerializedFields but no matter what I did I couldn't get the object to keep its values after pressing "Play" (SetDirty didn't help), so I'm assuming my approach was wrong.

How would you approach solving this issue?

Comment
Add comment · Show 3
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 viesc123 · Jun 19, 2021 at 06:28 PM 0
Share

Are you using Undo.RecordObject in your Editor Scripts? Serialized values set by scripts usually only become persistent when you do.

avatar image ArseneySorokin viesc123 · Jun 20, 2021 at 01:31 PM 0
Share

I tried both " Undo.RecordObject" and "EditorUtility.SetDirty". None of them help. All the "simple" values are saved, but the list gets reset whenever I press play.

avatar image eneIr ArseneySorokin · Jun 21, 2021 at 02:37 AM 0
Share

Have you tried using serializedObject.FindProperty to get the list's value in the onEnable()?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Jun 21, 2021 at 07:16 AM

Note that for the longest time it was true that Unity did not support polymorphism for custom serializable classes as they were always serialized based on the field type. So there was no explicit type information stored in the serialized data which is of course necessary in order to deserialized the exact instance.


However they come up with a kinda hacky solution: the SerializeReference attribute. This will treat a reference type of a custom serializable class differently. Unity will actually create a "local" reference system within the same serialization stream.


So all the serialization system needs is that attribute on your field. However the downside is, you don't get any automatic inspector support here. Since you can now store derived classes in that field, you have to create a custom inspector / editor code to actually create and maintain those instances. This makes it almost impossible to use the normal recommended SerializedObject / SerializedProperty route- That also means providing multi object editing would become a real pain since when you select several "AIStrategyBuildingBlock" objects at once and the field / array could contain several different types, how would you handle that?


So going that route means:

  • you loose multi object editing because there's most likely no sensible way how to implement it.

  • you may have to mix the SerializedObject / SerializedProperty approach with direct accessing the target / targets reference or just use the target(s) object(s).

  • you have to write a lot of extra editor code in order to create and maintain those serialized references.


So in summary: It's now possible to serialize abstract type references, however it requires quite a bit of work and has some limitations.

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

Answer by ArseneySorokin · Jun 21, 2021 at 05:33 AM

I wasn't able to solve the problem. Turns out Unity cannot serialize abstract classes. https://docs.unity3d.com/Manual/script-Serialization.html

 How to ensure a custom class can be serialized
 Ensure it:
 
 Has the Serializable attribute
 Is not abstract
 Is not static
 Is not generic, though it may inherit from a generic class
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

125 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 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

Prevent double serialization of a custom class 0 Answers

Manually Saving ScriptableObject due to 2D Array... 0 Answers

Custom Editor Window not displaying anything 1 Answer

Problem with ScriptableObject and Custom Editor 2 Answers

How to create a custom editor for a ScriptableObject? 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