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 /
  • Help Room /
avatar image
0
Question by MichaI · Jan 06, 2019 at 11:52 PM · editorinspectorissueinheritanceplaymode

Custom Editor Inspector member class with inheritance, values resets after starting Play Mode

Hello, let's say I have following classes:

[System.Serializable]
public class A {
    public float a;
}

[System.Serializable]
public class B : A {
    public float b;
}

[System.Serializable]
public class C : A {
    public float c;
}
, Manager class which has class A as member:
using UnityEngine;
public class Manager : MonoBehaviour {
    public enum Mode { B, C };
    public Mode mode = Mode.B;
    [SerializeField]
    public A classA;
}
and custom inspector for Manager class:
 using UnityEngine;
 using UnityEditor;

 [CustomEditor(typeof(Manager))]
 public class ManagerEditor : Editor {
 
     private string[] tabNames = { "B", "C" };
     Manager myTarget;
 
     private void setClass() {
         if(myTarget.mode == Manager.Mode.B) {
             if (myTarget.classA.GetType() != typeof(B)) {
                 myTarget.classA = new B();
             }
             displayB();
         } else if (myTarget.mode == Manager.Mode.C) {
             if(myTarget.classA.GetType() != typeof(C)) {
                 myTarget.classA = new C();
             }
             displayC();
         }
     }
 
     private void displayC() {
         myTarget.classA.a = EditorGUILayout.FloatField("a", myTarget.classA.a);
         (myTarget.classA as C).c = EditorGUILayout.FloatField("c", (myTarget.classA as C).c);
     }
 
     private void displayB() {
         myTarget.classA.a = EditorGUILayout.FloatField("a", myTarget.classA.a);
         (myTarget.classA as B).b = EditorGUILayout.FloatField("b", (myTarget.classA as B).b);
     }
 
     public override void OnInspectorGUI() {
         myTarget = target as Manager;
         myTarget.mode = (Manager.Mode)GUILayout.Toolbar((int)myTarget.mode, tabNames);
         setClass();
     }
 }

Everything seams to be fine, but when I set some values in inspector in edit mode, for example mode C, and a = 1, c = 2: alt text

as soon as I press Play, every value resets: alt text

Is there any way to prevent that and keep values from edit mode in play mode or I am doing something wrong?

playmode.png (6.9 kB)
editmode.png (7.8 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

1 Reply

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

Answer by Bunny83 · Jan 07, 2019 at 12:00 AM

What you're doing wrong is that Unity's serialization system does not support inheritance for custom serializable classes. You may want to read the section

When might the serializer behave unexpectedly?

carefully.


Note that custom editor only provide a custom visualization of the serialized data. It can't change the way the serializer works. Custom serializable classes are serialized "inline". The type of the instance is not saved at all, only the values. They actually behave like structs. When the class is deserialized the type of the field is used. So no matter what object you store inside the "classA" field of your Manager, after deserialization it will be an instance of "A" since that's the type of the field.


If you need polymorphism / inheritance you have to use ScriptableObjects or MonoBehaviours, they support inheritance since those are seperately serialized objects. ScriptableObjects are meant to be saved as asset in the project. MonoBehaviours and ScriptableObject instances can be referenced by other serialized objects.

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 MichaI · Jan 07, 2019 at 02:42 PM 0
Share

Thank you very much for your aswer. I managed to solve this problem with ScriptableObjects as you advised me. Unfortunately because of this I cannot use Toolbar to automaticly chose mode and set options, but I have to create ScriptableObject assets and than attach them to my script. But maybe thats good because I can save many configurations and switch them any time. Do you know maybe some other more convinient way to automaticly create somehow scriptableobject so user don't have to create that every time?

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

136 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

Related Questions

Custom Editor List with child classes 1 Answer

How can I detect when the name of a gameobject has been changed in the editor and do something when that happens? 2 Answers

Is the glitch fixed? 0 Answers

How to reduce editor refresh time? 0 Answers

How to create a drop down menu in editor inspector 0 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