Pull up singleton behaviour.

This commit is contained in:
2023-11-29 13:12:19 +08:00
parent 522699152b
commit e339ec3e4c
7 changed files with 48 additions and 36 deletions

View File

@@ -0,0 +1,25 @@
using UnityEngine;
namespace Cryville.Common.Unity {
public abstract class SingletonBehaviour<TSelf> : MonoBehaviour where TSelf : SingletonBehaviour<TSelf> {
static TSelf s_instance;
public static TSelf Instance {
get {
return s_instance;
}
}
bool _validInstance;
protected virtual void Awake() {
if (s_instance != null) {
Debug.LogErrorFormat("Duplicate singleton behaviour {0}", typeof(TSelf));
Destroy(gameObject);
return;
}
s_instance = (TSelf)this;
_validInstance = true;
}
protected virtual void OnDestroy() {
if (_validInstance) s_instance = null;
}
}
}

View File

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