Location <T> LoadNode <T>(Location <T> parent, BehaviorTreeBlueprint <T> blueprint, int index) { TreeNodeData data = allNodes[index]; Type type = BehaviorTreeNodeAttribute.SerializedNameToType.TryGetValue(data.nodeTypeSerializableName); if (type == null) { throw new Exception($"Unexpected serialized node type name {data.nodeTypeSerializableName}."); } if (type.ContainsGenericParameters) { type = type.MakeGenericType(typeof(T)); } INodeType node; //Prepare node and parameters if (data.parameters == null || data.parameters.Length == 0) { node = (INodeType)Activator.CreateInstance(type); } else { object[] parameters = new object[data.parameters.Length]; for (int i = 0; i < data.parameters.Length; i++) { SerializableParameter parameter = data.parameters[i]; if (parameter.Type == ParameterType.behaviorAction) { parameter.LoadBehaviorAction(importData); parameters[i] = GetAction <T>(parameter); } else { parameters[i] = parameter.GetValue(); } } node = (INodeType)Activator.CreateInstance(type, parameters); } //Add to blueprint Location <T> location = blueprint.Add(parent, node, out bool success); if (!success) { throw new Exception($"Invalid node create from graph at GUID: {data.GUID}, position: {data.position}, serialized node name: {data.nodeTypeSerializableName}!"); } return(location); }
void LoadBranch <T>(Location <T> parent, BehaviorTreeBlueprint <T> blueprint, int index) { TreeNodeData data = allNodes[index]; for (int i = 0; i < data.children.Length; i++) { int childIndex = data.children[i]; var location = LoadNode(parent, blueprint, childIndex); LoadBranch(location, blueprint, childIndex); } }
TreeGraphNode DeserializeNode(TreeNodeData data) { NodeInfo info = serializedNameToInfo.TryGetValue(data.nodeTypeSerializableName) ?? throw new Exception($"Unexpected serialized name {data.nodeTypeSerializableName}"); TreeGraphNode node = graphView.CreateNewNode(info, data.position, data.parameters); node.GUID = data.GUID; //Children TreeGraphNode child = null; for (int i = 0; i < data.children.Length; i++) { child = DeserializeNode(allData[data.children[i]]); graphView.AddElement(child.ParentPort.ConnectTo(node.ChildrenPort)); } child?.RecalculateOrder(); //This will calculate the order of its siblings too return(node); }
int SerializeNode(TreeGraphNode node) { var data = new TreeNodeData { position = node.GetPosition().position, //Position nodeTypeSerializableName = node.Info.serializedName, //Node type name parameters = node.Parameters //Parameters }; //Children if (node.ChildrenPort == null || !node.ChildrenPort.connected) { data.children = Array.Empty <int>(); } else { var childIndices = new List <int>(); foreach (Edge connection in node.ChildrenPort.connections) { if (!(connection.input.node is TreeGraphNode child)) { continue; } while (child.Order >= childIndices.Count) { childIndices.Add(-1); } childIndices[child.Order] = SerializeNode(child); } data.children = childIndices.ToArray(); } //Finalize data.GUID = allData.Count; allData.Add(data); return(data.GUID); }