• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

137 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

Related Questions

Custom Editor List with child classes 1 Answer

UnityEvent Generic (Inspector Serialization) 0 Answers

How to draw a model in the preview window of my custom editor using OnPreviewGUI? 0 Answers

[Editor]Get FieldInfo by using an instance of that field 1 Answer

Custom Inspector changes not updating target in Edit Mode 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges