Exemple #1
0
        public static void ShowGo()
        {
            TextNewECtrl window = ScriptableObject.CreateInstance <TextNewECtrl>();

            if (Instance == null)
            {
                Instance = new RunTimeNodesManager();
            }
            InitCtrl(window);

            foreach (GameObject go in Selection.gameObjects)
            {
                string path = AssetDatabase.GetAssetPath(go);
                try
                {
                    BehaviorTreeConfig config = go.GetComponent <BehaviorTreeConfig>();
                    if (config == null)
                    {
                        Log.Debug($"{go.name}不是行为树");
                        continue;
                    }
                    Instance.InitBeTree(config.RootNodeProto, path, go.GetInstanceID());
                    window.SetName(Path.GetFileNameWithoutExtension(path));
                }
                catch (Exception err)
                {
                    BehaviourTreeDebugPanel.Error(Instance._curOpenningCtrl, $"打开行为树失败:{path}-> {err}");
                }
            }
        }
Exemple #2
0
        public BTEditorTree(BehaviorTreeConfig config)
        {
            Type rootType = Game.EntityEventManager.GetAssembly("Model").GetType($"Model.{config.RootNodeProto.name}");
            Node root     = (Node)Activator.CreateInstance(rootType, config.RootNodeProto);

            root.Id = BehaviorManager.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 (var argsItem in p.args_dict)
                {
                    FieldInfo fieldInfo = node.GetType().GetField(argsItem.Key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    fieldInfo.SetValue(node, argsItem.Value.GetValueByType(fieldInfo.FieldType));
                }
                foreach (NodeProto child in p.Children)
                {
                    Type t         = Game.EntityEventManager.GetAssembly("Model").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;
        }
Exemple #3
0
        public BTEditorTree(BehaviorTreeConfig config)
        {
            Type rootType = ObjectEvents.Instance.GetAssembly("Model").GetType($"Model.{config.RootNodeProto.Name}");
            Node root     = (Node)Activator.CreateInstance(rootType, config.RootNodeProto);

            root.Id = BTEntity.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         = ObjectEvents.Instance.GetAssembly("Model").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;
        }
        public static BehaviorNodeConfig AddChild(this BehaviorTreeConfig treeConfig, BehaviorNodeConfig parent, string name)
        {
            BehaviorNodeConfig child = treeConfig.CreateNodeConfig(name);

            AddChild(treeConfig, parent, child);
            return(child);
        }
        public BehaviorTree CreateTree(Scene scene, long ownerId, GameObject treeGo)
        {
            try
            {
                if (treeGo == null)
                {
                    return(null);
                }
                BehaviorTree tree;
                if (this.treeCache.TryGetValue(treeGo, out tree))
                {
                    return(tree);
                }


                BehaviorTreeConfig behaviorTreeConfig = treeGo.GetComponent <BehaviorTreeConfig>();
                Node node = this.CreateTreeNode(behaviorTreeConfig.RootNodeProto);

                tree = new BehaviorTree(scene, ownerId, node)
                {
                    GameObjectId = treeGo.GetInstanceID()
                };
                if (Define.IsAsync)
                {
                    this.treeCache.Add(treeGo, tree);
                }
                return(tree);
            }
            catch (Exception e)
            {
                throw new Exception($"行为树配置错误: {treeGo.name}", e);
            }
        }
 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>());
 }
        public BehaviorTree CreateTree(Scene scene, GameObject treeGo)
        {
            try
            {
                BehaviorTree tree;
                if (this.treeCache.TryGetValue(treeGo, out tree))
                {
                    return(tree);
                }

                BehaviorTreeConfig behaviorTreeConfig = treeGo.GetComponent <BehaviorTreeConfig>();
                Node node = this.CreateTreeNode(behaviorTreeConfig.RootNodeProto);
                tree = new BehaviorTree(scene, node);
                tree.behaviorTreeConfig = behaviorTreeConfig;
                if (Define.LoadResourceType == LoadResourceType.Async)
                {
                    this.treeCache.Add(treeGo, tree);
                }
                return(tree);
            }
            catch (Exception e)
            {
                throw new ConfigException($"行为树配置错误: {treeGo.name}", e);
            }
        }
        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);
        }
Exemple #9
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);
        }
        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);
        }
Exemple #11
0
        public static BTEditorTree OpenFromGameObject(GameObject source)
        {
            BehaviorTreeConfig sourceTree = source.GetComponent <BehaviorTreeConfig>();

            if (sourceTree == null)
            {
                throw new GameException($"{source.name}预制中不包含行为树");
            }
            return(new BTEditorTree(sourceTree));
        }
        public static void WriteToJson(BehaviorTreeConfig root, string namePath)
        {
            NodeProto proto = root.RootNodeProto;
            string    path  = namePath;

            using (StreamWriter writer = new StreamWriter(path))
            {
                string json = MongoHelper.ToJson(proto);
                writer.Write(json);
            }
        }
Exemple #13
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);
        }
        private static GameObject CreatePrefabWithNodeProto(NodeProto proto)
        {
            GameObject         go         = new GameObject(proto.Desc);
            BehaviorTreeConfig treeConfig = go.AddComponent <BehaviorTreeConfig>();
            GameObject         noGo       = new GameObject(proto.Name);
            BehaviorNodeConfig nodeConfig = noGo.AddComponent <BehaviorNodeConfig>();

            treeConfig.RootNodeConfig = nodeConfig;
            nodeConfig.id             = proto.Id;
            nodeConfig.name           = nodeConfig.name;
            nodeConfig.describe       = proto.Desc;
            noGo.transform.SetParent(go.transform);
            return(go);
        }
Exemple #15
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);
     }
 }
        private static BehaviorNodeConfig CreateNodeConfig(this BehaviorTreeConfig treeConfig, string name)
        {
            ClientNodeTypeProto proto = ExportNodeTypeConfig.GetNodeTypeProtoFromDll(name);
            GameObject          go    = new GameObject();

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

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

            foreach (var 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);
        }
Exemple #17
0
 public void SaveToBehaviorTreeConfig(BehaviorTreeConfig config)
 {
     _root.Serialize(config);
 }
        public void OnGUI()
        {
            _name = EditorDataFields.EditorDataField("名字", _name);

            using (new EditorHorizontalLayout())
            {
                if (GUILayout.Button("搜索客户端"))
                {
                    bool get = false;
                    _goes.Clear();
                    _files.Clear();
                    foreach (NodeParam param in _nodes)
                    {
                        if (param.NodeType.Name == _name)
                        {
                            get       = true;
                            _selected = param;
                            break;
                        }
                    }

                    if (get)
                    {
                        List <string> paths = EditorResHelper.GetAllPath("Assets", true);
                        foreach (string path in paths)
                        {
                            GameObject         go     = AssetDatabase.LoadAssetAtPath <GameObject>(path);
                            BehaviorTreeConfig config = go.GetComponent <BehaviorTreeConfig>();
                            if (!config)
                            {
                                continue;
                            }

                            NodeProto         p     = config.RootNodeProto;
                            Stack <NodeProto> stack = new Stack <NodeProto>();
                            stack.Push(p);

                            while (stack.Count > 0)
                            {
                                NodeProto node = stack.Pop();
                                if (node.Name == _name)
                                {
                                    _goes.Add(go);
                                    break;
                                }

                                foreach (NodeProto child in node.Children)
                                {
                                    stack.Push(child);
                                }
                            }
                        }
                    }
                    else
                    {
                        _selected = null;
                    }
                }
                if (GUILayout.Button("搜索服务器"))
                {
                    string path = EditorTreeConfigHelper.Instance.Config.ServersPath;
                    bool   get  = false;
                    _goes.Clear();
                    _files.Clear();
                    foreach (NodeParam param in _nodes)
                    {
                        if (param.NodeType.Name == _name)
                        {
                            get       = true;
                            _selected = param;
                            break;
                        }
                    }

                    if (get)
                    {
                        string[] files = Directory.GetFiles(path, "*.txt");
                        foreach (string file in files)
                        {
                            try
                            {
                                StreamReader      reader = new StreamReader(file);
                                string            data   = reader.ReadToEnd();
                                NodeProto         p      = MongoHelper.FromJson <NodeProto>(data);
                                Queue <NodeProto> queue  = new Queue <NodeProto>();
                                queue.Enqueue(p);
                                while (queue.Count > 0)
                                {
                                    NodeProto node = queue.Dequeue();
                                    if (node.Name == _name)
                                    {
                                        _files.Add(file);
                                        break;
                                    }

                                    foreach (NodeProto child in node.Children)
                                    {
                                        queue.Enqueue(child);
                                    }
                                }
                            }
                            catch (Exception err)
                            {
                                BehaviourTreeDebugPanel.Error($"文件({file})无法解析成行为树");
                                Log.Error(err);
                            }
                        }
                    }
                }
            }

            if (_selected == null)
            {
                EditorGUILayout.LabelField("请输入节点名称,不需要命名空间");
                return;
            }
            if (_goes.Count > 0)
            {
                EditorDataFields.EditorListDataField(_goes, 20);
            }
            else if (_files.Count > 0)
            {
                EditorDataFields.EditorListDataField(_files, 20);
            }
        }
Exemple #19
0
 private BTEditorTree(Node root, BehaviorTreeConfig config)
 {
     _root         = root;
     _root.Id      = BTEntity.NodeIdStartIndex;
     this.BTConfig = config;
 }