Esempio n. 1
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);
        }
Esempio n. 2
0
        public static BehaviorNodeConfig AddChild(this BehaviorTreeConfig treeConfig, BehaviorNodeConfig parent, string name)
        {
            BehaviorNodeConfig child = treeConfig.CreateNodeConfig(name);

            AddChild(treeConfig, parent, child);
            return(child);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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 NodeProto ConfigToNode(BehaviorNodeConfig nodeProto)
        {
            NodeProto nodeData = new NodeProto();

            nodeData.Id       = nodeProto.id;
            nodeData.Name     = nodeProto.name;
            nodeData.Desc     = nodeProto.describe;
            nodeData.Args     = nodeProto.GetArgsDict();
            nodeData.children = new List <NodeProto>();
            foreach (Transform child in nodeProto.gameObject.transform)
            {
                BehaviorNodeConfig nodeConfig = child.gameObject.GetComponent <BehaviorNodeConfig>();
                NodeProto          childData  = ConfigToNode(nodeConfig);
                nodeData.children.Add(childData);
            }
            return(nodeData);
        }
Esempio n. 6
0
        private static NodeProto BehaviorNodeConfigToNodeProto(BehaviorNodeConfig behaviorNodeConfig)
        {
            NodeProto nodeProto = new NodeProto
            {
                Id       = behaviorNodeConfig.id,
                Name     = behaviorNodeConfig.name,
                Desc     = behaviorNodeConfig.describe,
                Args     = behaviorNodeConfig.GetArgsDict(),
                children = new List <NodeProto>()
            };

            foreach (Transform child in behaviorNodeConfig.gameObject.transform)
            {
                BehaviorNodeConfig nodeConfig = child.gameObject.GetComponent <BehaviorNodeConfig>();
                NodeProto          childData  = BehaviorNodeConfigToNodeProto(nodeConfig);
                nodeProto.children.Add(childData);
            }
            return(nodeProto);
        }
        public static BehaviorNodeConfig ProtoToConfig(NodeProto nodeData)
        {
            GameObject         go         = new GameObject();
            BehaviorNodeConfig nodeConfig = go.AddComponent <BehaviorNodeConfig>();

            nodeConfig.id       = nodeData.Id;
            nodeConfig.name     = nodeData.Name;
            go.name             = nodeData.Name;
            nodeConfig.describe = nodeData.Desc;
            foreach (KeyValuePair <string, object> args in nodeData.Args.Dict())
            {
                Type originType = NodeMetaHelper.GetFieldType(nodeData.Name, args.Key);
                try
                {
                    string fieldName                    = args.Key;
                    object fieldValue                   = args.Value;
                    Type   type                         = BTTypeManager.GetBTType(originType);
                    UnityEngine.Component comp          = go.AddComponent(type);
                    FieldInfo             fieldNameInfo = type.GetField("fieldName");
                    fieldNameInfo.SetValue(comp, fieldName);
                    FieldInfo fieldValueinfo = type.GetField("fieldValue");
                    if (TypeHelper.IsEnumType(originType))
                    {
                        fieldValue = fieldValue.ToString();
                    }
                    fieldValueinfo.SetValue(comp, fieldValue);
                }
                catch (Exception e)
                {
                    throw new Exception($"transform failed,nodeName:{nodeData.Name}  fieldName:{args.Key} fieldType:{originType} {e}");
                }
            }
            foreach (NodeProto child in nodeData.children)
            {
                BehaviorNodeConfig childConfig = ProtoToConfig(child);
                childConfig.gameObject.transform.parent = nodeConfig.gameObject.transform;
            }
            return(nodeConfig);
        }
Esempio n. 8
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>());
 }
Esempio n. 9
0
 public void Clear()
 {
     DestroyImmediate(RootNodeConfig, true);
     RootNodeConfig = null;
 }