public BehaviorNodeConfig BehaviorNodeDataToNodeConfig(BehaviorNodeData 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; List <string> unUseList = new List <string>(); foreach (KeyValuePair <string, object> args in nodeData.Args.Dict()) { if (!NodeMetaHelper.NodeHasField(nodeData.Name, args.Key)) { unUseList.Add(args.Key); continue; } Type originType = NodeMetaHelper.GetFieldType(nodeData.Name, args.Key); try { string fieldName = args.Key; object fieldValue = args.Value; Type type = BTTypeManager.GetBTType(originType); 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 (string key in unUseList) { nodeData.Args.Remove(key); } foreach (BehaviorNodeData child in nodeData.children) { BehaviorNodeConfig childConfig = this.BehaviorNodeDataToNodeConfig(child); childConfig.gameObject.transform.parent = nodeConfig.gameObject.transform; } return(nodeConfig); }
private void DrawProp(string nodeName, List <NodeFieldDesc> fieldList, NodeParamType nodeParamType) { for (int i = 0; i < fieldList.Count; i++) { NodeFieldDesc desc = fieldList[i]; Type fieldType = NodeMetaHelper.GetFieldType(nodeName, desc.name); NodeMeta nodeMeta = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(nodeName); // 如果不存在这个参数,给一个默认的 if (!mCurBehaviorNode.Args.ContainsKey(desc.name)) { object obj = desc.value ?? BTTypeManager.GetDefaultValue(fieldType); mCurBehaviorNode.Args.Add(desc.name, obj); } object newValue = null; if (TypeHelper.IsStringType(fieldType)) { if (nodeParamType == NodeParamType.Input) { newValue = InputEnumFieldValue(desc); } else if (nodeParamType == NodeParamType.Output && nodeMeta.classify == NodeClassifyType.Root.ToString()) { newValue = ConstTextFieldValue(desc); } else { newValue = TextFieldValue(desc); } } else if (TypeHelper.IsFloatType(fieldType)) { newValue = FloatFieldValue(desc); } else if (TypeHelper.IsDoubleType(fieldType)) { newValue = DoubletFieldValue(desc); } else if (TypeHelper.IsIntType(fieldType)) { newValue = IntFieldValue(desc); } else if (TypeHelper.IsLongType(fieldType)) { newValue = LongFieldValue(desc); } else if (TypeHelper.IsBoolType(fieldType)) { newValue = BoolFieldValue(desc); } else if (TypeHelper.IsObjectType(fieldType)) { newValue = ObjectFieldValue(desc); } else if (TypeHelper.IsIntArrType(fieldType)) { newValue = IntArrFieldValue(desc); } else if (TypeHelper.IsLongArrType(fieldType)) { newValue = LongArrFieldValue(desc); } else if (TypeHelper.IsStringArrType(fieldType)) { newValue = StrArrFieldValue(desc); } else if (TypeHelper.IsFloatArrType(fieldType)) { newValue = FloatArrFieldValue(desc); } else if (TypeHelper.IsDoubleArrType(fieldType)) { newValue = DoubleArrFieldValue(desc); } else if (TypeHelper.IsEnumType(fieldType)) { newValue = EnumFieldValue(desc); } else if (TypeHelper.IsObjectArrayType(fieldType)) { newValue = ObjectArrFieldValue(desc); } else { Log.Error($"行为树节点暂时未支持此类型:{fieldType}!"); return; } mCurBehaviorNode.Args.SetKeyValueComp(desc.name, newValue); } }