using UnityEditor; using UnityEngine; namespace Utility { /// /// Ein Singleton das von erbt. Ermöglicht es aus einem normalen GameObject ein Singleton zu machen. /// Stellt sicher, dass es nur ein Exemplar von sich selbst gibt. /// /// Der Typ des Singletons. public abstract class MonoBehaviourSingleton : MonoBehaviour, ISerializationCallbackReceiver where T : MonoBehaviourSingleton { private static T _instance; public static T Instance { get => _instance; private set { if (_instance == null) { _instance = value; } else if (_instance != value) { Debug.LogError("Instance already exists. Deleting duplicate."); if (Application.isEditor) { DestroyImmediate(value.gameObject); } else { Destroy(value.gameObject); } } } } /// Wenn du diese methode überlädst, rufe auf jeden Fall base.Awake() auf! protected virtual void Awake() { Instance = (T)this; } public void OnBeforeSerialize() { // Nothing to do here. } public void OnAfterDeserialize() { if (!Application.isEditor) { // The value of Instance is lost after deserialization, so we need to set it again. Instance = (T)this; } } } }