public string SaveCanvas(string path) { //TODO better way than try catch block AND change to return bool? try { if (nodes == null) { nodes = new List <Node>(); } if (connections == null) { connections = new List <Connection>(); } EditorSaveObject save = BuildSaveObject(); AssetDatabase.CreateAsset(save, path); } catch (Exception e) { return(e.Message); } return(null); }
public string LoadCanvas(string path) { //SAME AS SAVE CANVAS try { //EditorSaveObject load = Resources.Load(path) as EditorSaveObject; EditorSaveObject load = AssetDatabase.LoadAssetAtPath(path, typeof(EditorSaveObject)) as EditorSaveObject; //build new CP / Index List <ConnectionPoint> CPIndex = new List <ConnectionPoint>(); for (int i = 0; i < load.NumberOfCP; i++) { CPIndex.Add(new ConnectionPoint()); } //build nodes int spent = 0; //tracks index of used CP nodes = new List <Node>(); for (int i = 0; i < load.nodeinfos.Count; i++) { Type t = Type.GetType(load.nodeinfos[i].type); ConstructorInfo ctor = t.GetConstructor(new[] { GetType(), typeof(NodeInfo) }); Node n = (Node)Convert.ChangeType(ctor.Invoke(new object[] { this, load.nodeinfos[i] }), t); n.Rebuild(CPIndex.GetRange(spent, load.NodeCPIndex[i])); spent += load.NodeCPIndex[i]; AddNode(n); } //build connections connections = new List <Connection>(); for (int i = 0; i < load.ConnectionIndexIn.Count; i++) { connections.Add(new Connection(CPIndex[load.ConnectionIndexIn[i]], CPIndex[load.ConnectionIndexOut[i]], RemoveConnection)); } offset = new Vector2(load.offset.x, load.offset.y); drag = Vector2.zero; } catch (Exception e) { return(e.Message); } return(null); }
private EditorSaveObject BuildSaveObject() { //Build CP array and Node CP index for reference List <ConnectionPoint> CPArray = new List <ConnectionPoint>(); List <int> NodeCPIndex = new List <int>(); for (int i = 0; i < nodes.Count; i++) { List <ConnectionPoint> t = nodes[i].GetConnectionPoints(); CPArray.AddRange(t); NodeCPIndex.Add(t.Count); } //Build Connection Reference Index List <int> ConnectionIndexIn = new List <int>(); List <int> ConnectionIndexOut = new List <int>(); for (int i = 0; i < connections.Count; i++) { ConnectionIndexIn.Add(CPArray.IndexOf(connections[i].inPoint)); ConnectionIndexOut.Add(CPArray.IndexOf(connections[i].outPoint)); } //Build Node Info List <NodeInfo> nodeinfos = new List <NodeInfo>(); for (int i = 0; i < nodes.Count; i++) { nodeinfos.Add(nodes[i].GetInfo()); } //Return Save Object EditorSaveObject save = CreateInstance <EditorSaveObject>(); save.init(nodeinfos, NodeCPIndex, ConnectionIndexIn, ConnectionIndexOut, CPArray.Count, offset); return(save); }