public void OnValidate() { if (Application.isPlaying) { return; } bool isPrefab; #if UNITY_2018_3_OR_NEWER isPrefab = UnityEditor.PrefabUtility.IsPartOfPrefabAsset(this.gameObject); #else isPrefab = this.gameObject.scene.name == null; #endif // Set a new save identification if it is not a prefab at the moment. if (!isPrefab) { Lowscope.Tools.ValidateHierarchy.Add(this); bool isDuplicate = false; Saveable saveable = null; if (sceneName != gameObject.scene.name) { UnityEditor.Undo.RecordObject(this, "Updated Object Scene ID"); if (SaveSettings.Get().resetSaveableIdOnNewScene) { saveIdentification = ""; } sceneName = gameObject.scene.name; } if (SaveSettings.Get().resetSaveableIdOnDuplicate) { // Does the object have a valid save id? If not, we give a new one. if (!string.IsNullOrEmpty(saveIdentification)) { isDuplicate = saveIdentificationCache.TryGetValue(saveIdentification, out saveable); if (!isDuplicate) { if (saveIdentification != "") { saveIdentificationCache.Add(saveIdentification, this); } } else { if (saveable == null) { saveIdentificationCache.Remove(saveIdentification); saveIdentificationCache.Add(saveIdentification, this); } else { if (saveable.gameObject != this.gameObject) { UnityEditor.Undo.RecordObject(this, "Updated Object Scene ID"); saveIdentification = ""; } } } } } if (string.IsNullOrEmpty(saveIdentification)) { UnityEditor.Undo.RecordObject(this, "ClearedSaveIdentification"); int guidLength = SaveSettings.Get().gameObjectGuidLength; #if NET_4_6 saveIdentification = $"{gameObject.scene.name}-{gameObject.name}-{System.Guid.NewGuid().ToString().Substring(0, 5)}"; #else saveIdentification = string.Format("{0}-{1}-{2}", gameObject.scene.name, gameObject.name, System.Guid.NewGuid().ToString().Substring(0, guidLength)); #endif saveIdentificationCache.Add(saveIdentification, this); UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(this.gameObject.scene); } } else { saveIdentification = string.Empty; sceneName = string.Empty; } List <ISaveable> obtainSaveables = new List <ISaveable>(); obtainSaveables.AddRange(GetComponentsInChildren <ISaveable>(true).ToList()); for (int i = 0; i < externalListeners.Count; i++) { if (externalListeners[i] != null) { obtainSaveables.AddRange(externalListeners[i].GetComponentsInChildren <ISaveable>(true).ToList()); } } for (int i = cachedSaveableComponents.Count - 1; i >= 0; i--) { if (cachedSaveableComponents[i].monoBehaviour == null) { cachedSaveableComponents.RemoveAt(i); } } if (obtainSaveables.Count != cachedSaveableComponents.Count) { if (cachedSaveableComponents.Count > obtainSaveables.Count) { for (int i = cachedSaveableComponents.Count - 1; i >= obtainSaveables.Count; i--) { cachedSaveableComponents.RemoveAt(i); } } int saveableComponentCount = cachedSaveableComponents.Count; for (int i = saveableComponentCount - 1; i >= 0; i--) { if (cachedSaveableComponents[i] == null) { cachedSaveableComponents.RemoveAt(i); } } ISaveable[] cachedSaveables = new ISaveable[cachedSaveableComponents.Count]; for (int i = 0; i < cachedSaveables.Length; i++) { cachedSaveables[i] = cachedSaveableComponents[i].monoBehaviour as ISaveable; } ISaveable[] missingElements = obtainSaveables.Except(cachedSaveables).ToArray(); for (int i = 0; i < missingElements.Length; i++) { CachedSaveableComponent newSaveableComponent = new CachedSaveableComponent() { monoBehaviour = missingElements[i] as MonoBehaviour }; string typeString = newSaveableComponent.monoBehaviour.GetType().Name.ToString(); var identifier = ""; while (!IsIdentifierUnique(identifier)) { int guidLength = SaveSettings.Get().componentGuidLength; string guidString = System.Guid.NewGuid().ToString().Substring(0, guidLength); identifier = string.Format("{0}-{1}", typeString, guidString); } newSaveableComponent.identifier = identifier; cachedSaveableComponents.Add(newSaveableComponent); } UnityEditor.EditorUtility.SetDirty(this); UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(this.gameObject.scene); } }
public void OnValidate() { if (Application.isPlaying) { return; } if (GetComponent <IGuidProvider>() == null) { Undo.AddComponent <GuidComponent>(gameObject); } List <ISaveableComponent> obtainSaveables = new List <ISaveableComponent>(); GetComponentsInChildren(true, obtainSaveables); for (int i = 0; i < externalListeners.Count; i++) { if (externalListeners[i] != null) { obtainSaveables.AddRange(externalListeners[i].GetComponentsInChildren <ISaveableComponent>(true) .ToList()); } } for (int i = serializedSaveableComponents.Count - 1; i >= 0; i--) { if (serializedSaveableComponents[i].component == null) { serializedSaveableComponents.RemoveAt(i); } } if (obtainSaveables.Count != serializedSaveableComponents.Count) { if (serializedSaveableComponents.Count > obtainSaveables.Count) { for (int i = serializedSaveableComponents.Count - 1; i >= obtainSaveables.Count; i--) { serializedSaveableComponents.RemoveAt(i); } } int saveableComponentCount = serializedSaveableComponents.Count; for (int i = saveableComponentCount - 1; i >= 0; i--) { if (serializedSaveableComponents[i] == null) { serializedSaveableComponents.RemoveAt(i); } } ISaveableComponent[] cachedSaveables = new ISaveableComponent[serializedSaveableComponents.Count]; for (int i = 0; i < cachedSaveables.Length; i++) { cachedSaveables[i] = serializedSaveableComponents[i].component as ISaveableComponent; } ISaveableComponent[] missingElements = obtainSaveables.Except(cachedSaveables).ToArray(); for (int i = 0; i < missingElements.Length; i++) { CachedSaveableComponent newSaveableComponent = new CachedSaveableComponent() { identifier = GenerateIdForSaveableComponent(missingElements[i]), component = missingElements[i] as Component }; serializedSaveableComponents.Add(newSaveableComponent); } UnityEditor.EditorUtility.SetDirty(this); UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(gameObject.scene); } }