public BTGraphNode CreateBTNode(BTGraphNodeData nodeData) { BTGraphNode node = new BTGraphNode(nodeData.isRootNode) { title = nodeData.title }; if (!nodeData.isRootNode) { Port inputPort = node.InstantiatePort(Orientation.Horizontal, Direction.Input, Port.Capacity.Multi, typeof(bool)); inputPort.portName = "Input"; node.inputContainer.Add(inputPort); } else { if (RootNode != null) { RemoveElement(RootNode); } node.title = "Root"; RootNode = node; AddElement(RootNode); } Port outputPort = node.InstantiatePort(Orientation.Horizontal, Direction.Output, Port.Capacity.Multi, typeof(bool)); outputPort.portName = "Output"; node.outputContainer.Add(outputPort); if (!nodeData.isRootNode) { TextField txtNodeType = new TextField("Type"); txtNodeType.value = nodeData.nodeType; txtNodeType.AddToClassList("nodetype-input"); txtNodeType.RegisterValueChangedCallback(evt => { node.NodeType = evt.newValue; }); node.extensionContainer.Add(txtNodeType); TextField txtNodeTitle = new TextField(); txtNodeTitle.AddToClassList("title-input"); txtNodeTitle.RegisterValueChangedCallback(evt => { node.title = evt.newValue; }); txtNodeTitle.SetValueWithoutNotify(node.title); node.titleContainer.RemoveAt(0); node.titleContainer.Insert(0, txtNodeTitle); } node.RefreshExpandedState(); node.RefreshPorts(); node.SetPosition(new Rect(nodeData.position, MIN_NODE_SIZE)); AddElement(node); return(node); }
private void LoadData(string filePath = null) { if (string.IsNullOrEmpty(filePath)) { filePath = EditorUtility.OpenFilePanel( "Select BehaviorTree Graph data file", Application.dataPath, "asset" ); } if (!string.IsNullOrEmpty(filePath)) { filePath = filePath.Replace(Application.dataPath, "Assets"); BTGraphData data = AssetDatabase.LoadAssetAtPath <BTGraphData>(filePath); if (data != null) { ResetGraphView(); EditorPrefs.SetString(EditorPreference_LastLoadedBehaviorTreeGraph, filePath); lblFilePath.text = filePath; foreach (BTGraphNodeData nodeData in data.nodes) { BTGraphNode restoredNode = graphView.CreateBTNode(nodeData); restoredNode.Id = nodeData.id; restoredNode.title = nodeData.title; restoredNode.NodeType = nodeData.nodeType; graphView.AddElement(restoredNode); } foreach (BTGraphEdgeData edgeData in data.edges) { BTGraphNode fromNode = edgeData.fromId == null ? null : (BTGraphNode)graphView.nodes.ToList().Find((n) => ((BTGraphNode)n).Id == edgeData.fromId); BTGraphNode toNode = edgeData.toId == null ? null : (BTGraphNode)graphView.nodes.ToList().Find((n) => ((BTGraphNode)n).Id == edgeData.toId); if (fromNode != null && toNode != null) { Edge restoredEdge = new Edge() { output = (Port)fromNode.outputContainer.ElementAt(0), input = (Port)toNode.inputContainer.ElementAt(0) }; restoredEdge.input.Connect(restoredEdge); restoredEdge.output.Connect(restoredEdge); graphView.AddElement(restoredEdge); } } } else { EditorPrefs.SetString(EditorPreference_LastLoadedBehaviorTreeGraph, ""); } } }
private void SaveData(string filePath = null) { if (string.IsNullOrEmpty(filePath)) { filePath = EditorUtility.SaveFilePanel( "Save BehaviorTree Graph data file", Application.dataPath, "New BehaviorTree Graph", "asset" ); } if (!string.IsNullOrEmpty(filePath)) { BTGraphData data = ScriptableObject.CreateInstance <BTGraphData>(); foreach (Edge edge in graphView.edges.ToList()) { BTGraphNode outputNode = edge.output.node as BTGraphNode; BTGraphNode inputNode = edge.input.node as BTGraphNode; data.edges.Add(new BTGraphEdgeData() { fromId = outputNode.Id, toId = inputNode.Id }); } List <Node> nodes = graphView.nodes.ToList(); for (int i = 0; i < nodes.Count; ++i) { BTGraphNode node = (BTGraphNode)nodes[i]; data.nodes.Add(new BTGraphNodeData() { id = node.Id, isRootNode = node.IsRootNode, title = node.title, nodeType = node.NodeType, position = node.GetPosition().position }); if (node.IsRootNode) { data.rootNode = data.nodes[data.nodes.Count - 1]; } } filePath = filePath.Replace(Application.dataPath, "Assets"); AssetDatabase.CreateAsset(data, filePath); AssetDatabase.SaveAssets(); EditorPrefs.SetString(EditorPreference_LastLoadedBehaviorTreeGraph, filePath); lblFilePath.text = filePath; } }