public bool LoadGraphData() { ClearGraphData(); m_NoSelectLabel.visible = false; m_GraphView.visible = true; if (m_GameLogicData == null || m_GameLogicData.m_graphNodes.Count == 0) { return(false); } // create the nodes List <Node> createdNodes = new List <Node>(); for (int i = 0; i < m_GameLogicData.m_graphNodes.Count; ++i) { Node node = DeserializeNode(m_GameLogicData.m_graphNodes[i]); createdNodes.Add(node); m_GraphView.AddElement(node); } //connect the nodes for (int i = 0; i < m_GameLogicData.m_graphNodes.Count; ++i) { for (int iEdge = 0; iEdge < m_GameLogicData.m_graphNodes[i].m_outputs.Count; ++iEdge) { GameLogicData.GameLogicGraphEdge edge = m_GameLogicData.m_graphNodes[i].m_outputs[iEdge]; Port outputPort = createdNodes[i].outputContainer[edge.m_sourcePort] as Port; Port inputPort = createdNodes[edge.m_targetNode].inputContainer[edge.m_targetPort] as Port; m_GraphView.AddElement(outputPort.ConnectTo(inputPort)); } } return(true); }
public void SaveGraphData() { if (m_GameLogicData == null) { return; } string assetPath = AssetDatabase.GetAssetPath(m_GameLogicData); List <Node> nodes = m_GraphView.nodes.ToList(); GameLogicData graphData = ScriptableObject.CreateInstance <GameLogicData>(); graphData.m_graphNodes = new List <GameLogicData.GameLogicGraphNode>(); for (int i = 0; i < nodes.Count; ++i) { Node currentNode = nodes[i]; Type nodeType = currentNode.userData as Type; GameLogicData.GameLogicGraphNode graphNode = new GameLogicData.GameLogicGraphNode(); if (nodeType == null) { graphNode.m_type = null; graphNode.m_typeData = null; } else { MethodInfo method = nodeType.GetMethod("ExtractExtraData", BindingFlags.Static | BindingFlags.Public); if (method == null) { Debug.LogError("Failed to find method ExtractExtraData()!"); return; } graphNode.m_type = nodeType.FullName; graphNode.m_typeData = (string)method.Invoke(null, new object[] { currentNode }); } graphNode.m_title = currentNode.title; graphNode.m_position = currentNode.GetPosition().position; graphNode.m_outputs = new List <GameLogicData.GameLogicGraphEdge>(); foreach (Port port in currentNode.outputContainer) { foreach (Edge edge in port.connections) { GameLogicData.GameLogicGraphEdge serializedEdge = new GameLogicData.GameLogicGraphEdge(); serializedEdge.m_sourcePort = currentNode.outputContainer.IndexOf(edge.output); serializedEdge.m_targetNode = nodes.IndexOf(edge.input.node); serializedEdge.m_targetPort = edge.input.node.inputContainer.IndexOf(edge.input); graphNode.m_outputs.Add(serializedEdge); } } graphData.m_graphNodes.Add(graphNode); } EditorUtility.CopySerialized(graphData, m_GameLogicData); AssetDatabase.SaveAssets(); m_GameLogicData = AssetDatabase.LoadAssetAtPath <GameLogicData>(assetPath); }