void ShowAddSubLayer(StateLayer parent, Rect rect) { layerTypesName[0] = "Add"; if (Selection.gameObjects.Length <= 1) { rect.x = Screen.width - 72 - EditorGUI.indentLevel * 16; rect.y += 1; rect.width = 36 + EditorGUI.indentLevel * 16; EditorGUI.BeginDisabledGroup(Application.isPlaying); GUIStyle style = new GUIStyle("MiniToolbarPopup"); style.fontStyle = FontStyle.Bold; style.alignment = TextAnchor.MiddleCenter; int layerTypeIndex = EditorGUI.Popup(rect, layerTypes.IndexOf(selectedLayerType) + 1, layerTypesName.ToArray(), style) - 1; selectedLayerType = layerTypeIndex == -1 ? null : layerTypes[Mathf.Clamp(layerTypeIndex, 0, layerTypes.Count - 1)]; if (selectedLayerType != null) { StateMachineUtility.AddLayer(machine, selectedLayerType, parent); selectedLayerType = null; } EditorGUI.EndDisabledGroup(); } }
public static void UpdateLayerStates(StateMachine machine, StateLayer layer) { foreach (Type stateType in LayerStateDict[layer.GetType()]) { StateMachineUtility.AddState(machine, layer, stateType); } }
void CleanUp() { foreach (UnityEngine.Object selectedObject in targets) { StateMachine selectedMachine = selectedObject as StateMachine; if (selectedMachine != null) { StateMachineUtility.UpdateCallbacks(selectedMachine); StateMachineUtility.CleanUp(selectedMachine, selectedMachine.gameObject); } } }
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()); }
void ShowAddLayer() { layerTypes = new List <Type>(); layerTypesName = new List <string> { "Add Layer" }; foreach (Type layerType in StateMachineUtility.LayerStateDict.Keys) { if (Array.TrueForAll(existingLayers, layer => layer.GetType() != layerType)) { layerTypes.Add(layerType); layerTypesName.Add(StateMachineUtility.LayerTypeNameDict[layerType]); } } if (Selection.gameObjects.Length <= 1) { EditorGUI.BeginDisabledGroup(Application.isPlaying); GUIStyle style = new GUIStyle("popup"); style.fontStyle = FontStyle.Bold; style.alignment = TextAnchor.MiddleCenter; int layerTypeIndex = EditorGUILayout.Popup(layerTypes.IndexOf(selectedLayerType) + 1, layerTypesName.ToArray(), style) - 1; selectedLayerType = layerTypeIndex == -1 ? null : layerTypes[Mathf.Clamp(layerTypeIndex, 0, layerTypes.Count - 1)]; EditorGUI.EndDisabledGroup(); if (selectedLayerType != null) { StateMachineUtility.AddLayer(machine, selectedLayerType, machine); selectedLayerType = null; } } else { GUI.Box(EditorGUI.IndentedRect(EditorGUILayout.GetControlRect()), "Multi-editing is not supported.", new GUIStyle(EditorStyles.helpBox)); } }
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(); } }
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()); }
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(); }