Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 smileteam · Oct 21, 2016 at 08:39 PM · physicslistinspectorserializationclasses

Serialising subclass variables in inspector

Is it possible to serialise the variables in a subclass in the inspector (In list)?

At the moment I have a sub class, a base class, and a "class manager" that has a list of type baseclass. In runtime it adds classes of type subclass into the list of baseclass.
But when the classes are added to the list and updates the inspector, it looks like the inspector only shows the baseclass variables, and none of the subclass' variables.
For the most part it looks like the list supports subclasses, and I can call subclass functions and whatnot, but the inspector still doesn't serialise the subclass variables?

Is there a fix for this? Or should I find a different solution for having a list of different subclasses?

alt text

BaseClass.cs :

 using UnityEngine;
 using System.Collections;
 
 [System.SerializableAttribute]
 public class BaseClass {
 
     public string name;
 
     public int baseInt;
 
     public float baseFloat;
     public virtual void TestFunction(float flt)
     {
         baseFloat -= flt;
         Debug.Log("TestFunction baseclass called.");
     }
 }

SubClass.cs

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class SubClass : BaseClass {
 
     [SerializeField]
     public float subFloat;
 
     public override void TestFunction(float flt) {
         base.TestFunction(flt);
         Debug.Log(subFloat);
     }
 }

ClassManager.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ClassManager : MonoBehaviour {
 
     public List<BaseClass> classList = new List<BaseClass>();
 
     BaseClass currentClass;
 
     public void Button() {
         BaseClass wow = new SubClass();
         AddClassToList(wow);
         currentClass = classList[0];
     }
 
     public void OtherButton() {
         currentClass.TestFunction(0.5f);
     }
 
     public void AddClassToList(BaseClass bs) {
         classList.Add(bs);
         print(bs.GetType());
     }
 
     public void RemoveClassFromList(BaseClass bs) {
         classList.Remove(bs);
     }
 }


capture.png (23.1 kB)
Comment
Add comment · Show 2
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 hexagonius · Oct 21, 2016 at 09:36 PM 0
Share

Just looked up this post and the last code chunk might just do the trick. If not, the whole post looks like very interesting for you:

http://answers.unity3d.com/questions/51615/do-custom-inspectors-support-inheritance.html

avatar image Bunny83 hexagonius · Oct 21, 2016 at 11:29 PM 0
Share

The question you've linked is not relevant here. The question was about $$anonymous$$onoBehaviour derived classes and their custom inspectors. Custom inspectors can't change how things are serialized, only how they are displayed in the inspector. The code WhyDoIDoIt posted is no longer relevant as Unity now supports writing a custom inspector for the base class and have it apply to derived classes as well. That's why the accepted answer is the accepted answer ^^.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Oct 21, 2016 at 11:24 PM

No, Unity doesn't support inheritance for custom serializable classes. Only classes derived from UnityEngine.Object support inheritance when it comes to serialization in Unity. The only classes you can actually use as base class are MonoBehaviour (which always have to be attached to a gameobject) or ScriptableObject.

Unity serializes custom serializable classes based on the variable type. So no matter what actual instance the variable references it will always be serialized as base class.


Update:
Since Unity 2019.3 there's an update:


Kernel: Introducing new serialization attribute: SerializeReference


Unity now has the SerializeReference attribute which adds some new features to the serialization system and allows custom serializable classes to be serialized "as reference". However those references are still local to the housing object. So they are serialized alongside with the housing object. Though such fields now support inheritance and the type of the instance is no longer determined by the field type but the actual type.


This adds makes it more versatile, but at the same time the inspector will no longer create those classes for you since even "null" is now supported. As I mentioned above those references can't span across multiple components or scriptable objects. Those can only reference other custom serializable classes that Unity's serialization system supports.


As you can read in the documentation such fields now also support interfaces or abstract types (since they are no longer bound to their field type)

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
2

Answer by Ruzihm · Mar 10, 2021 at 06:51 AM

The above answer is out of date with the introduction of the SerializeReference decorator. Adding this decorator to the above code will now produce the desired outcome:

 [SerializeReference] public List classList = new List();
Comment
Add comment · Show 1 · 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 krechevskoy · Nov 03, 2021 at 10:47 PM 0
Share

Bless you! You have no idea how much pain you just spared me.

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

90 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

Related Questions

*Really* need help on Serialisation, stuck on this for over a week now, deadline looming 1 Answer

Serialize variables of elements in a list ? 0 Answers

Difference between assigning a value in inspector and with a custom editor script 1 Answer

A node in a childnode? 1 Answer

How do I make child classes in a list editable from the Inspector? 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