public override void OnInspectorGUI()
    {
        deleteButtonStyle = new GUIStyle(GUI.skin.button);
        deleteButtonStyle.normal.textColor = Color.red;
        deleteButtonStyle.stretchWidth     = false;

        editableFoldoutStyle = new GUIStyle(EditorStyles.foldout);
        editableFoldoutStyle.stretchWidth = false;

        int j = 0;

        // Since elements in a foreach loop are immutable, we have to store changes in temporary variables
        string groupToRename = null;
        string newGroupName  = null;
        string groupToRemove = null;

        foreach (KeyValuePair <string, ListOfString> group in blackboardGroups)
        {
            GUILayout.BeginHorizontal();

            string textInGroupNameField = EditorGUILayout.DelayedTextField(group.Key);
            Rect   foldoutRect          = GUILayoutUtility.GetLastRect();
            groupFoldouts[j] = EditorGUI.Foldout(foldoutRect, groupFoldouts[j], "", false);
            if (groupFoldouts[j] == false)
            {
                GUILayout.EndHorizontal();
                GUILayout.Space(20);
                j++;
                continue;
            }

            if (textInGroupNameField != group.Key)
            {
                groupToRename = group.Key;
                newGroupName  = textInGroupNameField;
            }

            if (GUILayout.Button("X", deleteButtonStyle))
            {
                groupToRemove = group.Key;
            }

            GUILayout.EndHorizontal();

            currentGroupKey = group.Key;
            currentGroup    = group.Value.list;

            reorderableLists[j].DoLayoutList();
            GUILayout.Space(20);

            j++;
        }

        currentGroupKey = null;
        currentGroup    = null;

        if (groupToRename != null)
        {
            Undo.RecordObject(bb, "Rename blackboard group");

            bb.RenameGroup(groupToRename, newGroupName);
            RefreshFoldoutsList();
            RefreshReordableLists();
            EditorUtility.SetDirty(bb);
        }

        if (groupToRemove != null)
        {
            Undo.RecordObject(bb, "Remove blackboard group");
            bb.RemoveGroup(groupToRemove);
            RefreshFoldoutsList();
            RefreshReordableLists();
            EditorUtility.SetDirty(bb);
        }

        if (GUILayout.Button("Add Group"))
        {
            Undo.RecordObject(bb, "Add blackboard group");
            bb.AddGroup();
            RefreshFoldoutsList();
            RefreshReordableLists();
            EditorUtility.SetDirty(bb);
        }
    }
Esempio n. 2
0
    public override void OnInspectorGUI()
    {
        deleteButtonStyle = new GUIStyle(GUI.skin.button);
        deleteButtonStyle.normal.textColor = Color.red;
        deleteButtonStyle.stretchWidth     = false;

        editableFoldoutStyle = new GUIStyle(EditorStyles.foldout);
        editableFoldoutStyle.stretchWidth = false;

        // Blackboard selector/foldout
        GUILayout.BeginHorizontal();

        showBlackboard = EditorGUILayout.Foldout(showBlackboard, "Blackboard", true);

        Blackboard newBB = (Blackboard)EditorGUILayout.ObjectField(
            bbctrl.EditorGetBlackboard(), typeof(Blackboard), false);

        if (newBB != bb)
        {
            Undo.RecordObject(bbctrl, "Update blackboardController's blackboard");
            bbctrl.EditorSetBlackboard(newBB);
            RefreshBlackboard();
            EditorUtility.SetDirty(bbctrl);
        }

        GUILayout.EndHorizontal();
        GUILayout.Space(20);

        if (!showBlackboard || bb == null)
        {
            return;
        }


        int j = 0;

        // Since elements in a foreach loop are immutable, we have to store changes in temporary variables
        string groupToRename = null;
        string newGroupName  = null;
        string groupToRemove = null;

        foreach (KeyValuePair <string, ListOfString> group in blackboardGroups)
        {
            GUILayout.BeginHorizontal();

            string textInGroupNameField = EditorGUILayout.DelayedTextField(group.Key);
            Rect   foldoutRect          = GUILayoutUtility.GetLastRect();
            groupFoldouts[j] = EditorGUI.Foldout(foldoutRect, groupFoldouts[j], "", false);
            if (groupFoldouts[j] == false)
            {
                GUILayout.EndHorizontal();
                GUILayout.Space(20);
                j++;
                continue;
            }

            if (textInGroupNameField != group.Key)
            {
                groupToRename = group.Key;
                newGroupName  = textInGroupNameField;
            }

            if (GUILayout.Button("X", deleteButtonStyle))
            {
                groupToRemove = group.Key;
            }

            GUILayout.EndHorizontal();

            currentGroupKey = group.Key;
            currentGroup    = group.Value.list;

            reorderableLists[j].DoLayoutList();
            GUILayout.Space(20);

            j++;
        }

        currentGroupKey = null;
        currentGroup    = null;

        if (groupToRename != null)
        {
            Undo.RecordObject(bb, "Rename blackboard group");

            bb.RenameGroup(groupToRename, newGroupName);
            RefreshFoldoutsList();
            RefreshReordableLists();
            EditorUtility.SetDirty(bb);
            EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
        }

        if (groupToRemove != null)
        {
            Undo.RecordObject(bb, "Remove blackboard group");
            bb.RemoveGroup(groupToRemove);
            RefreshFoldoutsList();
            RefreshReordableLists();
            EditorUtility.SetDirty(bb);
            EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
        }

        if (GUILayout.Button("Add Group"))
        {
            Undo.RecordObject(bb, "Add blackboard group");
            bb.AddGroup();
            RefreshFoldoutsList();
            RefreshReordableLists();
            EditorUtility.SetDirty(bb);
            EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
        }
    }