예제 #1
0
 public static void CheckHasName(BehaviorTreeConfig config, string path, string nodeName)
 {
     if (HasNode(config.RootNodeProto, nodeName))
     {
         Log.Info($"{nodeName}: {path}");
     }
 }
예제 #2
0
        public BTEditorTree(BehaviorTreeConfig config)
        {
            Type rootType = typeof(Game).Assembly.GetType($"Model.{config.RootNodeProto.Name}");
            Node root     = (Node)Activator.CreateInstance(rootType, config.RootNodeProto);

            root.Id = BTEditor.NodeIdStartIndex;
            Queue <NodeProto> protoStack = new Queue <NodeProto>();
            Queue <Node>      nodeStack  = new Queue <Node>();

            protoStack.Enqueue(config.RootNodeProto);
            nodeStack.Enqueue(root);
            while (protoStack.Count > 0)
            {
                NodeProto p    = protoStack.Dequeue();
                Node      node = nodeStack.Dequeue();

                foreach (KeyValuePair <string, object> argsItem in p.Args.Dict())
                {
                    FieldInfo fieldInfo = node.GetType().GetField(argsItem.Key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    fieldInfo.SetValue(node, argsItem.Value);
                }
                foreach (NodeProto child in p.Children)
                {
                    Type t         = typeof(Game).Assembly.GetType($"Model.{child.Name}");
                    Node childNode = (Node)Activator.CreateInstance(t, child);
                    AddChild(childNode, node);
                    protoStack.Enqueue(child);
                    nodeStack.Enqueue(childNode);
                }
            }
            this.BTConfig = config;
            _root         = root;
        }
예제 #3
0
파일: TreeEditor.cs 프로젝트: fry/refw
        public TreeEditor()
        {
            InitializeComponent();

            activeConfig = new BehaviorTreeConfig();
            RefreshAddControls();
        }
예제 #4
0
        public static void Serialize(this Node root, BehaviorTreeConfig config)
        {
            config.Clear();
            BehaviorNodeConfig         rootNp = config.AddRootNode(root.GetType().Name);
            Queue <Node>               queue  = new Queue <Node>();
            Queue <BehaviorNodeConfig> npQue  = new Queue <BehaviorNodeConfig>();

            rootNp.describe = root.Description;
            queue.Enqueue(root);
            npQue.Enqueue(rootNp);
            while (queue.Count > 0)
            {
                Node cur = queue.Dequeue();
                BehaviorNodeConfig np = npQue.Dequeue();
                foreach (Node child in cur.GetChildren)
                {
                    BehaviorNodeConfig childNp = GetNodeConfigFromNode(child);
                    queue.Enqueue(child);
                    npQue.Enqueue(childNp);
                    config.AddChild(np, childNp);
                }
            }
            //             PrintNode(root);
            //             PrintConfigNode(config.RootNodeConfig);
        }
예제 #5
0
        public BehaviorTreeData BehaviorTreeConfigToTreeData(BehaviorTreeConfig config)
        {
            BehaviorTreeData tree = new BehaviorTreeData();

            tree.Root = NodeConfigToNodeData(config.RootNodeConfig);
            return(tree);
        }
예제 #6
0
        public TreeEditor()
        {
            InitializeComponent();

            activeConfig = new BehaviorTreeConfig();
            RefreshAddControls();
        }
예제 #7
0
        private static BehaviorNodeConfig CreateNodeConfig(this BehaviorTreeConfig treeConfig, string name)
        {
            NodeMeta   proto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(name);
            GameObject go    = new GameObject()
            {
                name = name
            };

            go.transform.parent = treeConfig.gameObject.transform;
            BehaviorNodeConfig node = go.AddComponent <BehaviorNodeConfig>();

            node.name     = name;
            node.describe = proto.describe;

            foreach (NodeFieldDesc args in proto.new_args_desc)
            {
                Type type = BTTypeManager.GetBTType(args.type);
                UnityEngine.Component comp = go.AddComponent(type);
                FieldInfo             info = type.GetField("fieldName");
                info.SetValue(comp, args.name);
                FieldInfo info1 = type.GetField("fieldValue");
                info1.SetValue(comp, args.value);
            }
            return(node);
        }
예제 #8
0
        public static BehaviorNodeConfig AddChild(this BehaviorTreeConfig treeConfig, BehaviorNodeConfig parent, string name)
        {
            BehaviorNodeConfig child = treeConfig.CreateNodeConfig(name);

            AddChild(treeConfig, parent, child);
            return(child);
        }
예제 #9
0
 public static BehaviorNodeConfig AddChild(this BehaviorTreeConfig treeConfig, BehaviorNodeConfig parent, BehaviorNodeConfig child)
 {
     child.transform.parent = parent.transform;
     child.transform.SetAsLastSibling();
     child.GetComponent <BehaviorNodeConfig>().id = treeConfig.RootNodeId + treeConfig.AutoId;
     return(child.GetComponent <BehaviorNodeConfig>());
 }
예제 #10
0
        public BehaviorTreeConfig BehaviorTreeDataToConfig(BehaviorTreeData tree)
        {
            GameObject         curTreeGo = Object.Instantiate(CurTreeGO);
            BehaviorTreeConfig config    = curTreeGo.GetComponent <BehaviorTreeConfig>();

            if (config == null)
            {
                config = curTreeGo.AddComponent <BehaviorTreeConfig>();
            }
            foreach (Transform child in config.gameObject.transform)
            {
                Object.DestroyImmediate(child.gameObject);
            }
            try
            {
                config.RootNodeConfig = this.BehaviorNodeDataToNodeConfig(tree.Root);
            }
            catch (Exception e)
            {
                Log.Error($"tree name : {tree.BehaviorNodeData.Name} {e}");
            }

            config.RootNodeConfig.gameObject.transform.parent = config.gameObject.transform;
            return(config);
        }
예제 #11
0
        private void SavePrefabTree()
        {
            ResetTreeId();

            BehaviorTreeConfig config = BehaviorTreeDataToConfig(CurTree);

            RenameTree(config);
            Object.DestroyImmediate(config.gameObject);
        }
예제 #12
0
        public static BehaviorNodeConfig AddRootNode(this BehaviorTreeConfig treeConfig, string rootName)
        {
            BehaviorNodeConfig go = treeConfig.CreateNodeConfig(rootName);

            treeConfig.RootNodeConfig    = go.GetComponent <BehaviorNodeConfig>();
            treeConfig.RootNodeConfig.id = BTEditor.NodeIdStartIndex;
            go.gameObject.name           = rootName;
            return(go);
        }
        public static BehaviourTreeNodeProxy <T> AddNodeToLast <T>(BehaviorTreeConfig tree) where T : Node
        {
            BehaviorNodeConfig         parent = tree.RootNodeConfig;
            string                     name   = typeof(T).Name;
            BehaviorNodeConfig         p      = tree.AddChild(parent, name);
            BehaviourTreeNodeProxy <T> proxy  = new BehaviourTreeNodeProxy <T>(p.ToNodeProto());

            return(proxy);
        }
예제 #14
0
        public static BTEditorTree OpenFromGameObject(GameObject source)
        {
            BehaviorTreeConfig sourceTree = source.GetComponent <BehaviorTreeConfig>();

            if (sourceTree == null)
            {
                throw new Exception($"{source.name}预制中不包含行为树");
            }
            return(new BTEditorTree(sourceTree));
        }
예제 #15
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            BehaviorTreeConfig config = target as BehaviorTreeConfig;

            if (GUILayout.Button("打开行为树"))
            {
                BTEntity.Instance.OpenBehaviorEditor(config.gameObject);
            }
            EditorUtility.SetDirty(config);
        }
예제 #16
0
        public static BTEditorTree CreateTree <T>(GameObject source)
        {
            BehaviorTreeConfig sourceTree = source.GetComponent <BehaviorTreeConfig>();

            if (sourceTree == null)
            {
                sourceTree = source.AddComponent <BehaviorTreeConfig>();
            }
            Node         root = (Node)Activator.CreateInstance(typeof(T), new NodeProto());
            BTEditorTree tree = new BTEditorTree(root, sourceTree);

            return(tree);
        }
예제 #17
0
 public void Save()
 {
     if (IsPrefab(this.BTConfig.gameObject))
     {
         GameObject go = UnityEngine.Object.Instantiate(this.BTConfig.gameObject);
         go.name = this.BTConfig.gameObject.name;
         BehaviorTreeConfig newConfig = go.GetComponent <BehaviorTreeConfig>();
         _root.Serialize(newConfig);
         PrefabUtility.ReplacePrefab(go, this.BTConfig, ReplacePrefabOptions.ReplaceNameBased);
         UnityEngine.Object.DestroyImmediate(go);
     }
     else
     {
         _root.Serialize(this.BTConfig);
     }
 }
예제 #18
0
        public void RenameTree(BehaviorTreeConfig config)
        {
            string newName = $"{config.RootNodeConfig.describe}";

            if (!config.RootNodeConfig.describe.StartsWith("BT_"))
            {
                newName = $"BT_{config.RootNodeConfig.describe}";
            }
            config.gameObject.name = newName;
            CurTreeGO = PrefabUtility.ReplacePrefab(config.gameObject, CurTreeGO, ReplacePrefabOptions.ReplaceNameBased);
            string prefabPath = AssetDatabase.GetAssetPath(CurTreeGO);
            string result     = AssetDatabase.RenameAsset(prefabPath, newName);

            if (result.Contains("Invalid file name"))
            {
                Log.Error(result);
            }
            EditorUtility.SetDirty(config.gameObject);
        }
        public static GameObject CreateTree(string path, string rootNodeName, string desc = "默认行为树")
        {
            GameObject prefab = null;

            try
            {
                GameObject go = new GameObject();
                go.name = desc;
                prefab  = PrefabUtility.CreatePrefab($"{path}/BT_{desc}.prefab", go, ReplacePrefabOptions.ReplaceNameBased);
                BehaviorTreeConfig config = prefab.AddComponent <BehaviorTreeConfig>();
                config.AddRootNode(rootNodeName);
                EditorUtility.SetDirty(config);
                EditorUtility.SetDirty(prefab);
                Object.DestroyImmediate(go);
            }
            catch (Exception ex)
            {
                Log.Error($"创建行为树失败|{ex}");
            }
            return(prefab);
        }
        public static BTEditorTree Open(GameObject go)
        {
            BehaviorTreeConfig config = go.GetComponent <BehaviorTreeConfig>();

            return(new BTEditorTree(config));
        }
예제 #21
0
 public static void SaveOneTree(BehaviorTreeConfig treeConfig, string treePath, params object[] paramList)
 {
     EditorUtility.SetDirty(treeConfig.gameObject);
     AssetDatabase.SaveAssets();
 }
예제 #22
0
        public void NewLoadPrefabTree()
        {
            BehaviorTreeConfig config = CurTreeGO.GetComponent <BehaviorTreeConfig>();

            CurTree = BehaviorTreeConfigToTreeData(config);
        }
예제 #23
0
 private BTEditorTree(Node root, BehaviorTreeConfig config)
 {
     _root         = root;
     _root.Id      = BTEditor.NodeIdStartIndex;
     this.BTConfig = config;
 }
예제 #24
0
 public void SaveToBehaviorTreeConfig(BehaviorTreeConfig config)
 {
     _root.Serialize(config);
 }