Unity-SerializableDictionary icon indicating copy to clipboard operation
Unity-SerializableDictionary copied to clipboard

Collapsing custom inspector with Serializable Dictionary causes it to become not reopenable

Open PascalRunde opened this issue 4 years ago • 0 comments

Code example below. I'll write an easy instruction on how to reproduce the bug.

  1. Create a class containing all the code below
  2. Add SerializableDictionaryBugExample to any Monobehaviour in a scene
  3. Toggle the bool to "true"
  4. Use the "+" to add a new empty dictionary entry (Dont set the value for the custom class)
  5. Toggle the bool to "false"
  6. You will not be able to click the Toggle again

Actually clicking the toggle button works, but somehow it never really gets applied. I guess theres an exception somewhere in the OnInspectorGui loop. I couldn't finde out how to solve this problem. I would really appreciate if anyone can help me!

using System;
using UnityEditor;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.SceneManagement;
#endif

[Serializable]
public class SerializableDictionaryExample : SerializableDictionary<SomeOtherClass, int> {}

public class SomeOtherClass : MonoBehaviour
{
    
}

public class SerializableDictionaryBugExample : MonoBehaviour
{
    public bool boolean;

    public SerializableDictionaryExample DictionaryExample;

#if UNITY_EDITOR
    [CustomEditor(typeof(SerializableDictionaryBugExample))]
    [CanEditMultipleObjects]
    public class SerializableDictionaryBugExampleEditor : Editor
    {
        private SerializedProperty DictionaryExampleProperty;
        private SerializableDictionaryBugExample exampleClass;

        public void OnEnable()
        {
            DictionaryExampleProperty = serializedObject.FindProperty("DictionaryExample");
            exampleClass = (SerializableDictionaryBugExample) target;
        }

        public override void OnInspectorGUI()
        {
            EditorGUI.BeginChangeCheck();
            serializedObject.Update();

            EditorGUILayout.LabelField("General", EditorStyles.boldLabel);
            exampleClass.boolean = EditorGUILayout.Toggle("ToggleButton", exampleClass.boolean);
            if (exampleClass.boolean)
            {
                EditorGUILayout.PropertyField(DictionaryExampleProperty,
                    new GUIContent("Dictionary", "Some Tooltip")
                );
            }


            serializedObject.ApplyModifiedProperties();
            var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
            if (EditorGUI.EndChangeCheck())
            {
                EditorSceneManager.MarkSceneDirty(prefabStage.scene);
            }
        }
    }
#endif
}

PascalRunde avatar Apr 23 '21 15:04 PascalRunde