Esempio n. 1
0
 public object GetValueByType(Type type)
 {
     try
     {
         FieldInfo fieldInfo = GetFieldInfo(type);
         if (BehaviorTreeArgsDict.IsEnumType(type))
         {
             Enum value;
             if (string.IsNullOrEmpty(enumValue))
             {
                 value = GetDefaultEnumValue(type);
             }
             else
             {
                 value = (Enum)Enum.Parse(type, enumValue);
             }
             return(value);
         }
         if (BehaviorTreeArgsDict.IsStringType(type))
         {
             if (string.IsNullOrEmpty(this.StringValue))
             {
                 this.StringValue = this.enumValue;
                 return(this.StringValue);
             }
         }
         return(fieldInfo.GetValue(this));
     }
     catch (Exception err)
     {
         throw new GameException($"行为树报错:{err}");
     }
 }
Esempio n. 2
0
 public void SetValue(Type type, string fieldName, object value)
 {
     foreach (BTTypeBaseComponent item in gameObject.GetComponents <BTTypeBaseComponent>())
     {
         if (fieldName == item.fieldName)
         {
             object    fieldValue     = value;
             FieldInfo fieldValueinfo = item.GetType().GetField("fieldValue");
             if (BehaviorTreeArgsDict.IsEnumType(type))
             {
                 fieldValue = value.ToString();
             }
             fieldValueinfo.SetValue(item, fieldValue);
         }
     }
 }
Esempio n. 3
0
        private FieldInfo GetFieldInfo(Type type)
        {
            string fieldName;

            if (BehaviorTreeArgsDict.IsEnumType(type))
            {
                fieldName = "enumValue";
            }
            else if (type.IsArray)
            {
                fieldName = type.GetElementType() + "Array";
            }
            else
            {
                fieldName = type.Name + "Value";
            }
            fieldName = RemovePrefix(fieldName);
            FieldInfo fieldInfo = GetType().GetField(fieldName);

            return(fieldInfo);
        }
Esempio n. 4
0
        public static Type GetBTType(Type originType)
        {
            Type type = null;

            try
            {
                if (BehaviorTreeArgsDict.IsEnumType(originType))
                {
                    type = BTTypeDict[typeof(Enum)];
                }
                else
                {
                    type = BTTypeDict[originType];
                }
            }
            catch (Exception e)
            {
                throw new Exception($"{originType} not exist!", e);
            }
            return(type);
        }
Esempio n. 5
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);
        }