void SetLayerStates() { foreach (Type stateType in StateMachineUtility.LayerStateDict[currentLayer.GetType()]) { if (statesProperty.arraySize == 0 || statesProperty.TrueForAll <State>(state => state.GetType() != stateType)) { AddToArray(statesProperty); State newState = currentLayer.gameObject.AddComponent(stateType) as State; newState.layer = currentLayer; newState.machine = stateMachine; newState.hideFlags = HideFlags.HideInInspector; statesProperty.GetLastArrayElement().SetValue(newState); } } }
public static void UpdateLayerStates(StateMachine machine, StateLayer layer) { foreach (Type stateType in LayerStateDict[layer.GetType()]) { StateMachineUtility.AddState(machine, layer, stateType); } }
void ShowLayers() { layersProperty = serializedObject.FindProperty("layers"); CleanUpLayers(); ShowAddLayer(); if (layersProperty.arraySize > 0) { Separator(); } for (int i = 0; i < layersProperty.arraySize; i++) { currentLayerProperty = layersProperty.GetArrayElementAtIndex(i); currentLayer = currentLayerProperty.GetValue <StateLayer>(); currentLayerSerialized = new SerializedObject(currentLayerProperty.objectReferenceValue); statesProperty = currentLayerSerialized.FindProperty("states"); currentStatesProperty = currentLayerSerialized.FindProperty("currentStates"); BeginBox(); GUIStyle style = new GUIStyle("foldout"); style.fontStyle = FontStyle.Bold; if (DeleteFoldOut(layersProperty, i, currentLayer.GetType().Name.ToGUIContent(), style, OnLayerDeleted)) { break; } ShowLayer(); EndBox(); } }
public static void UpdateLayerStates(StateMachine machine, StateLayer layer) { var types = LayerTypeStateTypeDict[layer.GetType()]; for (int i = 0; i < types.Count; i++) { AddState(machine, layer, types[i]); } }
public static void AddMissingStates(StateMachine machine, StateLayer layer) { UnityEditor.SerializedObject layerSerialized = new UnityEditor.SerializedObject(layer); UnityEditor.SerializedProperty statesProperty = layerSerialized.FindProperty("stateReferences"); foreach (Type stateType in LayerStateDict[layer.GetType()]) { if (!Array.Exists(statesProperty.GetValues <UnityEngine.Object>(), state => state.GetType() == stateType)) { AddState(machine, layer, stateType); } } }
public override void OnEnable() { base.OnEnable(); layer = (StateLayer)target; if (layer.Machine == null) { Type layerType = layer.GetType(); StateMachine machine = layer.CachedGameObject.GetOrAddComponent<StateMachine>(); StateMachineUtility.AddLayer(machine, layerType, machine); } }
public override void OnEnable() { base.OnEnable(); layer = (StateLayer)target; if (layer.Machine == null) { Type layerType = layer.GetType(); StateMachine machine = layer.CachedGameObject.GetOrAddComponent <StateMachine>(); StateMachineUtility.AddLayer(machine, layerType, machine); } }
public static void AddMissingStates(StateMachine machine, StateLayer layer) { #if UNITY_EDITOR var layerSerialized = new UnityEditor.SerializedObject(layer); var statesProperty = layerSerialized.FindProperty("stateReferences"); var stateTypes = LayerTypeStateTypeDict[layer.GetType()]; for (int i = 0; i < stateTypes.Count; i++) { var stateType = stateTypes[i]; if (statesProperty != null && !Array.Exists(statesProperty.GetValues <UnityEngine.Object>(), state => state.GetType() == stateType)) { AddState(machine, layer, stateType); } } #endif }
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()); }
public static string GetLayerPrefix(StateLayer layer) { return(GetLayerPrefix(layer.GetType())); }
public static void UpdateLayerStates(StateMachine machine, StateLayer layer) { var types = LayerTypeStateTypeDict[layer.GetType()]; for (int i = 0; i < types.Count; i++) AddState(machine, layer, types[i]); }
public static string GetLayerPrefix(StateLayer layer) { return GetLayerPrefix(layer.GetType()); }
public static void AddMissingStates(StateMachine machine, StateLayer layer) { #if UNITY_EDITOR var layerSerialized = new UnityEditor.SerializedObject(layer); var statesProperty = layerSerialized.FindProperty("stateReferences"); var stateTypes = LayerTypeStateTypeDict[layer.GetType()]; for (int i = 0; i < stateTypes.Count; i++) { var stateType = stateTypes[i]; if (statesProperty != null && !Array.Exists(statesProperty.GetValues<UnityEngine.Object>(), state => state.GetType() == stateType)) AddState(machine, layer, stateType); } #endif }
void ShowLayerOptions(StateLayer layer, Rect rect) { bool isScrolling = (Screen.width - rect.xMax) > 5; GUIStyle style = new GUIStyle("MiniToolbarPopup"); style.fontStyle = FontStyle.Bold; rect.x = Screen.width - (isScrolling ? 60 : 45) - EditorGUI.indentLevel * 15; rect.y += 1; rect.width = 24 + EditorGUI.indentLevel * 15; if (!Application.isPlaying && (Event.current.type == EventType.ExecuteCommand || rect.Contains(Event.current.mousePosition))) { List<UnityEngine.Object> validParents = new List<UnityEngine.Object>(); List<Type> layerTypes = new List<Type>(); List<string> options = new List<string> { "..." }; bool machineIsParent = StateMachineUtility.IsParent(layer, machine); foreach (KeyValuePair<Type, List<Type>> pair in StateMachineUtility.LayerTypeStateTypeDict) { PropertyInfo layerProperty = pair.Key.GetProperty("Layer", ReflectionExtensions.AllFlags); PropertyInfo machineProperty = pair.Key.GetProperty("Machine", ReflectionExtensions.AllFlags); if ((machineProperty == null || machineProperty.PropertyType.IsInstanceOfType(machine)) && (layerProperty == null || layerProperty.PropertyType.IsInstanceOfType(layer)) && Array.TrueForAll(existingLayers, existingLayer => existingLayer.GetType() != pair.Key)) { layerTypes.Add(pair.Key); options.Add("Add/" + StateMachineUtility.LayerTypeFormattedDict[pair.Key]); } } if (!machineIsParent) { validParents.Add(machine); options.Add("Move To/Machine"); } for (int i = 0; i < existingLayers.Length; i++) { StateLayer existingLayer = existingLayers[i]; if (layer != existingLayer && !StateMachineUtility.IsParent(layer, existingLayer) && !StateMachineUtility.GetSubLayersRecursive(layer).Contains(existingLayer)) { validParents.Add(existingLayer); options.Add("Move To/" + StateMachineUtility.FormatLayer(existingLayer.GetType())); } } options.Add("Copy"); if (EditorPrefs.GetString("Clipboard Layer Type") == layer.GetType().Name) { options.Add("Paste/Layer"); options.Add("Paste/Layer and States"); options.Add("Paste/Layer and Sublayers"); options.Add("Paste/All"); } options.Add("Generate"); if (Selection.gameObjects.Length <= 1) { // int index = EditorGUI.Popup(rect, layerTypes.IndexOf(selectedLayerType) + 1, options.ToArray(), style) - 1; int index = EditorGUI.Popup(rect, 0, options.ToArray(), style) - 1; string option = index == -1 ? "" : options[index + 1]; if (index < layerTypes.Count) { selectedLayerType = index == -1 ? null : layerTypes[Mathf.Clamp(index, 0, options.Count - 1)]; if (selectedLayerType != null) { StateMachineUtility.AddLayer(machine, selectedLayerType, layer); selectedLayerType = null; } } else if (option.StartsWith("Move To")) { UnityEngine.Object parent = validParents[index - layerTypes.Count]; StateMachineUtility.MoveLayerTo(layer, parent); } else if (option.StartsWith("Copy")) { EditorPrefs.SetString("Clipboard Layer Type", layer.GetType().Name); EditorPrefs.SetInt("Clipboard Layer ID", layer.GetInstanceID()); } else if (option.StartsWith("Paste")) { StateLayer layerToCopy = EditorUtility.InstanceIDToObject(EditorPrefs.GetInt("Clipboard Layer ID")) as StateLayer; if (option == "Paste/Layer") { StateMachineUtility.CopyLayer(layer, layerToCopy, false, false); } else if (option == "Paste/Layer and States") { StateMachineUtility.CopyLayer(layer, layerToCopy, true, false); } else if (option == "Paste/Layer and Sublayers") { StateMachineUtility.CopyLayer(layer, layerToCopy, false, true); } else if (option == "Paste/All") { StateMachineUtility.CopyLayer(layer, layerToCopy, true, true); } EditorPrefs.SetString("Clipboard Layer Type", ""); EditorPrefs.SetInt("Clipboard Layer ID", 0); } else if (option.StartsWith("Generate")) { StateMachineGeneratorWindow generator = StateMachineGeneratorWindow.Create(); string layerTypeName = layer.GetTypeName(); string layerScriptPath = AssetDatabaseUtility.GetAssetPath(layerTypeName + ".cs"); generator.Path = string.IsNullOrEmpty(layerScriptPath) ? generator.Path : Path.GetDirectoryName(layerScriptPath); generator.Machine = StateMachineUtility.FormatMachine(machine); generator.Layer = layerTypeName; generator.Inherit = StateMachineUtility.FormatLayer(layer.GetType().BaseType); generator.SubLayer = layer.Layer == null ? "" : StateMachineUtility.FormatLayer(layer.Layer.GetType()); } } } else { EditorGUI.BeginDisabledGroup(Application.isPlaying); EditorGUI.Popup(rect, 0, new[] { "..." }, style); EditorGUI.EndDisabledGroup(); } }
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(); }
void ShowLayers() { layersProperty = serializedObject.FindProperty("layers"); CleanUpLayers(); ShowAddLayer(); if (layersProperty.arraySize > 0) { Separator(); } for (int i = 0; i < layersProperty.arraySize; i++) { currentLayerProperty = layersProperty.GetArrayElementAtIndex(i); currentLayer = currentLayerProperty.GetValue<StateLayer>(); currentLayerSerialized = new SerializedObject(currentLayerProperty.objectReferenceValue); statesProperty = currentLayerSerialized.FindProperty("states"); currentStatesProperty = currentLayerSerialized.FindProperty("currentStates"); BeginBox(); GUIStyle style = new GUIStyle("foldout"); style.fontStyle = FontStyle.Bold; if (DeleteFoldOut(layersProperty, i, currentLayer.GetType().Name.ToGUIContent(), style, OnLayerDeleted)) { break; } ShowLayer(); EndBox(); } }
public static void AddMissingStates(StateMachine machine, StateLayer layer) { #if UNITY_EDITOR UnityEditor.SerializedObject layerSerialized = new UnityEditor.SerializedObject(layer); UnityEditor.SerializedProperty statesProperty = layerSerialized.FindProperty("stateReferences"); foreach (Type stateType in LayerStateDict[layer.GetType()]) { if (!Array.Exists(statesProperty.GetValues<UnityEngine.Object>(), state => state.GetType() == stateType)) { AddState(machine, layer, stateType); } } #endif }
public static void UpdateLayerStates(StateMachine machine, StateLayer layer) { foreach (Type stateType in LayerStateDict[layer.GetType()]) { StateMachineUtility.AddState(machine, layer, stateType); } }
void ShowLayerOptions(StateLayer layer, Rect rect) { bool isScrolling = (Screen.width - rect.xMax) > 5; GUIStyle style = new GUIStyle("MiniToolbarPopup"); style.fontStyle = FontStyle.Bold; rect.x = Screen.width - (isScrolling ? 60 : 45) - EditorGUI.indentLevel * 15; rect.y += 1; rect.width = 24 + EditorGUI.indentLevel * 15; if (!Application.isPlaying && (Event.current.type == EventType.ExecuteCommand || rect.Contains(Event.current.mousePosition))) { List <UnityEngine.Object> validParents = new List <UnityEngine.Object>(); List <Type> layerTypes = new List <Type>(); List <string> options = new List <string> { "..." }; bool machineIsParent = StateMachineUtility.IsParent(layer, machine); foreach (KeyValuePair <Type, List <Type> > pair in StateMachineUtility.LayerTypeStateTypeDict) { PropertyInfo layerProperty = pair.Key.GetProperty("Layer", ReflectionExtensions.AllFlags); PropertyInfo machineProperty = pair.Key.GetProperty("Machine", ReflectionExtensions.AllFlags); if ((machineProperty == null || machineProperty.PropertyType.IsInstanceOfType(machine)) && (layerProperty == null || layerProperty.PropertyType.IsInstanceOfType(layer)) && Array.TrueForAll(existingLayers, existingLayer => existingLayer.GetType() != pair.Key)) { layerTypes.Add(pair.Key); options.Add("Add/" + StateMachineUtility.LayerTypeFormattedDict[pair.Key]); } } if (!machineIsParent) { validParents.Add(machine); options.Add("Move To/Machine"); } for (int i = 0; i < existingLayers.Length; i++) { StateLayer existingLayer = existingLayers[i]; if (layer != existingLayer && !StateMachineUtility.IsParent(layer, existingLayer) && !StateMachineUtility.GetSubLayersRecursive(layer).Contains(existingLayer)) { validParents.Add(existingLayer); options.Add("Move To/" + StateMachineUtility.FormatLayer(existingLayer.GetType())); } } options.Add("Copy"); if (EditorPrefs.GetString("Clipboard Layer Type") == layer.GetType().Name) { options.Add("Paste/Layer"); options.Add("Paste/Layer and States"); options.Add("Paste/Layer and Sublayers"); options.Add("Paste/All"); } options.Add("Generate"); if (Selection.gameObjects.Length <= 1) { // int index = EditorGUI.Popup(rect, layerTypes.IndexOf(selectedLayerType) + 1, options.ToArray(), style) - 1; int index = EditorGUI.Popup(rect, 0, options.ToArray(), style) - 1; string option = index == -1 ? "" : options[index + 1]; if (index < layerTypes.Count) { selectedLayerType = index == -1 ? null : layerTypes[Mathf.Clamp(index, 0, options.Count - 1)]; if (selectedLayerType != null) { StateMachineUtility.AddLayer(machine, selectedLayerType, layer); selectedLayerType = null; } } else if (option.StartsWith("Move To")) { UnityEngine.Object parent = validParents[index - layerTypes.Count]; StateMachineUtility.MoveLayerTo(layer, parent); } else if (option.StartsWith("Copy")) { EditorPrefs.SetString("Clipboard Layer Type", layer.GetType().Name); EditorPrefs.SetInt("Clipboard Layer ID", layer.GetInstanceID()); } else if (option.StartsWith("Paste")) { StateLayer layerToCopy = EditorUtility.InstanceIDToObject(EditorPrefs.GetInt("Clipboard Layer ID")) as StateLayer; if (option == "Paste/Layer") { StateMachineUtility.CopyLayer(layer, layerToCopy, false, false); } else if (option == "Paste/Layer and States") { StateMachineUtility.CopyLayer(layer, layerToCopy, true, false); } else if (option == "Paste/Layer and Sublayers") { StateMachineUtility.CopyLayer(layer, layerToCopy, false, true); } else if (option == "Paste/All") { StateMachineUtility.CopyLayer(layer, layerToCopy, true, true); } EditorPrefs.SetString("Clipboard Layer Type", ""); EditorPrefs.SetInt("Clipboard Layer ID", 0); } else if (option.StartsWith("Generate")) { StateMachineGeneratorWindow generator = StateMachineGeneratorWindow.Create(); string layerTypeName = layer.GetTypeName(); string layerScriptPath = AssetDatabaseUtility.GetAssetPath(layerTypeName + ".cs"); generator.Path = string.IsNullOrEmpty(layerScriptPath) ? generator.Path : Path.GetDirectoryName(layerScriptPath); generator.Machine = StateMachineUtility.FormatMachine(machine); generator.Layer = layerTypeName; generator.Inherit = StateMachineUtility.FormatLayer(layer.GetType().BaseType); generator.SubLayer = layer.Layer == null ? "" : StateMachineUtility.FormatLayer(layer.Layer.GetType()); } } } else { EditorGUI.BeginDisabledGroup(Application.isPlaying); EditorGUI.Popup(rect, 0, new[] { "..." }, style); EditorGUI.EndDisabledGroup(); } }