private static void GetChildrenForAutoSave(ES2AutoSave autoSave) { foreach (Transform t in autoSave.transform) { ES2AutoSave child = ES2AutoSave.GetAutoSave(t.gameObject); if (child == null) { child = ES2AutoSave.AddAutoSave(t.gameObject, RandomColor(), AutoSaveComponentsAreHidden(), true, ""); } GetChildrenForAutoSave(child); } }
/* * Refreshes the variables and Components for all Auto Saves in this scene. * Should be called after a change is made to the scene, or scripts are recompiled. */ public static void RefreshSceneAutoSaves() { // Only refresh if Easy Save is enabled for this scene. if (mgr == null || EditorApplication.isPlayingOrWillChangePlaymode) { return; } mgr.sceneObjects = new ES2AutoSave[0]; mgr.ids = new HashSet <string>(); Transform[] transforms = Resources.FindObjectsOfTypeAll <Transform>(); // Recurse over top-level scene objects and display their children hierarchically. foreach (Transform t in transforms) { if (t.parent == null && t.hideFlags == HideFlags.None) { ES2AutoSave autoSave = ES2AutoSave.GetAutoSave(t.gameObject); if (autoSave == null) { autoSave = ES2AutoSave.AddAutoSave(t.gameObject, RandomColor(), AutoSaveComponentsAreHidden(), true, ""); if (AutoSaveComponentsAreHidden()) { autoSave.hideFlags = HideFlags.HideInInspector; } } // If a prefab has been added to the scene, give it an ID and treat it as a scene object. if (string.IsNullOrEmpty(autoSave.id)) { autoSave.id = ES2AutoSave.GenerateID(); autoSave.prefabID = ""; } // Check for duplicate IDs. if (mgr.ids.Contains(autoSave.id)) { autoSave.id = ES2AutoSave.GenerateID(); } mgr.ids.Add(autoSave.id); UpdateAutoSave(autoSave); AddAutoSaveToManager(autoSave); GetOrAddChildrenForAutoSave(autoSave); } } }
private static void GetOrAddChildrenForAutoSave(ES2AutoSave autoSave) { if (autoSave == null) { return; } foreach (Transform t in autoSave.transform) { ES2AutoSave child = ES2AutoSave.GetAutoSave(t.gameObject); if (child == null) { child = ES2AutoSave.AddAutoSave(t.gameObject, RandomColor(), AutoSaveComponentsAreHidden(), true, ""); } UpdateAutoSave(child); AddAutoSaveToManager(child); GetOrAddChildrenForAutoSave(child); } }
// Enable Auto Save for a prefab and it's children. public static void EnableAutoSaveForSelectedPrefabRecursive(GameObject prefab) { ES2AutoSave autoSave = prefab.GetComponent <ES2AutoSave>(); // Only add an Auto Save if this prefab doesn't already have one. if (autoSave == null) { // Add an ES2AutoSave to the prefab and add it to the Auto Save manager prefab. autoSave = ES2AutoSave.AddAutoSave(prefab, RandomColor(), AutoSaveComponentsAreHidden(), false, ""); ES2AutoSaveGlobalManager globalManager = globalMgr; // Don't add prefab to prefab array if it's a child prefab. if (autoSave.transform.parent == null) { ArrayUtility.Add(ref globalManager.prefabArray, autoSave); } } foreach (Transform childTransform in prefab.transform) { EnableAutoSaveForSelectedPrefabRecursive(childTransform.gameObject); } }
/* * Reads an Auto Save from a reader. * Returns false if there are no more Auto Saves to load. * Optionally allows you to specify an Auto Save to load it into. */ private bool ReadAutoSave(ES2Reader reader, ES2AutoSave autoSave = null) { // If we've not already read the Auto Save's ID, read it. string autoSaveID = reader.reader.ReadString(); if (autoSaveID == "null") { return(false); } int endPosition = (int)reader.stream.Position; int length = reader.reader.ReadInt32(); endPosition += length; bool isPrefab = reader.reader.ReadBoolean(); string prefabID = isPrefab ? reader.reader.ReadString() : ""; if (autoSave != null) { // If an Auto Save has already been specified, do nothing. // i.e. this happens when loading a child Auto Save. } // Manage loading of scene objects. else if (!isPrefab) { // If no Auto Save exists, create a GameObject and Auto Save for it. if ((autoSave = GetAutoSave(autoSaveID)) == null) { autoSave = ES2AutoSave.AddAutoSave(new GameObject(), Color.red, true, false, autoSaveID); } } // Manage loading of prefabs. else { ES2AutoSave prefabAutoSave = null; ES2AutoSave[] prefabs = globalManager.prefabArray; // TODO: Use Dictionary for performance? for (int i = 0; i < prefabs.Length; i++) { if (prefabs[i] != null && prefabs[i].prefabID == prefabID) { prefabAutoSave = prefabs[i]; break; } } GameObject instance; if (prefabAutoSave == null) { // If Auto Save with ID doesn't exist, create a blank object with a new Auto Save instead. instance = new GameObject(); autoSave = ES2AutoSave.AddAutoSave(instance, Color.clear, true, false, autoSaveID); autoSave.prefabID = prefabID; } else { // If an object with this ID doesn't already exist, instantiate the prefab. if (autoSaves.TryGetValue(autoSaveID, out autoSave)) { instance = autoSave.gameObject; } else { instance = Instantiate(prefabAutoSave.gameObject); autoSave = instance.GetComponent <ES2AutoSave>(); autoSave.id = autoSaveID; AddAutoSave(autoSave, autoSaveID); } } } /* Read Instance Variables. */ // Parent string parentID = reader.reader.ReadString(); if (parentID != "") { if (parentID == "null") { autoSave.transform.SetParent(null, false); } else { ES2AutoSave parentAutoSave; if (!autoSaves.TryGetValue(parentID, out parentAutoSave)) { // Only set parent if the parent actually exists. } else { autoSave.transform.SetParent(parentAutoSave.transform, false); } } } // activeSelf string activeSelf = reader.reader.ReadString(); if (activeSelf == "True") { autoSave.gameObject.SetActive(true); } if (activeSelf == "False") { autoSave.gameObject.SetActive(false); } // name string name = reader.reader.ReadString(); if (name != "") { autoSave.name = name; } // tag string tag = reader.reader.ReadString(); if (tag != "") { autoSave.tag = tag; } // layer int layer = reader.reader.ReadInt32(); if (layer > -1) { autoSave.gameObject.layer = layer; } // If this is a prefab, we'll need to load it's children too. if (autoSave.isPrefab) { List <string> childPrefabIDs = reader.ReadList <string>(); for (int i = 0; i < childPrefabIDs.Count; i++) { bool foundAutoSave = false; // Get the child with the given prefab ID, and load the data into it. foreach (Transform t in autoSave.transform) { ES2AutoSave childAutoSave = t.GetComponent <ES2AutoSave>(); if (childAutoSave != null) { if (childAutoSave.prefabID == childPrefabIDs[i]) { foundAutoSave = true; ReadAutoSave(reader, childAutoSave); break; } } } // If we didn't find an appropriate Auto Save, it's likely that this has been made a child of // the prefab at runtime, so load it as a normal auto save. if (!foundAutoSave) { ReadAutoSave(reader, null); /* * // Skip the Auto Save. * reader.reader.ReadString(); * int endPos = (int)reader.stream.Position; * int len = reader.reader.ReadInt32(); * endPos += len; * reader.stream.Position = endPos;*/ } } } while (reader.stream.Position != endPosition) { ReadComponent(autoSave, reader); } return(true); }