Skip to content

Saving the graph and loading it  #98

Closed
@NaorHadar

Description

@NaorHadar

So I need to serialize my graph and I haven't seen any documentation of it so I searched through the code and managed to somewhat save the graph.

Note:: I needed to change the source of the plugin a bit (some values turned into public and I think that's it).

What I did is basically saved serializedNodes (type and jsonDatas), edges (inputNode.GUID, outputNode.GUID, inputFieldName, outputFieldName), and for the graph I just used JsonUtility.ToJson(tmpGraph, false); and saved that string.

After that I loaded them up again in OnEnable.

Everything is saved great, but when there is a RelayNode in there, its edges doesn't get saved, any idea what could happen?

Here is the modified source code of the DefaultGraphWindow.cs example, everything is in here, if you want to test it you need to change the plugin source a bit (as mentioned above):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using GraphProcessor;

public class DefaultGraphWindow : BaseGraphWindow
{
	BaseGraph tmpGraph;

	[MenuItem("Window/01 DefaultGraph")]
	public static BaseGraphWindow OpenWithTmpGraph()
	{
		var graphWindow = CreateWindow<DefaultGraphWindow>();

		graphWindow.Show();

		return graphWindow;
	}

	protected override void OnEnable()
	{
		base.OnEnable();

		tmpGraph = ScriptableObject.CreateInstance<BaseGraph>();
		tmpGraph.hideFlags = HideFlags.HideAndDontSave;
		// When the graph is opened from the window, we don't save the graph to disk
		InitializeGraph(tmpGraph);

		int nodeCount = EditorPrefs.GetInt("DefaultGraph_Node_Count", 0);
		if (nodeCount > 0)
		{
			// Nodes
			// If there is nodes, we want to load it
			for (int i = 0; i < nodeCount; i++)
			{
				JsonElement newNode = new JsonElement();
				newNode.type = EditorPrefs.GetString("DefaultGraph_Node_" + i.ToString() + "_type");
				newNode.jsonDatas = EditorPrefs.GetString("DefaultGraph_Node_" + i.ToString() + "_data");
				tmpGraph.serializedNodes.Add(newNode);
			}

			// Edges
			int edgeCount = EditorPrefs.GetInt("DefaultGraph_Edge_Count", 0);
			if (edgeCount > 0)
			{
				string ownerData = EditorPrefs.GetString("DefaultGraph_Owner");
				JsonUtility.FromJsonOverwrite(ownerData, tmpGraph);

				// clear all edges
				tmpGraph.edges.Clear();

				for (int i = 0; i < edgeCount; i++)
				{
					SerializableEdge newEdge = new SerializableEdge();
					newEdge.GUID = EditorPrefs.GetString("DefaultGraph_Edge_" + i.ToString() + "_GUID");
					newEdge.inputNodeGUID = EditorPrefs.GetString("DefaultGraph_Edge_" + i.ToString() + "_inputNodeGUID");
					newEdge.outputNodeGUID = EditorPrefs.GetString("DefaultGraph_Edge_" + i.ToString() + "_outputNodeGUID");
					newEdge.inputFieldName= EditorPrefs.GetString("DefaultGraph_Edge_" + i.ToString() + "_inputFieldName");
					newEdge.outputFieldName = EditorPrefs.GetString("DefaultGraph_Edge_" + i.ToString() + "_outputFieldName");
					newEdge.owner = tmpGraph;

					tmpGraph.edges.Add(newEdge);
				}

				tmpGraph.Deserialize();
				InitializeGraph(tmpGraph);
			}
		}

	}

	protected override void OnDisable()
	{
		base.OnDisable();

		// Nodes
		// No need to delete the count, we always overwrite it
		int prevNodeCount = EditorPrefs.GetInt("DefaultGraph_Node_Count", 0);
		for (int i = 0; i < prevNodeCount; i++)
		{
			EditorPrefs.DeleteKey("DefaultGraph_Node_" + i.ToString() + "_type");
			EditorPrefs.DeleteKey("DefaultGraph_Node_" + i.ToString() + "_data");
		}

		int nodeCount = tmpGraph.serializedNodes.Count;
		EditorPrefs.SetInt("DefaultGraph_Node_Count", nodeCount);
		for (int i = 0; i < nodeCount; i++)
		{
			EditorPrefs.SetString("DefaultGraph_Node_" + i.ToString() + "_type", tmpGraph.serializedNodes[i].type);
			EditorPrefs.SetString("DefaultGraph_Node_" + i.ToString() + "_data", tmpGraph.serializedNodes[i].jsonDatas);
		}

		// Edges
		int prevEdgeCount = EditorPrefs.GetInt("DefaultGraph_Edge_Count", 0);
		for (int i = 0; i < prevEdgeCount; i++)
		{
			EditorPrefs.DeleteKey("DefaultGraph_Edge_" + i.ToString() + "_type");
			EditorPrefs.DeleteKey("DefaultGraph_Edge_" + i.ToString() + "_data");
		}

		string ownerData = JsonUtility.ToJson(tmpGraph, false);
		EditorPrefs.SetString("DefaultGraph_Owner", ownerData);

		int edgesCount = tmpGraph.edges.Count;
		EditorPrefs.SetInt("DefaultGraph_Edge_Count", edgesCount);
		for (int i = 0; i < edgesCount; i++)
		{
			EditorPrefs.SetString("DefaultGraph_Edge_" + i.ToString() + "_GUID", tmpGraph.edges[i].GUID);
			EditorPrefs.SetString("DefaultGraph_Edge_" + i.ToString() + "_inputNodeGUID", tmpGraph.edges[i].inputNode.GUID);
			EditorPrefs.SetString("DefaultGraph_Edge_" + i.ToString() + "_outputNodeGUID", tmpGraph.edges[i].outputNode.GUID);
			EditorPrefs.SetString("DefaultGraph_Edge_" + i.ToString() + "_inputFieldName", tmpGraph.edges[i].inputFieldName);
			EditorPrefs.SetString("DefaultGraph_Edge_" + i.ToString() + "_outputFieldName", tmpGraph.edges[i].outputFieldName);
		}

		Debug.Log("Disable");
	}

	protected override void OnDestroy()
	{
		graphView?.Dispose();
		DestroyImmediate(tmpGraph);
	}

	protected override void InitializeWindow(BaseGraph graph)
	{
		titleContent = new GUIContent("Default Graph");

		if (graphView == null)
			graphView = new BaseGraphView(this);

		rootView.Add(graphView);
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions