add StartupSingleton

This commit is contained in:
in0finite 2021-09-07 00:04:15 +02:00
parent fdd15b81b5
commit 3081db59ad
2 changed files with 50 additions and 0 deletions

View file

@ -0,0 +1,39 @@
using System;
using UnityEngine;
namespace SanAndreasUnity.Utilities
{
public class StartupSingleton<T> : MonoBehaviour
where T : StartupSingleton<T>
{
public static T Singleton { get; private set; }
private void Awake()
{
if (Singleton != null)
{
throw new Exception($"Awake() method called twice for singleton of type {this.GetType().Name}");
}
Singleton = (T) this;
this.OnSingletonAwake();
}
protected virtual void OnSingletonAwake()
{
}
private void Start()
{
if (this != Singleton)
return;
this.OnSingletonStart();
}
protected virtual void OnSingletonStart()
{
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 43e2ad434a43a4a4f8369aee19fed4b4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: