public static void CreateStateMachine() { FsmEditor.ShowWindow(); StateMachine stateMachine = AssetCreator.CreateAsset <StateMachine> (true); if (stateMachine == null) { return; } stateMachine.color = (int)NodeColor.Blue; stateMachine.Name = stateMachine.name; FsmGameObject gameObject = ScriptableObject.CreateInstance <FsmGameObject> (); gameObject.Name = "Owner"; gameObject.hideFlags = HideFlags.HideInHierarchy; gameObject.IsHidden = true; gameObject.IsShared = true; stateMachine.Variables = ArrayUtility.Add <FsmVariable> (stateMachine.Variables, gameObject); AssetDatabase.AddObjectToAsset(gameObject, stateMachine); AssetDatabase.SaveAssets(); AnyState state = FsmEditorUtility.AddNode <AnyState> (FsmEditor.Center, stateMachine); state.color = (int)NodeColor.Aqua; state.Name = "Any State"; FsmEditor.SelectStateMachine(stateMachine); }
private void SelectStateMachine() { GUIContent content = new GUIContent(FsmEditor.Active != null?FsmEditor.Active.Name:"[None Selected]"); float width = EditorStyles.toolbarDropDown.CalcSize(content).x; width = Mathf.Clamp(width, 100f, width); if (GUILayout.Button(content, EditorStyles.toolbarDropDown, GUILayout.Width(width))) { GenericMenu toolsMenu = new GenericMenu(); if (FsmEditor.ActiveGameObject != null) { foreach (ICodeBehaviour behaviour in FsmEditor.ActiveGameObject.GetComponents <ICodeBehaviour>()) { SelectStateMachineMenu(behaviour.stateMachine, ref toolsMenu); } } else if (FsmEditor.Active != null) { SelectStateMachineMenu(FsmEditor.Active.Root, ref toolsMenu); } toolsMenu.AddItem(new GUIContent("[Create New]"), false, delegate() { StateMachine stateMachine = AssetCreator.CreateAsset <StateMachine> (true); if (stateMachine != null) { stateMachine.Name = stateMachine.name; AnyState state = FsmEditorUtility.AddNode <AnyState> (FsmEditor.Center, stateMachine); state.color = (int)NodeColor.Aqua; state.Name = "Any State"; FsmGameObject gameObject = ScriptableObject.CreateInstance <FsmGameObject> (); gameObject.Name = "Owner"; gameObject.hideFlags = HideFlags.HideInHierarchy; gameObject.IsHidden = true; gameObject.IsShared = true; stateMachine.Variables = ArrayUtility.Add <FsmVariable> (stateMachine.Variables, gameObject); AssetDatabase.AddObjectToAsset(gameObject, stateMachine); AssetDatabase.SaveAssets(); FsmEditor.SelectStateMachine(stateMachine); } }); toolsMenu.ShowAsContext(); } }
private void DoEvents(Event ev, bool isMouse) { if (Validate("showHelp", KeyCode.F1, isMouse)) { PreferencesEditor.ToggleBool(Preference.ShowShortcuts); ev.Use(); } if (Validate("centerView", KeyCode.Tab, isMouse)) { FsmEditor.instance.CenterView(); } if (Validate("selectAll", KeyCode.F2, isMouse)) { FsmEditor.instance.ToggleSelection(); ev.Use(); } if (Validate("createState", KeyCode.F3, isMouse) && FsmEditor.instance != null) { FsmEditorUtility.AddNode <State>(ev.mousePosition, FsmEditor.Active); if (FsmEditor.instance != null) { FsmEditor.instance.Repaint(); } ev.Use(); } if (Validate("actionBrowser", KeyCode.F4, isMouse)) { ActionBrowser.ShowWindow(); ev.Use(); } if (Validate("conditionBrowser", KeyCode.F5, isMouse)) { ConditionBrowser.ShowWindow(); ev.Use(); } }
public static T AddNode <T>(Vector2 position, StateMachine parent) { if (parent == null) { Debug.LogWarning("Can't add node to parent state machine, because the parent state machine is null!"); return(default(T)); } Node node = (Node)ScriptableObject.CreateInstance(typeof(T)); node.hideFlags = HideFlags.HideInHierarchy; node.Name = FsmEditorUtility.GenerateUniqueNodeName <T> (parent.Root); node.Parent = parent; parent.Nodes = ArrayUtility.Add <Node> (parent.Nodes, node); node.position = new Rect(position.x, position.y, FsmEditorStyles.StateWidth, FsmEditorStyles.StateHeight); UpdateNodeColor(node); if (EditorUtility.IsPersistent(parent)) { AssetDatabase.AddObjectToAsset(node, parent); } if (node.GetType() == typeof(StateMachine)) { node.position.width = FsmEditorStyles.StateMachineWidth; node.position.height = FsmEditorStyles.StateMachineHeight; AnyState state = FsmEditorUtility.AddNode <AnyState> (FsmEditor.Center, (StateMachine)node); UpdateNodeColor(state); state.Name = "Any State"; } AssetDatabase.SaveAssets(); return((T)(object)node); }
protected override void CanvasContextMenu () { if (currentEvent.type != EventType.MouseDown || currentEvent.button != 1 || currentEvent.clickCount != 1 || FsmEditor.Active == null){ return; } GenericMenu canvasMenu = new GenericMenu (); canvasMenu.AddItem (FsmContent.createState, false, delegate() { State state= FsmEditorUtility.AddNode<State>(mousePosition,FsmEditor.Active); state.IsStartNode=FsmEditor.Active.GetStartNode() == null; FsmEditorUtility.UpdateNodeColor(state); }); canvasMenu.AddItem (FsmContent.createSubFsm, false, delegate() { StateMachine stateMachine=FsmEditorUtility.AddNode<StateMachine>(mousePosition,FsmEditor.Active); stateMachine.IsStartNode=FsmEditor.Active.GetStartNode() == null; FsmEditorUtility.UpdateNodeColor(stateMachine); }); canvasMenu.AddItem (FsmContent.copy, false, delegate() { Pasteboard.Copy(new List<Node>(){FsmEditor.Active}); }); if (Pasteboard.CanPaste ()) { canvasMenu.AddItem (FsmContent.paste, false, delegate() { Pasteboard.Paste(mousePosition,FsmEditor.Active); }); } canvasMenu.AddSeparator (""); if (Selection.activeGameObject != null) { canvasMenu.AddItem (FsmContent.addToSelection, false, delegate() { foreach(GameObject go in Selection.gameObjects){ ICodeBehaviour behaviour = go.AddComponent<ICodeBehaviour>(); behaviour.stateMachine = FsmEditor.Active.Root; EditorUtility.SetDirty(behaviour); } SelectGameObject(Selection.activeGameObject); }); canvasMenu.AddItem (FsmContent.bindToGameObject, false, delegate() { foreach(GameObject go in Selection.gameObjects){ ICodeBehaviour behaviour = go.AddComponent<ICodeBehaviour>(); behaviour.stateMachine = (StateMachine)FsmUtility.Copy(FsmEditor.Active.Root);//FsmEditor.Active.Root; EditorUtility.SetDirty(behaviour); } SelectGameObject(Selection.activeGameObject); }); } else { canvasMenu.AddDisabledItem(FsmContent.addToSelection); canvasMenu.AddDisabledItem(FsmContent.bindToGameObject); } if (FsmEditor.Active.Root != null && !EditorUtility.IsPersistent(FsmEditor.Active.Root)) { canvasMenu.AddItem (FsmContent.saveAsAsset, false, delegate() { string mPath = EditorUtility.SaveFilePanelInProject ( "Save StateMachine as Asset", "New StateMachine.asset", "asset", ""); if(mPath != null){ StateMachine stateMachine=(StateMachine)FsmUtility.Copy(FsmEditor.Active.Root); AssetDatabase.CreateAsset(stateMachine,mPath); AssetDatabase.SaveAssets(); FsmEditorUtility.ParentChilds(stateMachine); } }); } else { canvasMenu.AddDisabledItem(FsmContent.saveAsAsset); } canvasMenu.ShowAsContext (); }
private void NodeContextMenu () { if (currentEvent.type != EventType.MouseDown || currentEvent.button != 1 || currentEvent.clickCount != 1) { return; } Node node = MouseOverNode (); if (node == null) { return; } GenericMenu nodeMenu = new GenericMenu (); if (Application.isPlaying && this.active != null && this.active.Owner != null && node != this.active.Owner.ActiveNode) { nodeMenu.AddItem (new GUIContent ("Execute"), false, delegate { this.active.Owner.SetNode (node); }); } nodeMenu.AddItem (FsmContent.makeTransition, false, delegate() { fromNode = node; }); if (!node.IsStartNode && !(node is AnyState)) { nodeMenu.AddItem (FsmContent.setAsDefault, false, delegate() { FsmEditorUtility.SetDefaultNode (node, FsmEditor.Active); }); } else { nodeMenu.AddDisabledItem (FsmContent.setAsDefault); } if (node.GetType () == typeof(State)) { State state = node as State; nodeMenu.AddItem (FsmContent.sequence, state.IsSequence, delegate() { state.IsSequence = !state.IsSequence; }); } if (node.GetType () != typeof(AnyState)) { nodeMenu.AddItem (FsmContent.moveToSubStateMachine, false, delegate() { StateMachine stateMachine = FsmEditorUtility.AddNode<StateMachine> (mousePosition, FsmEditor.Active); Pasteboard.Copy (selection); Pasteboard.Paste (mousePosition, stateMachine); foreach (Node mNode in selection) { if (!(mNode is AnyState)) { FsmEditorUtility.DeleteNode (mNode); } } selection.Clear (); UpdateUnitySelection (); EditorUtility.SetDirty (FsmEditor.Active); }); if (FsmEditor.Active.Parent != null) { nodeMenu.AddItem (FsmContent.moveToParentStateMachine, false, delegate() { Pasteboard.Copy (selection); Pasteboard.Paste (mousePosition, FsmEditor.Active.Parent); foreach (Node mNode in selection) { if (!(mNode is AnyState)) { FsmEditorUtility.DeleteNode (mNode); } } selection.Clear (); UpdateUnitySelection (); EditorUtility.SetDirty (FsmEditor.Active); }); } else { nodeMenu.AddDisabledItem (FsmContent.moveToParentStateMachine); } nodeMenu.AddItem (FsmContent.copy, false, delegate() { Pasteboard.Copy (selection); }); nodeMenu.AddItem (FsmContent.delete, false, delegate() { if (selection.Contains (node)) { foreach (Node mNode in selection) { if (!(mNode is AnyState)) { FsmEditorUtility.DeleteNode (mNode); } } selection.Clear (); UpdateUnitySelection (); } else { FsmEditorUtility.DeleteNode (node); } EditorUtility.SetDirty (FsmEditor.Active); }); } else { nodeMenu.AddDisabledItem (FsmContent.copy); nodeMenu.AddDisabledItem (FsmContent.delete); } nodeMenu.ShowAsContext (); Event.current.Use (); }