public string LoadCanvas(string iPath) { //SAME AS SAVE CANVAS try { //EditorSaveObject load = Resources.Load(path) as EditorSaveObject; EditorSaveNodeData aLoad = AssetDatabase.LoadAssetAtPath(iPath, typeof(EditorSaveNodeData)) as EditorSaveNodeData; //build new CP / Index List <NodeConnectionPoint> CPIndex = new List <NodeConnectionPoint>(); for (int i = 0; i < aLoad.NumberOfCP; i++) { CPIndex.Add(new NodeConnectionPoint()); } //build Nodes int spent = 0; //tracks index of used CP Nodes = new List <NodeBase>(); for (int i = 0; i < aLoad.NodeDatas.Count; i++) { Type t = Type.GetType(aLoad.NodeDatas[i].Type); ConstructorInfo ctor = t.GetConstructor(new[] { GetType(), typeof(NodeData) }); NodeBase aNode = (NodeBase)Convert.ChangeType(ctor.Invoke(new object[] { this, aLoad.NodeDatas[i] }), t); aNode.Rebuild(CPIndex.GetRange(spent, aLoad.NodeCPIndex[i])); spent += aLoad.NodeCPIndex[i]; AddNode(aNode); } //build Connections Connections = new List <NodeConnection>(); for (int i = 0; i < aLoad.ConnectionIndexIn.Count; i++) { Connections.Add(new NodeConnection(CPIndex[aLoad.ConnectionIndexIn[i]], CPIndex[aLoad.ConnectionIndexOut[i]], RemoveConnection)); } mOffset = new Vector2(aLoad.offset.x, aLoad.offset.y); mDrag = Vector2.zero; } catch (Exception e) { return(e.Message); } return(null); }
//******************************************************************************** // Tool Bar Functions //******************************************************************************** #region Tool Bar Functions public string SaveCanvas(string iPath) { //TODO better way than try catch block AND change to return bool? try { if (Nodes == null) { Nodes = new List <NodeBase>(); } if (Connections == null) { Connections = new List <NodeConnection>(); } EditorSaveNodeData aSave = BuildSaveObject(); AssetDatabase.CreateAsset(aSave, iPath); } catch (Exception e) { return(e.Message); } return(null); }
private EditorSaveNodeData BuildSaveObject() { //Build CP array and Node CP index for reference List <NodeConnectionPoint> CPArray = new List <NodeConnectionPoint>(); List <int> NodeCPIndex = new List <int>(); for (int i = 0; i < Nodes.Count; i++) { List <NodeConnectionPoint> 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 <NodeData> aNodeDatas = new List <NodeData>(); for (int i = 0; i < Nodes.Count; i++) { aNodeDatas.Add(Nodes[i].GetInfo()); } //Return Save Object EditorSaveNodeData save = CreateInstance <EditorSaveNodeData>(); save.init(aNodeDatas, NodeCPIndex, ConnectionIndexIn, ConnectionIndexOut, CPArray.Count, mOffset); return(save); }