GUIContent GetLayerLabel()
        {
            string label = currentLayer.GetType().Name;

            if (Application.isPlaying)
            {
                Type[]   activeStateTypes = currentLayer.GetActiveStates().GetTypes();
                string[] activeStateNames = new string[activeStateTypes.Length];
                for (int j = 0; j < activeStateNames.Length; j++)
                {
                    activeStateNames[j] = StateMachineUtility.FormatState(activeStateTypes[j], currentLayer);
                }

                label += " (" + activeStateNames.Concat(", ") + ")";
            }

            return(label.ToGUIContent());
        }
Example #2
0
        void ShowStates(SerializedProperty statesProperty, StateLayer layer)
        {
            for (int i = 0; i < statesProperty.arraySize; i++)
            {
                SerializedProperty stateProperty = statesProperty.GetArrayElementAtIndex(i);
                State state = stateProperty.GetValue <UnityEngine.Object>() as State;

                if (state == null)
                {
                    ShowLayer(statesProperty, i);
                    continue;
                }

                BeginBox(GetBoxStyle(state));

                Foldout(stateProperty, StateMachineUtility.FormatState(state.GetType(), layer).ToGUIContent(), GetStateStyle(state));
                Reorderable(statesProperty, i, true);

                ShowState(stateProperty);

                EndBox();
            }
        }
        void ShowStates()
        {
            for (int i = 0; i < statesProperty.arraySize; i++)
            {
                currentStateProperty = statesProperty.GetArrayElementAtIndex(i);
                currentState         = currentStateProperty.GetValue <State>();

                if (currentState == null)
                {
                    DeleteFromArray(statesProperty, i);
                    break;
                }

                BeginBox();

                Foldout(currentStateProperty, StateMachineUtility.FormatState(currentState.GetType(), currentLayer).ToGUIContent(), GetStateStyle());
                Reorderable(statesProperty, i, true);

                ShowState();

                EndBox();
            }
        }
Example #4
0
        GUIContent GetLayerLabel(StateLayer layer)
        {
            string label = layer.GetType().Name;

            if (Application.isPlaying && PrefabUtility.GetPrefabType(machine) != PrefabType.Prefab)
            {
                IState[] activeStates     = layer.GetActiveStates();
                string[] activeStateNames = new string[activeStates.Length];

                for (int i = 0; i < activeStateNames.Length; i++)
                {
                    activeStateNames[i] = activeStates[i] is IStateLayer ? activeStates[i].GetType().Name.Split('.').Last() : StateMachineUtility.FormatState(activeStates[i].GetType(), layer);
                }

                label += " (" + activeStateNames.Concat(", ") + ")";
            }

            return(label.ToGUIContent());
        }
Example #5
0
        void ShowLayer(SerializedProperty layersProperty, int index)
        {
            SerializedProperty layerProperty        = layersProperty.GetArrayElementAtIndex(index);
            StateLayer         layer                = layerProperty.GetValue <StateLayer>();
            SerializedObject   layerSerialized      = new SerializedObject(layer);
            SerializedProperty statesProperty       = layerSerialized.FindProperty("stateReferences");
            SerializedProperty activeStatesProperty = layerSerialized.FindProperty("activeStateReferences");

            StateMachineUtility.AddMissingStates(machine, layer);

            BeginBox(GetBoxStyle(layer));

            Rect rect = EditorGUILayout.BeginHorizontal();

            ShowAddSubLayer(layer, rect);

            if (DeleteFoldOut(layersProperty, index, GetLayerLabel(layer), GetLayerStyle(layer)))
            {
                StateMachineUtility.RemoveLayer(layer);
                return;
            }

            EditorGUILayout.EndHorizontal();

            CleanUpStates(statesProperty);

            if (layerProperty.isExpanded)
            {
                EditorGUI.indentLevel += 1;

                List <string> currentLayerStatesName = new List <string> {
                    "Empty"
                };

                foreach (IState state in statesProperty.GetValues <IState>())
                {
                    currentLayerStatesName.Add(state is IStateLayer ? state.GetType().Name.Split('.').Last() : StateMachineUtility.FormatState(state.GetType(), layer));
                }

                for (int i = 0; i < activeStatesProperty.arraySize; i++)
                {
                    SerializedProperty activeStateProperty = activeStatesProperty.GetArrayElementAtIndex(i);
                    UnityEngine.Object activeState         = activeStateProperty.objectReferenceValue;

                    if (Selection.gameObjects.Length <= 1)
                    {
                        Rect dragArea = EditorGUILayout.BeginHorizontal();

                        EditorGUI.BeginChangeCheck();

                        int stateIndex = EditorGUILayout.Popup(string.Format("Active State ({0})", i), statesProperty.IndexOf(activeState) + 1, currentLayerStatesName.ToArray(), GUILayout.MinWidth(200)) - 1;
                        activeState = stateIndex == -1 ? null : statesProperty.GetValue <UnityEngine.Object>(Mathf.Clamp(stateIndex, 0, statesProperty.arraySize - 1));
                        activeStateProperty.SetValue(activeState);

                        if (EditorGUI.EndChangeCheck() && Application.isPlaying)
                        {
                            layer.SwitchState(activeState == null ? typeof(EmptyState) : activeState.GetType(), i);
                        }

                        if (i == 0)
                        {
                            SmallAddButton(activeStatesProperty);
                        }
                        else if (DeleteButton(activeStatesProperty, i))
                        {
                            break;
                        }

                        EditorGUILayout.EndHorizontal();

                        Reorderable(activeStatesProperty, i, true, EditorGUI.IndentedRect(dragArea));
                    }
                    else
                    {
                        GUI.Box(EditorGUI.IndentedRect(EditorGUILayout.GetControlRect()), "Multi-editing is not supported.", new GUIStyle(EditorStyles.helpBox));
                    }
                }

                Separator();
                ShowLayerFields(layerSerialized);

                bool stateSeparator = statesProperty.arraySize > 0;
                layerSerialized.ApplyModifiedProperties();

                ShowStates(statesProperty, layer);

                if (stateSeparator)
                {
                    Separator();
                }

                EditorGUI.indentLevel -= 1;
            }

            EndBox();
        }
        void ShowLayer()
        {
            CleanUpStates();
            SetLayerStates();

            if (currentLayerProperty.isExpanded)
            {
                EditorGUI.indentLevel += 1;

                List <string> currentLayerStatesName = new List <string> {
                    "Empty"
                };

                for (int i = 0; i < statesProperty.arraySize; i++)
                {
                    currentLayerStatesName.Add(StateMachineUtility.FormatState(statesProperty.GetArrayElementAtIndex(i).GetValue().GetType(), currentLayer));
                }

                for (int i = 0; i < currentStatesProperty.arraySize; i++)
                {
                    SerializedProperty stateProperty = currentStatesProperty.GetArrayElementAtIndex(i);

                    Rect dragArea = EditorGUILayout.BeginHorizontal();

                    EditorGUI.BeginChangeCheck();

                    int stateIndex = EditorGUILayout.Popup(string.Format("Active State ({0})", i), stateProperty.objectReferenceValue == null ? 0 : statesProperty.IndexOf(stateProperty.objectReferenceValue) + 1, currentLayerStatesName.ToArray(), GUILayout.MinWidth(200)) - 1;
                    stateProperty.SetValue(stateIndex == -1 ? null : statesProperty.GetArrayElementAtIndex(Mathf.Clamp(stateIndex, 0, statesProperty.arraySize - 1)).GetValue());

                    if (EditorGUI.EndChangeCheck() && Application.isPlaying)
                    {
                        currentLayer.SwitchState(stateProperty.objectReferenceValue == null ? typeof(EmptyState) : stateProperty.objectReferenceValue.GetType(), i);
                    }

                    if (i == 0)
                    {
                        SmallAddButton(currentStatesProperty);
                    }
                    else if (DeleteButton(currentStatesProperty, i))
                    {
                        break;
                    }

                    EditorGUILayout.EndHorizontal();

                    Reorderable(currentStatesProperty, i, true, EditorGUI.IndentedRect(dragArea));
                }

                Separator();
                ShowLayerFields();
                ShowStates();
                if (statesProperty.arraySize > 0)
                {
                    Separator();
                }

                EditorGUI.indentLevel -= 1;
            }

            currentLayerSerialized.ApplyModifiedProperties();
        }