void SaveGenerator (Generator gen, Vector2 pos) { if (locked) return; string path= UnityEditor.EditorUtility.SaveFilePanel( "Export Nodes", "", "MapMagicExport.nodes", "nodes"); if (path==null || path.Length==0) return; Generator[] saveGens = SmartCopyGenerators(gen); if (gen!=null) for (int i=0; i<saveGens.Length; i++) saveGens[i].guiRect.position -= gen.guiRect.position; //preparing serialization arrays List<string> classes = new List<string>(); List<UnityEngine.Object> objects = new List<UnityEngine.Object>(); List<object> references = new List<object>(); List<float> floats = new List<float>(); //saving CustomSerialization.WriteClass(saveGens, classes, objects, floats, references); System.IO.StreamWriter writer = new System.IO.StreamWriter(path); writer.Write( CustomSerialization.ExportXML(classes, objects, floats) ); writer.Close(); //AssetDatabase.CreateAsset(saveGens, path); //AssetDatabase.SaveAssets(); }
void SaveGenerator (Generator gen, Vector2 pos, string path=null) //@Chuck: use this similarly to ExportToFile. If path is defined it will export generator using it { if (changeLock) return; if (path==null) path= UnityEditor.EditorUtility.SaveFilePanel( "Export Nodes", "", "MapMagicExport.nodes", "nodes"); if (path==null || path.Length==0) return; Generator[] saveGens = SmartCopyGenerators(gen); if (gen!=null) for (int i=0; i<saveGens.Length; i++) saveGens[i].guiRect.position -= gen.guiRect.position; //preparing serialization arrays List<string> classes = new List<string>(); List<UnityEngine.Object> objects = new List<UnityEngine.Object>(); List<object> references = new List<object>(); List<float> floats = new List<float>(); //saving CustomSerialization.WriteClass(saveGens, classes, objects, floats, references); using (System.IO.StreamWriter writer = new System.IO.StreamWriter(path)) writer.Write(CustomSerialization.ExportXML(classes, objects, floats)); //AssetDatabase.CreateAsset(saveGens, path); //AssetDatabase.SaveAssets(); }
public void OnAfterDeserialize() { //System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); timer.Start(); if (serializedVersion < 10) { Debug.LogError("MapMagic: trying to load unknow version scene (v." + serializedVersion / 10f + "). " + "This may cause errors or drastic drop in performance. " + "Delete this MapMagic object and create the new one from scratch when possible."); } //loading old serializer if (classes.Length == 0 && serializer.entities.Count != 0) { serializer.ClearLinks(); list = (Generator[])serializer.Retrieve(listNum); serializer.ClearLinks(); OnBeforeSerialize(); serializer = null; //will not make it null, just 0-length } List <string> classesList = new List <string>(); classesList.AddRange(classes); List <UnityEngine.Object> objectsList = new List <UnityEngine.Object>(); objectsList.AddRange(objects); List <float> floatsList = new List <float>(); floatsList.AddRange(floats); references.Clear(); list = (Generator[])CustomSerialization.ReadClass(0, classesList, objectsList, floatsList, references); references.Clear(); //timer.Stop(); Debug.Log("Deserialize Time: " + timer.ElapsedMilliseconds + "ms"); }
public static object DeepCopy(object src) { List <string> classes = new List <string>(); List <UnityEngine.Object> objects = new List <UnityEngine.Object>(); List <float> floats = new List <float>(); List <object> saveReferences = new List <object>(); List <object> loadReferences = new List <object>(); int num = CustomSerialization.WriteClass(src, classes, objects, floats, saveReferences); return(CustomSerialization.ReadClass(num, classes, objects, floats, loadReferences)); }
void LoadGenerator (Vector2 pos) { if (MapMagic.instance.guiGens == null) MapMagic.instance.guiGens = MapMagic.instance.gens; string path= UnityEditor.EditorUtility.OpenFilePanel( "Import Nodes", "", "nodes"); if (path==null || path.Length==0) return; //preparing serialization arrays List<string> classes = new List<string>(); List<UnityEngine.Object> objects = new List<UnityEngine.Object>(); List<object> references = new List<object>(); List<float> floats = new List<float>(); //loading System.IO.StreamReader reader = new System.IO.StreamReader(path); CustomSerialization.ImportXML(reader.ReadToEnd(), out classes, out objects, out floats); Generator[] loadedGens = (Generator[])CustomSerialization.ReadClass(0, classes, objects, floats, references); //offset for (int i=loadedGens.Length-1; i>=0; i--) loadedGens[i].guiRect.position += pos; //GeneratorsAsset loadedGens = (GeneratorsAsset)AssetDatabase.LoadAssetAtPath(path, typeof(GeneratorsAsset)); /*for (int i=loadedGens.list.Length-1; i>=0; i--) { //cloning //loadedGens.list[i] = loadedGens.list[i].ReflectionCopy(); Generator gen = loadedGens.list[i]; //offset gen.guiRect.position += pos; //ignoring already existing outputs if (gen is Generator.IOutput && MapMagic.instance.guiGens.GetGenerator(gen.GetType())!=null) { Debug.Log ("MapMagic: tried to load Output which already exists (" + gen + "). Skipping."); loadedGens.UnlinkGenerator(gen); ArrayUtility.RemoveAt(ref loadedGens.list, i); } }*/ ArrayUtility.AddRange(ref MapMagic.instance.guiGens.list, loadedGens); MapMagic.instance.guiGens.ChangeGenerator(null); repaint=true; forceAll=true; Repaint(); }
public GeneratorsAsset ReleaseAsset() { #if UNITY_EDITOR UnityEditor.Undo.RecordObject(MapMagic.instance, "MapMagic Release Data"); MapMagic.instance.setDirty = !MapMagic.instance.setDirty; GeneratorsAsset newData = ScriptableObject.CreateInstance <GeneratorsAsset>(); newData.list = (Generator[])CustomSerialization.DeepCopy(list); return(newData); //UnityEditor.EditorUtility.SetDirty(MapMagic.instance); #else return(null); #endif }
private Generator[] SmartCopyGenerators (Generator gen) { //saving all gens if clicked to background if (gen == null) return (Generator[])CustomSerialization.DeepCopy(MapMagic.instance.guiGens.list); //saving group else if (gen is Group) { Group grp = (Group)gen; Generator[] gens = MapMagic.instance.guiGens.list; //creating a list of group children (started with a group itself) List<Generator> gensList = new List<Generator>(); gensList.Add(gen); for (int g=0; g<gens.Length; g++) if (grp.guiRect.Contains(gens[g].guiRect)) gensList.Add(gens[g]); //copying group children Generator[] copyGens = (Generator[])CustomSerialization.DeepCopy(gensList.ToArray()); //unlinking children from out-of-group generators HashSet<Generator> copyGensHash = new HashSet<Generator>(); for (int g=0; g<copyGens.Length; g++) copyGensHash.Add(copyGens[g]); for (int g=0; g<copyGens.Length; g++) foreach (Generator.Input input in copyGens[g].Inputs()) { if (input.link == null) continue; if (!copyGensHash.Contains(input.linkGen)) input.Unlink(); } return copyGens; } //single generator else { Generator copyGen = (Generator)CustomSerialization.DeepCopy(gen); foreach (Generator.Input input in copyGen.Inputs()) { if (input != null) input.Unlink(); } return new Generator[] { copyGen }; } }
public void OnBeforeSerialize() { //System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); timer.Start(); serializedVersion = MapMagic.version; List <string> classesList = new List <string>(); List <UnityEngine.Object> objectsList = new List <UnityEngine.Object>(); List <float> floatsList = new List <float>(); references.Clear(); CustomSerialization.WriteClass(list, classesList, objectsList, floatsList, references); classes = classesList.ToArray(); objects = objectsList.ToArray(); floats = floatsList.ToArray(); //serializer.Clear(); //listNum = serializer.Store(list); //timer.Stop(); Debug.Log("Serialize Time: " + timer.ElapsedMilliseconds + "ms"); }