Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by NamelessGames_ · Jun 04 at 10:27 PM · unityeditorinspectorserializationpropertydrawer

Serialize Custom Abstract Class (No-MonoBehaviour / No-ScriptableObject)

Hi guys,
I'm trying to serializa a custom abstract class but I can't reach a point picking which derived class it should be.

I saw that OdinInspector can do this if my field is inside a SerializedMonoBehaviour instead of MonoBehaviour. However I don't want this dependency.

Anyone figures out how to reach this kind of serialization?

I've the inspector look and functionality ready, I only miss how to (any way, this is an example of my missing point)

property.objectReferenceValue = myCustomAbstractClass



Code example:

 [Serializable]
 public abstract class Condition 
 {
     public string Name;
 }
 
 [Serializable]
 public class IntCondition : Condition
 {
     [SerializeField] int _comparisonTerm1 = 0;
     [SerializeField] int _comparisonTerm2 = 0;
 }
 
 [Serializable]
 public class BoolCondition : Condition
 {
     [SerializeField] bool _comparisonTerm = false;
 }

 public class ExampleScript : MonoBehaviour
 {
     [SerializeField] Condition _condition = null;
 }
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
1

Answer by Bunny83 · Jun 05 at 09:07 AM

Your question is a bit confusing. I guess by

No-MonoBehaviour / No-ScriptableObject

you mean that you still want the class to be serialized inside a MonoBehaviour but the abstract class itself is not a MonoBehaviour / ScriptableObject.


Originally Unity did not support any sort of polymorphism for custom serializable classes. This was the case for years. Unity now introduced the SerializeReference attribute. This allows to serialize plain C# classes and support polymorphism. However even Unity does support serialzing such class constructs, Unity does not provide any built-in way to create those classes. You have to provide your own custom editor / property drawer any implement whatever you need.


One of the issues that Unity would have is: what classes it should offer for you? What if those classes do not have a parameterless constructor? What if the type is a generic type? Solutions like the OdinInspector applies several restrictions and applies some assumptions how you may use such references. Though Unity now supports "object" variables as well which could contain literally any C# object that is serializable. So how would an inspector look like? How would you choose which object should be created?


Of course my last question can usually easily be answered in a specific case, as your concrete example. However Unity does not know your intentions. There are many editor UI implementations out there which provide different level of automatic support for derived classes. Have a look over here. Maybe in the future Unity implements something similar as well. However this would be a neverending story and would produce more and more grief among users as there's always that one specific usecase that SerializeReference can handle but the UI doesn't.

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

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

How to correctly use [SerializeReference] on generic types 0 Answers

Unity types unable to be serialized 2 Answers

Unity scriptable object inspector, field overlap eachother 0 Answers

Restricting enum options in inspector when using a propertydrawer. 0 Answers

Property Drawers In Serializable Classes - GetPropertyHeight 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