Search Unity

Scene not set dirty when using Serializables in Custom Editor

Discussion in 'Immediate Mode GUI (IMGUI)' started by Eknoes, Aug 15, 2016.

  1. Eknoes

    Eknoes

    Joined:
    Sep 15, 2015
    Posts:
    56
    Hi,

    i'm using a Serializable class to edit it's values in the inspector. This works fine when i use it directly in a MonoBehavior, but when i use a custom editor script to manipulate the data the scene is not set dirty. It's really annoying that i can't save a scene after i edited the values in the inspector.

    Here is a small example for demonsration:

    Code (CSharp):
    1. using UnityEngine;
    2. public class DataManager : MonoBehaviour
    3. {
    4.     public Data data;
    5. }
    6.  
    7. using System;
    8. [Serializable]
    9. public class Data
    10. {
    11.     public int num;
    12. }
    13.  
    14. using UnityEditor;
    15. [CustomEditor(typeof(DataManager))]
    16. public class DataEditor : Editor
    17. {
    18.     private DataManager dataManager;
    19.  
    20.     public override void OnInspectorGUI()
    21.     {
    22.         dataManager = target as DataManager;
    23.         dataManager.data.num = EditorGUILayout.IntField(dataManager.data.num);
    24.     }
    25. }

    The only solution i found is to manually check for changes in OnInspectorGUI and then set the scene dirty or use EditorGUI.BeginChangeCheck(); and Undo.RecordObject, but that means i need to write additional code for every single variable.

    Is there no better solution for this?
     
  2. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
  3. Eknoes

    Eknoes

    Joined:
    Sep 15, 2015
    Posts:
    56
  4. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    Agreed, I would definitely like to see more compile time error prevention with SerializedProperties. It's something I've brought up with our scripting/serialization teams quite a bit. :)

    On the editor team, we're heavy users of this API too.