mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-10 14:44:17 +00:00
add StartupSingleton
This commit is contained in:
parent
fdd15b81b5
commit
3081db59ad
2 changed files with 50 additions and 0 deletions
39
Assets/Scripts/Utilities/StartupSingleton.cs
Normal file
39
Assets/Scripts/Utilities/StartupSingleton.cs
Normal 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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Utilities/StartupSingleton.cs.meta
Normal file
11
Assets/Scripts/Utilities/StartupSingleton.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 43e2ad434a43a4a4f8369aee19fed4b4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in a new issue