Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
PascalRunde opened this issue Apr 23, 2021 · 0 comments

Comments

@PascalRunde
Copy link

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant