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); }
public static BehaviorNodeConfig AddChild(this BehaviorTreeConfig treeConfig, BehaviorNodeConfig parent, string name) { BehaviorNodeConfig child = treeConfig.CreateNodeConfig(name); AddChild(treeConfig, parent, child); return(child); }
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); }
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); }
private static GameObject CreatePrefabWithNodeProto(NodeProto proto) { GameObject go = new GameObject(proto.Desc); BehaviorTreeConfig treeConfig = go.AddComponent <BehaviorTreeConfig>(); GameObject noGo = new GameObject(proto.Name); BehaviorNodeConfig nodeConfig = noGo.AddComponent <BehaviorNodeConfig>(); treeConfig.RootNodeConfig = nodeConfig; nodeConfig.id = proto.Id; nodeConfig.name = nodeConfig.name; nodeConfig.describe = proto.Desc; noGo.transform.SetParent(go.transform); return(go); }
public static NodeProto ConfigToNode(BehaviorNodeConfig nodeProto) { NodeProto nodeData = new NodeProto(); nodeData.nodeId = nodeProto.id; nodeData.name = ((UnityEngine.Object)nodeProto).name; nodeData.describe = nodeProto.describe; nodeData.args_dict = 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); }
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.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); }
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); }
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); }
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>()); }
public void Clear() { DestroyImmediate(RootNodeConfig, true); RootNodeConfig = null; }
public bool Save(out GameObject saveGo) { saveGo = null; if (!SaveCheck()) { return(false); } NodeParam root = _root; Queue <NodeParam> paramQueue = new Queue <NodeParam>(); Dictionary <NodeParam, GameObject> parentGoes = new Dictionary <NodeParam, GameObject>(); GameObject goRoot = null; paramQueue.Enqueue(root); while (paramQueue.Count > 0) { NodeParam p = paramQueue.Dequeue(); GameObject go = new GameObject(); GameObject parent; if (!parentGoes.TryGetValue(p, out parent)) { goRoot = go; } else { go.transform.SetParent(parent.transform); } BehaviorNodeConfig nodeConfig = go.AddComponent <BehaviorNodeConfig>(); nodeConfig.id = p.NodeID; nodeConfig.name = p.NodeType.Name; go.name = nodeConfig.name; nodeConfig.describe = p.NodeDesc; foreach (ParamInfo param in p.Fields) { string key = param.FieldName; object value = param.DefaultValue; AddComponentToGameObject(param.ParamType, go, key, value); } foreach (ParamInfoInput input in p.Inputs) { string key = input.InputFieldName; if (input.Input == null) { continue; } string value = input.Input.OutputName; AddComponentToGameObject(typeof(string), go, key, value); } foreach (ParamInfoOutput op in p.Outputs) { string key = op.OutputFieldName; string value = op.OutputName; AddComponentToGameObject(typeof(string), go, key, value); } foreach (NodeParam child in p.ChildrenList) { paramQueue.Enqueue(child); parentGoes.Add(child, go); } } saveGo = goRoot; return(true); }