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

            AddChild(treeConfig, parent, child);
            return(child);
        }
예제 #3
0
파일: NodeExtension.cs 프로젝트: yph0011/ET
        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);
        }
예제 #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);
        }
예제 #5
0
        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);
        }
예제 #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);
        }
예제 #7
0
        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);
        }
예제 #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>());
 }
예제 #9
0
 public void Clear()
 {
     DestroyImmediate(RootNodeConfig, true);
     RootNodeConfig = null;
 }