Ejemplo n.º 1
0
        private void CopyNode(NodeEditor editor)
        {
            Node node = editor.node.Copy();

            if (isLocal)
            {
                Undo.RegisterCompleteObjectUndo(tree, "复制结点");
            }
            else
            {
                runtimeUndo.RecordTreeChange(tree, "复制结点");
            }
            BehaviourTree.Traverse(node, n =>
            {
                CreateNewNode(n, $"({tree.Nodes.Count}) {n.GetType().Name}{(tree.IsRuntime ? "(R)" : string.Empty)}", n._position + new Vector2(30, 30));
            });
            DrawTreeView(tree);
        }
Ejemplo n.º 2
0
        private GraphViewChange OnGraphViewChanged(GraphViewChange graphViewChange)
        {
            if (graphViewChange.elementsToRemove != null)
            {
                HashSet <NodeEditor> removedNodes = new HashSet <NodeEditor>();
                //为了保证撤销重做的顺利进行,先遍历结点,再遍历连线
                graphViewChange.elementsToRemove.ForEach(elem =>
                {
                    if (elem is NodeEditor editor)
                    {
                        if (isLocal)
                        {
                            Undo.RegisterCompleteObjectUndo(tree, "删除结点");
                        }
                        else
                        {
                            runtimeUndo.RecordTreeChange(tree, "删除结点");
                        }
                        tree.DeleteNode(editor.node);
                        removedNodes.Add(editor);
                    }
                });
                graphViewChange.elementsToRemove.ForEach(elem =>
                {
                    if (elem is Edge edge)
                    {
                        NodeEditor parent = edge.output.node as NodeEditor;
                        NodeEditor child  = edge.input.node as NodeEditor;
                        if (!removedNodes.Contains(parent) && !removedNodes.Contains(child))//不是是因删除结点引起的断连才记录
                        {
                            if (isLocal)
                            {
                                Undo.RegisterCompleteObjectUndo(tree, "断开子结点");
                            }
                            else
                            {
                                runtimeUndo.RecordTreeChange(tree, "断开子结点");
                            }
                        }
                        parent.node.RemoveChild(child.node);
                        UpdateValid(parent);
                        UpdateValid(child);
                    }
                });
            }

            if (graphViewChange.edgesToCreate != null)
            {
                graphViewChange.edgesToCreate.ForEach(edge =>
                {
                    if (isLocal)
                    {
                        Undo.RegisterCompleteObjectUndo(tree, "连接子结点");
                    }
                    else
                    {
                        runtimeUndo.RecordTreeChange(tree, "连接子结点");
                    }
                    NodeEditor parent = edge.output.node as NodeEditor;
                    NodeEditor child  = edge.input.node as NodeEditor;
                    parent.node.AddChild(child.node);
                    UpdateValid(parent);
                    UpdateValid(child);
                });
            }
            if (graphViewChange.elementsToRemove != null || graphViewChange.edgesToCreate != null)
            {
                EditorUtility.SetDirty(tree);
            }

            nodes.ForEach((n) =>
            {
                NodeEditor editor = n as NodeEditor;
                editor.TrySort();
            });

            tree.SortPriority();

            return(graphViewChange);
        }
Ejemplo n.º 3
0
 public void InspectNode(BehaviourTree tree, NodeEditor node)
 {
     Clear();
     if (serializedObject != null)
     {
         serializedObject.Dispose();
     }
     this.tree = tree;
     if (!tree.IsInstance)
     {
         global = ZetanUtility.Editor.LoadAsset <GlobalVariables>();
     }
     else
     {
         global = BehaviourManager.Instance.GlobalVariables;
     }
     nodeEditor = node;
     if (tree && nodeEditor != null && nodeEditor.node)
     {
         serializedObject = new SerializedObject(tree);
         SerializedProperty nodes     = serializedObject.FindProperty("nodes");
         IMGUIContainer     container = new IMGUIContainer(() =>
         {
             if (tree && nodeEditor != null && nodeEditor.node)
             {
                 serializedObject.UpdateIfRequiredOrScript();
                 EditorGUI.BeginChangeCheck();
                 EditorGUILayout.LabelField($"[结点名称]\t{nodeEditor.node.name}");
                 EditorGUILayout.LabelField($"[优先级]\t{nodeEditor.node.priority}");
                 var nType     = nodeEditor.node.GetType();
                 var fields    = new HashSet <string>(nType.GetFields(ZetanUtility.CommonBindingFlags).Select(f => f.Name));
                 var fieldsMap = new HashSet <string>();
                 using (SerializedProperty property = nodes.GetArrayElementAtIndex(tree.Nodes.IndexOf(nodeEditor.node)))
                 {
                     using var end = property.GetEndProperty();
                     property.Next(true);
                     while (property.NextVisible(false) && !SerializedProperty.EqualContents(property, end))
                     {
                         string field = property.name;
                         if (field == "name" || field == "priority" || field == "child" || field == "children" || field == "start" ||
                             field == "isRuntime" || field == "isShared" || field == "isGlobal")
                         {
                             continue;
                         }
                         if (fields.Contains(field))
                         {
                             if (fieldsMap.Contains(field))
                             {
                                 break;
                             }
                             fieldsMap.Add(field);
                             var fInfo = nType.GetField(field, ZetanUtility.CommonBindingFlags);
                             DrawProperty(property, fInfo);
                         }
                     }
                 }
                 if (EditorGUI.EndChangeCheck())
                 {
                     serializedObject.ApplyModifiedProperties();
                 }
             }
         });
         Add(container);
     }
 }