48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
|
using UnityEngine;
|
|||
|
|
|||
|
namespace Utility
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Ein Singleton das von <see cref="MonoBehaviour"/> erbt. Ermöglicht es aus einem normalen GameObject ein Singleton zu machen.
|
|||
|
/// Stellt sicher, dass es nur ein Exemplar von sich selbst gibt.
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T">Der Typ des Singletons.</typeparam>
|
|||
|
public abstract class MonoBehaviourSingleton<T> : MonoBehaviour, ISerializationCallbackReceiver where T : MonoBehaviourSingleton<T>
|
|||
|
{
|
|||
|
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.");
|
|||
|
Destroy(value.gameObject);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <remarks>Wenn du diese methode überlädst, rufe auf jeden Fall base.Awake() auf!</remarks>
|
|||
|
protected virtual void Awake()
|
|||
|
{
|
|||
|
Instance = (T)this;
|
|||
|
}
|
|||
|
|
|||
|
public void OnBeforeSerialize()
|
|||
|
{
|
|||
|
// Nothing to do here.
|
|||
|
}
|
|||
|
|
|||
|
public void OnAfterDeserialize()
|
|||
|
{
|
|||
|
// The value of Instance is lost after deserialization, so we need to set it again.
|
|||
|
Instance = (T)this;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|