/// <summary> /// Creates and returns the <see cref="BehaviourTree"/> corresponding to this <see cref="XMLElement"/> /// </summary> /// <param name="parent"></param> /// <param name="selectedNode"></param> /// <returns></returns> public BehaviourTree ToBehaviourTree(ClickableElement parent, BaseNode selectedNode = null) { BehaviourTree bt = ScriptableObject.CreateInstance <BehaviourTree>(); bt.InitBehaviourTreeFromXML(parent, this.windowPosX, this.windowPosY, this.Id, this.name); foreach (XMLElement node in this.nodes) { switch (node.elemType) { case nameof(FSM): node.ToFSM(bt, null); break; case nameof(BehaviourTree): node.ToBehaviourTree(bt, null); break; case nameof(UtilitySystem): node.ToUtilitySystem(bt, null); break; case nameof(BehaviourNode): node.ToBehaviourNode(null, bt, parent); break; default: Debug.LogError("Wrong content in saved data"); break; } } foreach (XMLElement trans in this.transitions) { BaseNode node1 = bt.nodes.Where(n => n.identificator == trans.fromId || n.subElem?.identificator == trans.fromId).FirstOrDefault(); BaseNode node2 = bt.nodes.Where(n => n.identificator == trans.toId || n.subElem?.identificator == trans.toId).FirstOrDefault(); if (node1 != null && node2 != null) { bt.transitions.Add(trans.ToTransitionGUI(bt, node1, node2)); } } if (parent) { switch (parent.GetType().ToString()) { case nameof(FSM): StateNode state = ScriptableObject.CreateInstance <StateNode>(); state.InitStateNodeFromXML(parent, stateType.Unconnected, bt.windowRect.position.x, bt.windowRect.position.y, this.Id, this.name, bt); if (this.secondType.Equals(stateType.Entry.ToString())) { ((FSM)parent).AddEntryState(state); } else { parent.nodes.Add(state); } break; case nameof(BehaviourTree): BehaviourNode node = ScriptableObject.CreateInstance <BehaviourNode>(); node.InitBehaviourNode(parent, behaviourType.Leaf, bt.windowRect.x, bt.windowRect.y, bt); parent.nodes.Add(node); if (selectedNode != null) { TransitionGUI transition = ScriptableObject.CreateInstance <TransitionGUI>(); transition.InitTransitionGUI(parent, selectedNode, node); parent.transitions.Add(transition); selectedNode = node; } break; case nameof(UtilitySystem): UtilityNode utilNode = ScriptableObject.CreateInstance <UtilityNode>(); utilNode.InitUtilityNode(parent, utilityType.Action, bt.windowRect.position.x, bt.windowRect.position.y, bt); parent.nodes.Add(utilNode); break; } } return(bt); }