Greatest Time simulation ever

Außerdem:
- Coole Singletons, möglicherweise die geilsten Singletons, die jemals in Unity implementiert wurden
- Utility Klassen nach Utility Verschoben
- Schlaganfallsymptome in Developer entfernt
This commit is contained in:
Simon Lübeß
2024-04-04 19:41:19 +02:00
parent 7947eab66e
commit 80e25e2d81
20 changed files with 353 additions and 145 deletions

View File

@ -0,0 +1,48 @@
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;
}
}
}