Beispiel #1
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();
            }
        }
Beispiel #2
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].GetTypeName() : StateMachineUtility.FormatState(activeStates[i].GetType(), layer);
                }

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

            return(label.ToGUIContent());
        }
Beispiel #3
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();

            ShowLayerOptions(layer, rect);

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

            EditorGUILayout.EndHorizontal();

            CleanUpStates(statesProperty);

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

                List <string> currentLayerStatesName = new List <string> {
                    "Empty"
                };
                IState[] states = statesProperty.GetValues <IState>();

                for (int i = 0; i < states.Length; i++)
                {
                    IState state = states[i];
                    currentLayerStatesName.Add(state is IStateLayer ? state.GetTypeName() : 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();
        }