Example #1
0
        public static BehaviorNodeConfig ProtoToConfig(NodeProto nodeData)
        {
            GameObject         go         = new GameObject();
            BehaviorNodeConfig nodeConfig = go.AddComponent <BehaviorNodeConfig>();

            nodeConfig.id = nodeData.nodeId;
            ((UnityEngine.Object)nodeConfig).name = nodeData.name;
            go.name             = nodeData.name;
            nodeConfig.describe = nodeData.describe;
            foreach (var args in nodeData.args_dict)
            {
                Type originType = ExportNodeTypeConfig.GetFieldType(nodeData.name, args.Key);
                try
                {
                    string fieldName                    = args.Key;
                    object fieldValue                   = args.Value.GetValueByType(originType);
                    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 (BehaviorTreeArgsDict.IsEnumType(originType))
                    {
                        fieldValue = fieldValue.ToString();
                    }
                    fieldValueinfo.SetValue(comp, fieldValue);
                }
                catch (Exception ex)
                {
                    throw new GameException($"transform failed,nodeName:{nodeData.name}  fieldName:{args.Key} fieldType:{originType}");
                }
            }
            foreach (var child in nodeData.children)
            {
                BehaviorNodeConfig childConfig = ProtoToConfig(child);
                childConfig.gameObject.transform.parent = nodeConfig.gameObject.transform;
            }
            return(nodeConfig);
        }
        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);
        }