public static void ApplyGraphChange(GraphOwner go, GameObject instance)
    {
        //--------------------- 同步更新prefab资源
        bool isBound = go.graphIsBound;

        if (!isBound)
        {
            return;
        }

        var prefabType = UnityEditor.PrefabUtility.GetPrefabType(go.gameObject);

        if (prefabType == UnityEditor.PrefabType.PrefabInstance)
        {
            if (!Application.isPlaying)
            {
                GameObject prefab = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(instance);

                List <Graph> allNestGraphs = go.graph.GetAllNestedGraphs <Graph>(true);

                foreach (var v in allNestGraphs)
                {
                    if (!NestedUtility.CheckHasThisAsset(v, prefab))
                    {
                        UnityEditor.AssetDatabase.AddObjectToAsset(v, prefab);
                    }
                }

                UnityEditor.EditorApplication.delayCall +=
                    () => { AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(prefab)); };
                AssetDatabase.Refresh();
            }
        }
    }
Exemple #2
0
    void Spawn()
    {
        //生成物件 改名
        for (int i = 2; i <= 4; i++)
        {
            GameObject go = Instantiate(Sample.gameObject, transform, false);
            go.name = string.Format("P{0}", i);
        }

        for (int i = 0; i < 4; i++)
        {
            string    name  = string.Format("P{0}", i + 1);
            Transform child = transform.Find(name);

            System.Action <Transform> action = (t) =>
            {
                if (t.name == "ChaIcon")
                {
                    Infos[i].Icon          = t.GetComponent <Image>();
                    Infos[i].Icon.material = new Material(Infos[i].Icon.material);
                }
                if (t.name == "ChaName")
                {
                    Infos[i].ChaName = t.GetComponent <InputField>();
                }
                if (t.name == "PlayerName")
                {
                    Infos[i].PlayerName = t.GetComponent <InputField>();
                }
                if (t.name == "Cash")
                {
                    Infos[i].Cash = t.GetComponent <TextAnim>();
                }
                if (t.name == "Bank")
                {
                    Infos[i].Bank = t.GetComponent <TextAnim>();
                }
            };
            NestedUtility.TransformTreeAction(child, action);

            Infos[i].Rect              = child.GetComponent <RectTransform>();
            Infos[i].CanvasGroup       = child.GetComponent <CanvasGroup>();
            Infos[i].CanvasGroup.alpha = 0;
            Infos[i].Anim              = child.GetComponent <Animation>();
        }
    }
        ////////////////////////////////////////
        ///////////GUI AND EDITOR STUFF/////////
        ////////////////////////////////////////
#if UNITY_EDITOR
        protected override void OnNodeGUI()
        {
            GUILayout.Label(string.Format("Sub BT\n{0}", _nestedBT));
            if (nestedBT != null)
            {
            }
            else
            {
                if (!Application.isPlaying && GUILayout.Button("CREATE BOUND BehaviourTree"))
                {
                    NestedUtility.CreateBoundNested <BehaviourTree>(this, graph);
                    nestedBT.name = name;
                }
                if (!Application.isPlaying && GUILayout.Button("CREATE ASSET BehaviourTree"))
                {
                    Node.CreateNested <BehaviourTree>(this);
                }
            }
        }
Exemple #4
0
        ////////////////////////////
        //////EDITOR AND GUI////////
        ////////////////////////////
#if UNITY_EDITOR
        protected override void OnNodeGUI()
        {
            GUILayout.Label(string.Format("SubFSM\n{0}", _nestedFSM));
            if (nestedFSM != null)
            {
            }
            else
            {
                if (!Application.isPlaying && GUILayout.Button("CREATE BOUND FSM"))
                {
                    NestedUtility.CreateBoundNested <FSM>(this, graph);
                    nestedFSM.name = name;
                }
                if (!Application.isPlaying && GUILayout.Button("CREATE ASSET FSM"))
                {
                    Node.CreateNested <FSM>(this);
                }
            }
        }
        ////////////////////////////////////////
        ///////////GUI AND EDITOR STUFF/////////
        ////////////////////////////////////////
#if UNITY_EDITOR
        protected override void OnNodeGUI()
        {
            GUILayout.Label(_flowScript.ToString());

            if (flowScript != null)
            {
            }
            else
            {
                if (!Application.isPlaying && GUILayout.Button("CREATE BOUND FlowScirpt"))
                {
                    NestedUtility.CreateBoundNested <FlowScript>(this, graph);
                    flowScript.name = name;
                    CreateDefaultNode();
                }
                if (!Application.isPlaying && GUILayout.Button("CREATE ASSET FlowScirpt"))
                {
                    Node.CreateNested <FlowScript>(this);
                    CreateDefaultNode();
                }
            }
        }
Exemple #6
0
        ////////////////////////////////////////
        ///////////GUI AND EDITOR STUFF/////////
        ////////////////////////////////////////
#if UNITY_EDITOR
        protected override void OnNodeGUI()
        {
            GUILayout.Label(string.Format("Sub FlowScript\n{0}", _nestedFC));
            if (nestedFC != null)
            {
            }
            else
            {
                if (!Application.isPlaying && GUILayout.Button("CREATE BOUND FlowScript"))
                {
                    NestedUtility.CreateBoundNested <FlowScript>(this, graph);
                    GatherPorts();
                    nestedFC.name = name;
                }
                if (!Application.isPlaying && GUILayout.Button("CREATE ASSET FlowScript"))
                {
                    GatherPorts();
                    CreateNested <FlowScript>(this);
                }
            }
            base.OnNodeGUI();
        }
    public static void ApplyGraphChange(GraphOwner go, GameObject instance)
    {
        //--------------------- 同步更新prefab资源
        bool isBound = go.graphIsBound;

        if (!isBound)
        {
            return;
        }

#if UNITY_2018_3_OR_NEWER
        var prefabType = UnityEditor.PrefabUtility.GetPrefabInstanceStatus(go.gameObject);
        //Debug.Log("start to add child graph");
        if (prefabType != UnityEditor.PrefabInstanceStatus.MissingAsset || prefabType != UnityEditor.PrefabInstanceStatus.NotAPrefab)
        {
            //Debug.Log("check prefab type");
            if (!Application.isPlaying)
            {
                GameObject prefab = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(instance);
                //Debug.Log("Prefab name:" + prefab.name);
                List <Graph> allNestGraphs = new List <Graph>();
                allNestGraphs.Add(go.graph);
                allNestGraphs = go.graph.GetAllNestedGraphs <Graph>(true);

                foreach (var v in allNestGraphs)
                {
                    if (!NestedUtility.CheckHasThisAsset(v, prefab))
                    {
                        //Debug.Log(" add child graph");
                        UnityEditor.AssetDatabase.AddObjectToAsset(v, prefab);
                        AssetDatabase.SaveAssets();
                        EditorUtility.SetDirty(go);
                    }
                }

                UnityEditor.EditorApplication.delayCall += () => { UnityEditor.AssetDatabase.ImportAsset(UnityEditor.AssetDatabase.GetAssetPath(prefab)); };
            }
        }
#else
        var prefabType = UnityEditor.PrefabUtility.GetPrefabType(go.gameObject);

        if (prefabType == UnityEditor.PrefabType.PrefabInstance)
        {
            if (!Application.isPlaying)
            {
                GameObject prefab = (GameObject)PrefabUtility.GetPrefabParent(instance);

                List <Graph> allNestGraphs = go.graph.GetAllNestedGraphs <Graph>(true);

                foreach (var v in allNestGraphs)
                {
                    if (!NestedUtility.CheckHasThisAsset(v, prefab))
                    {
                        UnityEditor.AssetDatabase.AddObjectToAsset(v, prefab);
                    }
                }

                UnityEditor.EditorApplication.delayCall +=
                    () => { AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(prefab)); };
                AssetDatabase.Refresh();
            }
        }
#endif
    }
        ///----------------------------------------------------------------------------------------------

        ///Graph events AFTER nodes
        static void HandlePostNodesGraphEvents(Graph graph, Vector2 canvasMousePos)
        {
            //Shortcuts
            if (GUIUtility.keyboardControl == 0)
            {
                //Copy/Cut/Paste
                if (e.type == EventType.ValidateCommand || e.type == EventType.Used)
                {
                    if (e.commandName == "Copy" || e.commandName == "Cut")
                    {
                        List <Node> selection = null;
                        if (GraphEditorUtility.activeNode != null)
                        {
                            selection = new List <Node> {
                                GraphEditorUtility.activeNode
                            };
                        }
                        if (GraphEditorUtility.activeElements != null && GraphEditorUtility.activeElements.Count > 0)
                        {
                            selection = GraphEditorUtility.activeElements.Cast <Node>().ToList();
                        }
                        if (selection != null)
                        {
                            CopyBuffer.Set <Node[]>(Graph.CloneNodes(selection).ToArray());
                            if (e.commandName == "Cut")
                            {
                                foreach (Node node in selection)
                                {
                                    graph.RemoveNode(node);
                                }
                            }
                        }
                        e.Use();
                    }
                    if (e.commandName == "Paste")
                    {
                        if (CopyBuffer.Has <Node[]>())
                        {
                            TryPasteNodesInGraph(graph, CopyBuffer.Get <Node[]>(), canvasMousePos + new Vector2(500, 500) / graph.zoomFactor);
                        }
                        e.Use();
                    }
                }

                if (e.type == EventType.KeyUp)
                {
                    //Delete
                    if (e.keyCode == KeyCode.Delete || e.keyCode == KeyCode.Backspace)
                    {
                        if (GraphEditorUtility.activeElements != null && GraphEditorUtility.activeElements.Count > 0)
                        {
                            foreach (var obj in GraphEditorUtility.activeElements.ToArray())
                            {
                                if (obj is Node)
                                {
                                    graph.RemoveNode(obj as Node);
                                }
                                if (obj is Connection)
                                {
                                    graph.RemoveConnection(obj as Connection);
                                }
                            }
                            GraphEditorUtility.activeElements = null;
                        }

                        if (GraphEditorUtility.activeNode != null)
                        {
                            graph.RemoveNode(GraphEditorUtility.activeNode);
                            GraphEditorUtility.activeElement = null;
                        }

                        if (GraphEditorUtility.activeConnection != null)
                        {
                            graph.RemoveConnection(GraphEditorUtility.activeConnection);
                            GraphEditorUtility.activeElement = null;
                        }
                        e.Use();
                    }

                    //Duplicate
                    if (e.keyCode == KeyCode.D && e.control && !e.alt)
                    {
                        if (GraphEditorUtility.activeElements != null && GraphEditorUtility.activeElements.Count > 0)
                        {
                            TryPasteNodesInGraph(graph, GraphEditorUtility.activeElements.OfType <Node>().ToArray(), default(Vector2));
                        }
                        if (GraphEditorUtility.activeNode != null)
                        {
                            GraphEditorUtility.activeElement = GraphEditorUtility.activeNode.Duplicate(graph);
                        }
                        //Connections can't be duplicated by themselves. They do so as part of multiple node duplication.
                        e.Use();
                    }
                }
            }

            //No panel is obscuring
            if (GraphEditorUtility.allowClick)
            {
                #region 自定义快捷键

                if ((currentGraph.GetType() == typeof(FlowGraph) || currentGraph.GetType().IsSubclassOf(typeof(FlowGraph))) && GUIUtility.keyboardControl == 0)
                {
                    if (e.keyCode == KeyCode.F && e.control && !e.alt)
                    {
                        //Debug.Log("ctrl +f ");

                        FindNode toolNode = currentGraph.GetAllNodesOfType <FindNode>().FirstOrDefault();
                        if (toolNode != null)
                        {
                            NodeCanvas.Editor.GraphEditorUtility.activeElement = toolNode;
                        }
                        else
                        {
                            FindNode newNode = currentGraph.AddNode <FindNode>(ViewToCanvas(e.mousePosition));

                            NodeCanvas.Editor.GraphEditorUtility.activeElement = newNode;
                            //Debug.Log("There is no Find Node,Create a new node!");
                        }
                        //e.Use();
                    }


                    if (e.keyCode == KeyCode.A && e.control && !e.alt && !e.shift)
                    {
                        NodeCanvas.Editor.GraphEditorUtility.activeElements = null;
                        NodeCanvas.Editor.GraphEditorUtility.activeElement  = null;

                        if (currentGraph.allNodes == null)
                        {
                            return;
                        }

                        if (currentGraph.allNodes.Count > 1)
                        {
                            NodeCanvas.Editor.GraphEditorUtility.activeElements = currentGraph.allNodes.Cast <object>().ToList();
                            //e.Use();
                            return;
                        }
                        else
                        {
                            NodeCanvas.Editor.GraphEditorUtility.activeElement = currentGraph.allNodes[0];
                            //e.Use();
                        }
                    }
                    if (e.keyCode == KeyCode.C && e.type == EventType.KeyUp && !e.control && !e.alt && !e.shift && GUIUtility.keyboardControl == 0)
                    {
                        if (GraphEditorUtility.activeElement != null ||
                            GraphEditorUtility.activeElements != null && GraphEditorUtility.activeElements.Count > 0)
                        {
                            List <Node> node = new List <Node>();
                            if (GraphEditorUtility.activeElement != null)
                            {
                                node.Add((Node)GraphEditorUtility.activeElement);
                            }
                            else
                            {
                                node = GraphEditorUtility.activeElements.Cast <Node>().ToList();
                            }

                            Undo.RegisterCompleteObjectUndo(currentGraph, "Create Group");
                            if (currentGraph.canvasGroups == null)
                            {
                                currentGraph.canvasGroups = new List <Framework.CanvasGroup>();
                            }
                            Framework.CanvasGroup group = new Framework.CanvasGroup(GetNodeBounds(node).ExpandBy(100f), "New Canvas Group");
                            currentGraph.canvasGroups.Add(group);
                            group.isRenaming = true;
                        }
                    }
                    if (e.keyCode == KeyCode.G && !e.control && !e.alt && e.shift && GUIUtility.keyboardControl == 0)
                    {
                        if (NodeCanvas.Editor.GraphEditorUtility.activeElements == null ||
                            NodeCanvas.Editor.GraphEditorUtility.activeElements.Count < 2 && NodeCanvas.Editor.GraphEditorUtility.activeElement != null &&
                            NodeCanvas.Editor.GraphEditorUtility.activeElement.GetType() == typeof(MacroNodeWrapper))
                        {
                            //Debug.Log("current select is group node");
                            MacroNodeWrapper n = (MacroNodeWrapper)NodeCanvas.Editor.GraphEditorUtility.activeElement;

                            if (n.macro.allNodes.Count <= 2)
                            {
                                return;
                            }
                            List <Node> childNode = new List <Node>();

                            foreach (var c in n.macro.allNodes)
                            {
                                if (c.GetType() != typeof(MacroInputNode) && c.GetType() != typeof(MacroOutputNode))
                                {
                                    childNode.Add(c);
                                }
                            }

                            var newNodes = Graph.CopyNodesToGraph(childNode, currentGraph);

                            currentGraph.RemoveNode(n);
                            NodeCanvas.Editor.GraphEditorUtility.activeElement = null;

                            if (NodeCanvas.Editor.GraphEditorUtility.activeElements != null)
                            {
                                NodeCanvas.Editor.GraphEditorUtility.activeElements = newNodes.Cast <object>().ToList();
                            }
                        }
                    }

                    if (e.keyCode == KeyCode.G && e.control && !e.alt && !e.shift && GUIUtility.keyboardControl == 0)
                    {
                        //Debug.Log("ctrl +g ");
                        if (NodeCanvas.Editor.GraphEditorUtility.activeElements == null || NodeCanvas.Editor.GraphEditorUtility.activeElements.Count < 1)
                        {
                            //e.Use();
                            return;
                        }

                        List <Node> ns            = NodeCanvas.Editor.GraphEditorUtility.activeElements.Cast <Node>().ToList();
                        Rect        groupNodeRect = GetNodeBounds(ns, viewRect, false);
                        var         wrapper       = currentGraph.AddNode <MacroNodeWrapper>(groupNodeRect.center);
                        wrapper.macro              = NestedUtility.CreateBoundNested <GroupMacro>(wrapper, currentGraph);
                        wrapper.macro.name         = "NewGroup";
                        wrapper.macro.CategoryPath = (currentGraph.agent != null ? currentGraph.agent.name : currentGraph.name) + "/";
                        wrapper.macro.agent        = currentGraph.agent;

                        wrapper.macro.entry.position = new Vector2(groupNodeRect.x - 200f,
                                                                   groupNodeRect.y + 0.5f * groupNodeRect.height);
                        wrapper.macro.exit.position = new Vector2(groupNodeRect.x + groupNodeRect.width + 100f,
                                                                  groupNodeRect.y + 0.5f * groupNodeRect.height);

                        wrapper.macro.translation = -groupNodeRect.center + new Vector2(500f, 500f);

                        Graph.CopyNodesToGraph(NodeCanvas.Editor.GraphEditorUtility.activeElements.OfType <Node>().ToList(), wrapper.macro);

                        foreach (var c in ns)
                        {
                            currentGraph.RemoveNode(c);
                        }
                        NodeCanvas.Editor.GraphEditorUtility.activeElements.Clear();
                        NodeCanvas.Editor.GraphEditorUtility.activeElement = wrapper;
                    }
                    if (e.type == EventType.KeyDown && e.keyCode == KeyCode.I && GUIUtility.keyboardControl == 0)
                    {
                        List <Node> allNodes = currentGraph.GetAllNodesOfType <Node>();
                        if (allNodes != null && allNodes.Count < 2)
                        {
                            return;
                        }

                        if (NodeCanvas.Editor.GraphEditorUtility.activeElement != null)
                        {
                            if (allNodes.Contains((Node)NodeCanvas.Editor.GraphEditorUtility.activeElement))
                            {
                                allNodes.Remove((Node)NodeCanvas.Editor.GraphEditorUtility.activeElement);
                                NodeCanvas.Editor.GraphEditorUtility.activeElement = null;
                                NodeCanvas.Editor.GraphEditorUtility.activeElements.Clear();
                                NodeCanvas.Editor.GraphEditorUtility.activeElements = allNodes.Cast <object>().ToList();
                            }
                            return;
                        }

                        if (NodeCanvas.Editor.GraphEditorUtility.activeElements != null && NodeCanvas.Editor.GraphEditorUtility.activeElements.Count > 1)
                        {
                            List <object> invertNodeList = NodeCanvas.Editor.GraphEditorUtility.activeElements;
                            invertNodeList.ForEach(x =>
                            {
                                if (allNodes.Contains((Node)x))
                                {
                                    allNodes.Remove((Node)x);
                                }
                            });
                            NodeCanvas.Editor.GraphEditorUtility.activeElements.Clear();
                            if (allNodes.Count == 0)
                            {
                                NodeCanvas.Editor.GraphEditorUtility.activeElement = null;
                            }
                            else if (allNodes.Count == 1)
                            {
                                NodeCanvas.Editor.GraphEditorUtility.activeElement = allNodes[0];
                            }
                            else
                            {
                                NodeCanvas.Editor.GraphEditorUtility.activeElements = allNodes.Cast <object>().ToList();
                            }

                            return;
                        }
                        e.Use();
                    }
                    if (e.type == EventType.KeyUp && e.alt && e.control)
                    {
                        //Type genericType;

                        UnityEditor.GenericMenu.MenuFunction2 S = (obj) =>
                        {
                            NodeCanvas.Editor.GraphEditorUtility.activeElement = GraphEditor.currentGraph.AddNode(obj.GetType(),
                                                                                                                  ViewToCanvas(e.mousePosition));
                        };

                        UnityEditor.GenericMenu.MenuFunction2 D = (obj) =>
                        {
                            var genericT = typeof(SimplexNodeWrapper <>).MakeGenericType(obj.GetType());
                            NodeCanvas.Editor.GraphEditorUtility.activeElement = GraphEditor.currentGraph.AddNode(genericT, ViewToCanvas(e.mousePosition));
                        };


                        if (e.keyCode == KeyCode.S)
                        {
                            var menu = new UnityEditor.GenericMenu();

                            menu.AddItem(new GUIContent("Split"), false, S, new Split());
                            menu.AddItem(new GUIContent("Sequence"), false, S, new Sequence());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("SetActive"), false, D, new S_Active());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("SendEvent"), false, D, new SendEvent());
                            menu.AddItem(new GUIContent("SendEvent<T>"), false, D, new SendEvent <object>());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("Self"), false, S, new OwnerVariable());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("SwitchByIndex"), false, S, new Switch <object>());
                            menu.AddItem(new GUIContent("Switch Value"), false, D, new SwitchValue <object>());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("SwitchFlow/Switch Bool"), false, S, new SwitchBool());
                            menu.AddItem(new GUIContent("SwitchFlow/Switch String"), false, S, new SwitchString());
                            menu.AddItem(new GUIContent("SwitchFlow/Switch Int"), false, S, new SwitchInt());
                            menu.AddItem(new GUIContent("SwitchFlow/Switch Enum"), false, S, new SwitchEnum());
                            menu.AddItem(new GUIContent("SwitchFlow/Switch Tag"), false, S, new SwitchTag());
                            menu.AddItem(new GUIContent("SwitchFlow/Switch Comparison"), false, S, new SwitchComparison());

                            menu.ShowAsContext();
                            //e.Use();
                            return;
                        }

                        if (e.keyCode == KeyCode.A)
                        {
                            var menu = new UnityEditor.GenericMenu();

                            menu.AddItem(new GUIContent("Awake"), false, S, new ConstructionEvent());
                            menu.AddItem(new GUIContent("OnEnable"), false, S, new EnableEvent());
                            menu.AddItem(new GUIContent("Start"), false, S, new StartEvent());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("Update"), false, S, new UpdateEvent());
                            menu.AddItem(new GUIContent("FixedUpdate"), false, S, new FixedUpdateEvent());
                            menu.AddItem(new GUIContent("LateUpdate"), false, S, new LateUpdateEvent());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("OnDisable"), false, S, new DisableEvent());
                            menu.ShowAsContext();
                            return;
                        }
                        if (e.keyCode == KeyCode.C)
                        {
                            var menu = new UnityEditor.GenericMenu();
                            menu.AddItem(new GUIContent("Cache"), false, D, new Cache <object>());

                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("Coroutine"), false, S, new CoroutineState());
                            menu.AddItem(new GUIContent("Cooldown"), false, S, new Cooldown());
                            menu.AddItem(new GUIContent("Chance"), false, S, new Chance());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("CustomEvent"), false, S, new CustomEvent());
                            menu.AddItem(new GUIContent("CustomEvent<T>"), false, S, new CustomEvent <object>());

                            menu.ShowAsContext();
                            return;
                        }

                        if (e.keyCode == KeyCode.E)
                        {
                            var menu = new UnityEditor.GenericMenu();
                            menu.AddItem(new GUIContent("KeyBoard"), false, S, new KeyboardEvents());
                            menu.AddItem(new GUIContent("UnityEventAutoCallbackEvent"), false, S, new UnityEventAutoCallbackEvent());
                            menu.AddItem(new GUIContent("CSharpAutoCallbackEvent"), false, S, new CSharpAutoCallbackEvent());
                            menu.AddItem(new GUIContent("DelegateCallbackEvent"), false, S, new DelegateCallbackEvent());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("UnityEventCallbackEvent"), false, S, new UnityEventCallbackEvent());
                            menu.AddItem(new GUIContent("CSharpEventCallback"), false, S, new CSharpEventCallback());
                            menu.ShowAsContext();
                            return;
                        }
                        if (e.keyCode == KeyCode.D)
                        {
                            var menu = new UnityEditor.GenericMenu();

                            menu.AddItem(new GUIContent("Debug log OnScreen"), false, S, new G_LogOnScreen());
                            menu.AddItem(new GUIContent("Debug log"), false, D, new LogText());
                            menu.AddItem(new GUIContent("Debug Event"), false, D, new DebugEvent());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("DummyFlow"), false, S, new Dummy());
                            menu.AddItem(new GUIContent("DummyValue"), false, D, new Identity <object>());

                            menu.AddItem(new GUIContent("DoOnce"), false, S, new DoOnce());
                            menu.ShowAsContext();
                            e.Use();
                        }

                        if (e.keyCode == KeyCode.R)
                        {
                            var menu = new UnityEditor.GenericMenu();

                            menu.AddItem(new GUIContent("RelayFlowInput"), false, S, new RelayFlowInput());
                            menu.AddItem(new GUIContent("RelayFlowOutput"), false, S, new RelayFlowOutput());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("RelayValueInput"), false, S, new RelayValueInput <object>());
                            menu.AddItem(new GUIContent("RelayValueOutput"), false, S, new RelayValueOutput <object>());

                            menu.ShowAsContext();
                            e.Use();
                        }

                        if (e.keyCode == KeyCode.W)
                        {
                            var menu = new UnityEditor.GenericMenu();

                            menu.AddItem(new GUIContent("Wait"), false, D, new Wait());
                            menu.AddItem(new GUIContent("While"), false, S, new While());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("WaitForOneFrame"), false, D, new WaitForOneFrame());
                            menu.AddItem(new GUIContent("WaitForEndOfFrame"), false, D, new FlowCanvas.Nodes.WaitForEndOfFrame());
                            menu.AddItem(new GUIContent("WaitForFixedUpdate"), false, D, new WaitForFixedUpdate());
                            menu.AddItem(new GUIContent("WaitForPhysicsFrame"), false, D, new WaitForPhysicsFrame());
                            menu.ShowAsContext();
                            e.Use();
                        }

                        if (e.keyCode == KeyCode.F)
                        {
                            Selection.activeGameObject = null;  //防止快捷键冲突
                            var menu = new UnityEditor.GenericMenu();
                            menu.AddItem(new GUIContent("Find"), false, D, new G_FindGameObject());
                            menu.AddItem(new GUIContent("FindChild"), false, D, new G_FindChild());
                            menu.AddItem(new GUIContent("FindGameObjectWithTag"), false, D, new G_FindGameObjectWithTag());
                            menu.AddItem(new GUIContent("FindGameObjectsWithTag"), false, D, new G_FindGameObjectsWithTag());
                            menu.AddItem(new GUIContent("FindObjectOfType"), false, D, new FindObjectOfType());
                            menu.AddItem(new GUIContent("FindObjectsOfType"), false, D, new FindObjectsOfType());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("Finish"), false, S, new Finish());
                            menu.AddItem(new GUIContent("CustomFunction"), false, S, new CustomFunctionEvent());
                            menu.AddItem(new GUIContent("CallCustomFunction"), false, S, new CustomFunctionCall());
                            menu.AddItem(new GUIContent("CallCustomFunction(CustomPort)"), false, S, new FunctionCall());
                            menu.AddItem(new GUIContent("FunctionCustomReturn"), false, S, new Return());

                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("ForLoop"), false, S, new ForLoop());
                            menu.AddItem(new GUIContent("ForEach"), false, S, new ForEach <float>());
                            menu.AddItem(new GUIContent("FlipFlop"), false, S, new FlipFlop());

                            menu.ShowAsContext();
                            e.Use();
                        }

                        if (e.keyCode == KeyCode.G)
                        {
                            var menu = new UnityEditor.GenericMenu();
                            menu.AddItem(new GUIContent("GetName"), false, D, new G_Name());
                            menu.AddItem(new GUIContent("GetAcive"), false, D, new G_Active());
                            menu.AddItem(new GUIContent("GetPosition"), false, D, new G_Position());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("GetChildByIndex"), false, D, new G_Child());
                            menu.AddItem(new GUIContent("GetChildCount"), false, D, new G_ChildCount());
                            menu.AddSeparator("");
                            menu.AddItem(new GUIContent("GetComponentByType"), false, D, new G_Component());
                            menu.AddItem(new GUIContent("GetComponentsByType"), false, D, new G_Components());
                            menu.AddItem(new GUIContent("GetComponentByName"), false, D, new G_ComponentByTypeName());
                            menu.AddItem(new GUIContent("GetComponentsInChildren"), false, D, new G_ComponentsInChildren());
                            menu.AddItem(new GUIContent("GetComponentInChildren"), false, D, new G_ComponentInChildren());
                            menu.AddItem(new GUIContent("GetComponentInParent"), false, D, new G_ComponentInParent());

                            menu.ShowAsContext();
                            e.Use();
                        }
                    }

                    if (e.type == EventType.KeyUp && e.alt)
                    {
                        Type genericType;

                        switch (e.keyCode)
                        {
                        case KeyCode.Alpha1:
                            genericType = typeof(GetVariable <>).MakeGenericType(typeof(float));
                            break;

                        case KeyCode.Alpha2:
                            genericType = typeof(GetVariable <>).MakeGenericType(typeof(Vector2));
                            break;

                        case KeyCode.Alpha3:
                            genericType = typeof(GetVariable <>).MakeGenericType(typeof(Vector3));
                            break;

                        case KeyCode.Alpha4:
                            genericType = typeof(GetVariable <>).MakeGenericType(typeof(Quaternion));
                            break;

                        case KeyCode.Alpha5:
                            genericType = typeof(GetVariable <>).MakeGenericType(typeof(Color));
                            break;

                        default:
                            //genericType = typeof(GetVariable<>).MakeGenericType(typeof(float));
                            return;
                            //break;
                        }

                        var varN = (FlowNode)GraphEditor.currentGraph.AddNode(genericType);
                        varN.position = ViewToCanvas(e.mousePosition - varN.rect.center * 0.5f);

                        NodeCanvas.Editor.GraphEditorUtility.activeElement = varN;

                        e.Use();
                    }
                }
                if (e.type == EventType.KeyDown && e.keyCode == KeyCode.F && GUIUtility.keyboardControl == 0)
                {
                    FocusSelection();
                }

                #endregion


                //'Tilt' or 'Space' keys, opens up the complete context menu browser
                if (e.type == EventType.KeyDown && !e.shift && (e.keyCode == KeyCode.BackQuote || e.keyCode == KeyCode.Space))
                {
                    GenericMenuBrowser.Show(GetAddNodeMenu(graph, canvasMousePos), e.mousePosition, string.Format("Add {0} Node", graph.GetType().FriendlyName()), graph.baseNodeType);
                    e.Use();
                }

                //Right click canvas context menu. Basicaly for adding new nodes.
                if (e.type == EventType.ContextClick)
                {
                    var menu = GetAddNodeMenu(graph, canvasMousePos);
                    if (CopyBuffer.Has <Node[]>() && CopyBuffer.Peek <Node[]>()[0].GetType().IsSubclassOf(graph.baseNodeType))
                    {
                        menu.AddSeparator("/");
                        var copiedNodes = CopyBuffer.Get <Node[]>();
                        if (copiedNodes.Length == 1)
                        {
                            menu.AddItem(new GUIContent(string.Format("Paste Node ({0})", copiedNodes[0].GetType().FriendlyName())), false, () => { TryPasteNodesInGraph(graph, copiedNodes, canvasMousePos); });
                        }
                        else if (copiedNodes.Length > 1)
                        {
                            menu.AddItem(new GUIContent(string.Format("Paste Nodes ({0})", copiedNodes.Length.ToString())), false, () => { TryPasteNodesInGraph(graph, copiedNodes, canvasMousePos); });
                        }
                    }

                    if (NCPrefs.useBrowser)
                    {
                        menu.ShowAsBrowser(e.mousePosition, string.Format("Add {0} Node", graph.GetType().FriendlyName()), graph.baseNodeType);
                    }
                    else
                    {
                        menu.ShowAsContext();
                    }
                    e.Use();
                }
            }
        }
        ///----------------------------------------------------------------------------------------------

        //EDIT MENU
        static GenericMenu GetToolbarMenu_Edit(Graph graph, GraphOwner owner)
        {
            var menu = new GenericMenu();

            //Bind
            if (!Application.isPlaying && owner != null && !owner.graphIsBound)
            {
                menu.AddItem(new GUIContent("Bind To Owner"), false, () =>
                {
                    if (EditorUtility.DisplayDialog("Bind Graph", "This will make a local copy of the graph, bound to the owner.\n\nThis allows you to make local changes and assign scene object references directly.\n\nNote that you can also use scene object references through the use of Blackboard Variables.\n\nBind Graph?", "YES", "NO"))
                    {
                        Undo.RecordObject(owner, "New Local Graph");
                        owner.SetBoundGraphReference(owner.graph);
                        EditorUtility.SetDirty(owner);
                    }
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Bind To Owner"));
            }

            //Save to asset
            if (owner != null && owner.graphIsBound)
            {
                menu.AddItem(new GUIContent("Save To Asset"), false, () =>
                {
                    var newGraph = (Graph)EditorUtils.CreateAsset(graph.GetType());
                    if (newGraph != null)
                    {
                        EditorUtility.CopySerialized(graph, newGraph);
                        newGraph.Validate();
                        AssetDatabase.SaveAssets();

                        //SL--------------
                        NestedUtility.SaveToAsset(graph, newGraph);
                    }
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Save To Asset"));
            }

            //Create defined vars
            if (graph.blackboard != null)
            {
                menu.AddItem(new GUIContent("Promote Defined Parameters To Variables"), false, () =>
                {
                    if (EditorUtility.DisplayDialog("Promote Defined Parameters", "This will fill the current Blackboard with a Variable for each defined Parameter in the graph.\nContinue?", "YES", "NO"))
                    {
                        graph.PromoteDefinedParametersToVariables(graph.blackboard);
                    }
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Promote Defined Parameters To Variables"));
            }

            return(menu);
        }
        //This is called outside Begin/End Windows from GraphEditor.
        public static void ShowToolbar(Graph graph)
        {
            var owner = graph.agent != null && graph.agent is GraphOwner && (graph.agent as GraphOwner).graph == graph ? (GraphOwner)graph.agent : null;

            GUILayout.BeginHorizontal(EditorStyles.toolbar);
            GUI.backgroundColor = Color.white.WithAlpha(0.5f);

            ///----------------------------------------------------------------------------------------------
            ///Left side
            ///----------------------------------------------------------------------------------------------

            if (GUILayout.Button("File", EditorStyles.toolbarDropDown, GUILayout.Width(50)))
            {
                GetToolbarMenu_File(graph, owner).ShowAsContext();
            }

            if (GUILayout.Button("Edit", EditorStyles.toolbarDropDown, GUILayout.Width(50)))
            {
                GetToolbarMenu_Edit(graph, owner).ShowAsContext();
            }

            if (GUILayout.Button("Prefs", EditorStyles.toolbarDropDown, GUILayout.Width(50)))
            {
                GetToolbarMenu_Prefs(graph, owner).ShowAsContext();
            }

            // var customMenu = GetToolbarMenu_Custom(graph, owner);
            // if ( customMenu.GetItemCount() > 0 ) {
            //     if ( GUILayout.Button("More", EditorStyles.toolbarDropDown, GUILayout.Width(50)) ) {
            //         customMenu.ShowAsContext();
            //     }
            // }

            GUILayout.Space(10);

            if (owner != null && GUILayout.Button("Select Owner", EditorStyles.toolbarButton, GUILayout.Width(80)))
            {
                Selection.activeObject = owner;
                EditorGUIUtility.PingObject(owner);
            }

            if (EditorUtility.IsPersistent(graph) && GUILayout.Button("Select Graph", EditorStyles.toolbarButton, GUILayout.Width(80)))
            {
                Selection.activeObject = graph;
                EditorGUIUtility.PingObject(graph);
            }

            GUILayout.Space(10);

            if (GUILayout.Button(new GUIContent(StyleSheet.log, "Open Graph Console"), EditorStyles.toolbarButton, GUILayout.MaxHeight(12)))
            {
                GraphConsole.ShowWindow();
            }

            if (GUILayout.Button(new GUIContent(StyleSheet.lens, "Open Graph Finder"), EditorStyles.toolbarButton, GUILayout.MaxHeight(12)))
            {
                GraphFinder.ShowWindow();
            }

            GUILayout.Space(10);

            ///----------------------------------------------------------------------------------------------

            graph.CallbackOnGraphEditorToolbar();

            ///----------------------------------------------------------------------------------------------
            ///Mid
            ///----------------------------------------------------------------------------------------------

            GUILayout.Space(10);
            GUILayout.FlexibleSpace();

            //...

            //...

            GUILayout.FlexibleSpace();
            GUILayout.Space(10);

            //SL-------------
            //--------------------
            if (GUILayout.Button("ClearUselessNestedAssets", EditorStyles.toolbarButton, GUILayout.Width(175)))
            {
                if (EditorUtility.DisplayDialog("Clear Useless Nested Assets", "清理当前prefab(包括其子物体)或graph资源中无用的嵌套资源,\n\n(需选中物体的是资源目录下的prefab或主Graph Asset).\n\n注意:prefab实例中的嵌套的存盘资源,如果没有被prefab本体引用,同样也被清除 ", "YES", "NO!"))
                {
                    NestedUtility.DeleteAllUselessBoundAsset(graph);
                    e.Use();
                    return;
                }
            }


            ///----------------------------------------------------------------------------------------------
            ///Right side
            ///----------------------------------------------------------------------------------------------

            GUI.backgroundColor = Color.clear;
            GUI.color           = new Color(1, 1, 1, 0.3f);
            GUILayout.Label(string.Format("{0} @NodeCanvas Framework v{1}", graph.GetType().Name, NodeCanvas.Framework.Internal.GraphSerializationData.FRAMEWORK_VERSION), EditorStyles.toolbarButton);
            GUILayout.Space(10);
            GUI.color           = Color.white;
            GUI.backgroundColor = Color.white;

            //GRAPHOWNER JUMP SELECTION
            if (owner != null)
            {
                if (GUILayout.Button(string.Format("[{0}]", owner.gameObject.name), EditorStyles.toolbarDropDown, GUILayout.Width(120)))
                {
                    var menu = new GenericMenu();
                    foreach (var _o in Object.FindObjectsOfType <GraphOwner>().OrderBy(x => x.gameObject != owner.gameObject))
                    {
                        var o = _o;
                        menu.AddItem(new GUIContent(o.gameObject.name + "/" + o.GetType().Name), o == owner, () => { SetReferences(o); });
                    }
                    menu.ShowAsContext();
                }
            }

            Prefs.isEditorLocked = GUILayout.Toggle(Prefs.isEditorLocked, "Lock", EditorStyles.toolbarButton);
            GUILayout.EndHorizontal();
            GUI.backgroundColor = Color.white;
            GUI.color           = Color.white;
        }
Exemple #11
0
        //This is called outside Begin/End Windows from GraphEditor.
        public static void ShowToolbar(Graph graph)
        {
            var owner = graph.agent != null && graph.agent is GraphOwner && (graph.agent as GraphOwner).graph == graph? (GraphOwner)graph.agent : null;

            GUILayout.BeginHorizontal(EditorStyles.toolbar);
            GUI.backgroundColor = new Color(1f, 1f, 1f, 0.5f);

            ///----------------------------------------------------------------------------------------------
            ///Left side
            ///----------------------------------------------------------------------------------------------

            if (GUILayout.Button("File", EditorStyles.toolbarDropDown, GUILayout.Width(50)))
            {
                GetToolbarMenu_File(graph, owner).ShowAsContext();
            }

            if (GUILayout.Button("Edit", EditorStyles.toolbarDropDown, GUILayout.Width(50)))
            {
                GetToolbarMenu_Edit(graph, owner).ShowAsContext();
            }

            if (GUILayout.Button("Prefs", EditorStyles.toolbarDropDown, GUILayout.Width(50)))
            {
                GetToolbarMenu_Prefs(graph, owner).ShowAsContext();
            }

            GUILayout.Space(10);

            if (owner != null && GUILayout.Button("Select Owner", EditorStyles.toolbarButton, GUILayout.Width(80)))
            {
                Selection.activeObject = owner;
                EditorGUIUtility.PingObject(owner);
            }

            if (EditorUtility.IsPersistent(graph) && GUILayout.Button("Select Graph", EditorStyles.toolbarButton, GUILayout.Width(80)))
            {
                Selection.activeObject = graph;
                EditorGUIUtility.PingObject(graph);
            }

            GUILayout.Space(10);

            if (GUILayout.Button("Open Console", EditorStyles.toolbarButton, GUILayout.Width(90)))
            {
                var type   = ReflectionTools.GetType("NodeCanvas.Editor.GraphConsole");
                var method = type.GetMethod("ShowWindow");
                method.Invoke(null, null);
            }

            ///----------------------------------------------------------------------------------------------
            ///Mid
            ///----------------------------------------------------------------------------------------------

            GUILayout.Space(10);
            GUILayout.FlexibleSpace();

            //TODO: implement search
            // EditorUtils.SearchField(null);

            GUILayout.FlexibleSpace();
            GUILayout.Space(10);

            ///----------------------------------------------------------------------------------------------
            ///Right side
            ///----------------------------------------------------------------------------------------------

            //SL-------------
            //--------------------
            if (EditorUtility.IsPersistent(graph) && GUILayout.Button("ClearUselessNestedAssets", EditorStyles.toolbarButton, GUILayout.Width(175)))
            {
                if (EditorUtility.DisplayDialog("Clear Useless Nested Assets", "清理当前prefab(包括其子物体)或graph资源中无用的嵌套资源,\n\n(需选中物体的是资源目录下的prefab或主Graph Asset).\n\n注意:prefab实例中的嵌套的存盘资源,如果没有被prefab本体引用,同样也被清除 ", "YES", "NO!"))
                {
                    NestedUtility.DeleteAllUselessBoundAsset(graph);
                    e.Use();
                    return;
                }
            }


            GUI.backgroundColor = Color.clear;
            GUI.color           = new Color(1, 1, 1, 0.3f);
            GUILayout.Label(string.Format("{0} @NodeCanvas Framework v{1}", graph.GetType().Name, NodeCanvas.Framework.Internal.GraphSerializationData.FRAMEWORK_VERSION), EditorStyles.toolbarButton);
            GUILayout.Space(10);
            GUI.color           = Color.white;
            GUI.backgroundColor = Color.white;

            //GRAPHOWNER JUMP SELECTION
            if (owner != null)
            {
                if (GUILayout.Button(string.Format("[{0}]", owner.gameObject.name), EditorStyles.toolbarDropDown, GUILayout.Width(120)))
                {
                    var menu = new GenericMenu();
                    foreach (var _o in Object.FindObjectsOfType <GraphOwner>())
                    {
                        var o = _o;
                        menu.AddItem(new GUIContent(o.GetType().Name + "s/" + o.gameObject.name + " --" + o.graph.GetType().Name), false, () => { Selection.activeGameObject = o.gameObject; SetReferences(o); });
                    }
                    menu.ShowAsContext();
                }
            }

            NCPrefs.isLocked = GUILayout.Toggle(NCPrefs.isLocked, "Lock", EditorStyles.toolbarButton);
            GUILayout.EndHorizontal();
            GUI.backgroundColor = Color.white;
            GUI.color           = Color.white;
        }