private void Awake() { if (!Application.isPlaying) { DestructionWindow.OpenWindow("A self creating scriptable object singleton should not have an instance on editor time, it will now be destroyed ", new Object[] { this }); } }
/// <summary> /// Called when this object is inspected through the OnInspectGameObject editor script, this has to be private /// </summary> private void OnInspect() { if (!Application.isPlaying) { var instancesOfMytype = Resources.LoadAll <T>("Singletons"); if (instancesOfMytype.Length > 1) { var toBeDestroyed = new System.Collections.Generic.List <Object>(); for (int i = 0; i < instancesOfMytype.Length; i++) { if (instancesOfMytype[i].GetInstanceID() != GetInstanceID()) { toBeDestroyed.Add(instancesOfMytype[i]); } } DestructionWindow.OpenWindow("There shouldn't be multiple singletons in a hierarchy on a resource singleton object, it will now be destroyed on: ", toBeDestroyed.ToArray()); } if (this != null && gameObject != null) { for (int i = 0; i < UnityEditor.SceneManagement.EditorSceneManager.sceneCount; i++) { var activeScene = UnityEditor.SceneManagement.EditorSceneManager.GetSceneAt(i); var gobs = activeScene.GetRootGameObjects(); for (int t = 0; t < gobs.Length; t++) { if (gobs[t] == gameObject) { Debug.LogError("The resource singletons should be in the Resources Singletons folder please move: " + gameObject.name + " to the folder"); } foreach (Transform item in gobs[t].transform) { if (item.gameObject == gameObject) { Debug.LogError("The resource singletons should be in the Resources Singletons folder please move: " + gameObject.name + " to the folder"); } } } if (GetComponentsInChildren <ISingleton>().Length > 1) { DestructionWindow.OpenWindow("There shouldn't be multiple singletons in a hierarchy on a resource singleton object, it will now be destroyed on: ", new Object[] { this }); } } } } }
/// <summary> /// Called when inspecting a gameobject /// </summary> private void OnInspect() { if (!Application.isPlaying) { var isAsset = true; if (this != null && gameObject != null) { for (int i = 0; i < UnityEditor.SceneManagement.EditorSceneManager.sceneCount; i++) { var activeScene = UnityEditor.SceneManagement.EditorSceneManager.GetSceneAt(i); var gobs = activeScene.GetRootGameObjects(); for (int t = 0; t < gobs.Length; t++) { if (gobs[t] == gameObject) { isAsset = false; } foreach (Transform item in gobs[t].transform) { if (item.gameObject == gameObject) { isAsset = false; } } } if (GetComponentsInChildren <ISingleton>().Length > 1) { DestructionWindow.OpenWindow("There shouldn't be multiple singletons in a hierarchy on a resource singleton object, it will now be destroyed on: ", new Object[] { this }); } } } if (isAsset) { DestructionWindow.OpenWindow("This is a scene singleton and should only be in a scene: ", new Object[] { gameObject }); } } }
/// <summary> /// Called by the OnInspectSOInspector editor script, this method has to be private /// </summary> private void OnInspect() { if (!Application.isPlaying) { var instancesOfMytype = Resources.LoadAll <T>("Singletons"); if (instancesOfMytype.Length > 1) { var toBeDestroyed = new System.Collections.Generic.List <Object>(); for (int i = 0; i < instancesOfMytype.Length; i++) { if (instancesOfMytype[i].GetInstanceID() != GetInstanceID()) { toBeDestroyed.Add(instancesOfMytype[i]); } } DestructionWindow.OpenWindow("There shouldn't be multiple singletons in a hierarchy on a resource singleton object, it will now be destroyed on: ", toBeDestroyed.ToArray()); } } }
/// <summary> /// Method called by the unity editor when this object is added to a game object /// </summary> private void Reset() { DestructionWindow.OpenWindow("A self creating singleton can't be added to a gameobject on edit time, it will now be destroyed on: ", new Object[] { this }); }