/// <summary>
 /// A static method to remove the node from graph's nodes list and clean up
 /// </summary>
 /// <param name="graph"></param>
 /// <param name="deleteNode"></param>
 static void RemoveNodeAndCleanUp(ChainGraph graph, ChainBehaviorNode deleteNode)
 {
     foreach (ChainBehaviorNode n in graph.nodes)
     {
         if (n.output.nodes != null)
         {
             if (n.output.nodes.Contains(deleteNode))
             {
                 n.output.nodes.Remove(deleteNode);
             }
         }
     }
     graph.nodes.Remove(deleteNode);
 }
 /// <summary>
 /// A static method to delete the node in the graph
 /// </summary>
 /// <param name="nodeIndex"></param>
 /// <param name="graph"></param>
 public static void DeleteNode(int nodeIndex, ChainGraph graph)
 {
     if (graph != null)
     {
         if (graph.nodes.Count >= nodeIndex)
         {
             ChainBehaviorNode deleteNode = (ChainBehaviorNode)graph.nodes[nodeIndex];
             if (deleteNode != null)
             {
                 RemoveNodeAndCleanUp(graph, deleteNode);
                 UpdateChainBehaviorNodeID(graph);
                 UpdateFollowUps(graph);
                 UnityEngine.Object.DestroyImmediate(deleteNode, true);
                 AssetDatabase.SaveAssets();
                 AssetDatabase.Refresh();
             }
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Update key and mouse events
        /// </summary>
        /// <param name="e"></param>
        /// <param name="viewRect"></param>
        void ProcessEvents(Event e, Rect viewRect)
        {
            if (viewRect.Contains(e.mousePosition))
            {
                if (e.button == 0)
                {
                    if (e.type == EventType.MouseDown)
                    {
                        DeselectAllNodes();
                        bool setNode = false;
                        selectedNode = null;

                        // Select node
                        for (int i = 0; i < nodes.Count; i++)
                        {
                            if (nodes[i].nodeRect.Contains(e.mousePosition))
                            {
                                nodes[i].isSelected    = true;
                                selectedNode           = nodes[i];
                                Selection.activeObject = selectedNode;
                                setNode = true;
                            }
                        }

                        if (!setNode)
                        {
                            DeselectAllNodes();
                        }

                        if (connectionRequest)
                        {
                            connectionRequest = false;
                        }
                    }
                }
            }
        }
        /// <summary>
        ///  A static method to create a node in graph
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="nodeType"></param>
        /// <param name="pos"></param>
        public static void CreateNode(ChainGraph graph, NodeType nodeType, Vector2 pos)
        {
            if (graph != null)
            {
                ChainBehaviorNode node = null;
                switch (nodeType)
                {
                case NodeType.ChainBehaviorNode:
                    node          = ScriptableObject.CreateInstance <ChainBehaviorNode>();
                    node.nodeName = "Chain Behavior";
                    break;

                case NodeType.RootBehaviorNode:
                    node          = ScriptableObject.CreateInstance <RootBehaviorNode>();
                    node.nodeName = "Root Behavior";
                    break;

                default:
                    break;
                }

                if (node != null)
                {
                    node.InitNode(graph);
                    node.nodeRect.x  = pos.x;
                    node.nodeRect.y  = pos.y;
                    node.parentGraph = graph;
                    graph.nodes.Add(node);
                    UpdateChainBehaviorNodeID(graph);

                    node.name = node.nodeName;
                    AssetDatabase.AddObjectToAsset(node, graph);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                }
            }
        }
 /// <summary>
 /// A static method to reorder the list by its priority
 /// </summary>
 /// <param name="n"></param>
 public static void ReorderByPriority(ChainBehaviorNode n)
 {
     n.output.nodes = n.output.nodes.OrderBy(o => o.priority).ToList();
 }