feat: initial commit
This commit is contained in:
12
Assets/Common/Infrastructure/Runtime/DontDestroyOnLoad.cs
Normal file
12
Assets/Common/Infrastructure/Runtime/DontDestroyOnLoad.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace JoeSiu.Common.Infrastructure.Runtime
|
||||
{
|
||||
public class DontDestroyOnLoad : MonoBehaviour
|
||||
{
|
||||
private void Awake()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e6bbe0c45eed41ba9edaeb03c27d1af7
|
||||
timeCreated: 1763915394
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "Common.Infrastructure.Runtime",
|
||||
"rootNamespace": "JoeSiu",
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70e478b7eb804446bc79813af716d369
|
||||
timeCreated: 1763912104
|
||||
87
Assets/Common/Infrastructure/Runtime/MonoSingleton.cs
Normal file
87
Assets/Common/Infrastructure/Runtime/MonoSingleton.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace JoeSiu.Common.Infrastructure.Runtime
|
||||
{
|
||||
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
|
||||
{
|
||||
private static T _instance;
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance) return _instance;
|
||||
|
||||
var objs = FindObjectsByType<T>(FindObjectsSortMode.None);
|
||||
|
||||
if (objs.Length > 0)
|
||||
{
|
||||
AssignInstance(objs[0]);
|
||||
}
|
||||
|
||||
if (objs.Length > 1)
|
||||
{
|
||||
for (var index = 1; index < objs.Length; index++)
|
||||
{
|
||||
var obj = objs[index];
|
||||
DestroyDuplicate(obj);
|
||||
}
|
||||
}
|
||||
|
||||
if (!_instance)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"[MonoSingleton] No instance of type '{typeof(T).Name}' is found, make sure to add it in the scene!");
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public static T GetInstance() => _instance;
|
||||
|
||||
public static void CreateInstance(bool dontDestroyOnLoad = true)
|
||||
{
|
||||
var go = new GameObject(typeof(T).Name);
|
||||
var instance = go.AddComponent<T>();
|
||||
if (dontDestroyOnLoad) DontDestroyOnLoad(go);
|
||||
Debug.Log(
|
||||
$"[MonoSingleton] Created game object '{go.name}' {(dontDestroyOnLoad ? "(DontDestroyOnLoad)" : "")} for type '{typeof(T).Name}'",
|
||||
go);
|
||||
AssignInstance(instance);
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
public static void RemoveInstance()
|
||||
{
|
||||
_instance = null;
|
||||
Debug.Log($"[MonoSingleton] Removed instance of type '{typeof(T).Name}'");
|
||||
}
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
if (!_instance) AssignInstance(this as T);
|
||||
else if (_instance != this) DestroyDuplicate(this as T);
|
||||
}
|
||||
|
||||
private static void AssignInstance(T instance)
|
||||
{
|
||||
if (!instance) return;
|
||||
_instance = instance;
|
||||
Debug.Log($"[MonoSingleton] Assigned instance of type '{typeof(T).Name}' to object '{instance.gameObject.name}'",
|
||||
instance.gameObject);
|
||||
}
|
||||
|
||||
private static void DestroyDuplicate(T instance)
|
||||
{
|
||||
if (!_instance) return;
|
||||
if (!instance) return;
|
||||
|
||||
Debug.LogError(
|
||||
$"[MonoSingleton] Instance of type '{typeof(T).Name}' is already assigned to object '{_instance.gameObject.name}', destroying duplicate instance '{instance.gameObject.name}'...",
|
||||
instance.gameObject);
|
||||
|
||||
if (Application.isPlaying) Destroy(instance);
|
||||
else DestroyImmediate(instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7fdcac9d0a5a415c8fb702b32809e649
|
||||
timeCreated: 1763913277
|
||||
@@ -0,0 +1,57 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace JoeSiu.Common.Infrastructure.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// <see href="https://youtu.be/6kWUGEQiMUI"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class ScriptableObjectSingleton<T> : ScriptableObject where T : ScriptableObjectSingleton<T>
|
||||
{
|
||||
private static T _instance;
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance != null) return _instance;
|
||||
|
||||
var assets = Resources.LoadAll<T>("");
|
||||
|
||||
if (assets.Length > 0)
|
||||
{
|
||||
AssignInstance(assets[0]);
|
||||
}
|
||||
|
||||
if (assets.Length > 1)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"[ScriptableObjectSingleton] More than 1 instance of Singleton Scriptable Object of type: {typeof(T)} found");
|
||||
}
|
||||
|
||||
if (!_instance)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"[ScriptableObjectSingleton] No instance of type '{typeof(T).Name}' is found, make sure to add it to the Resources folder!");
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public static T GetInstance() => _instance;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
public static void RemoveInstance()
|
||||
{
|
||||
_instance = null;
|
||||
Debug.Log($"[ScriptableObjectSingleton] Removed instance of type '{typeof(T).Name}'");
|
||||
}
|
||||
|
||||
private static void AssignInstance(T instance)
|
||||
{
|
||||
if (!instance) return;
|
||||
_instance = instance;
|
||||
Debug.Log($"[ScriptableObjectSingleton] Assigned instance of type '{typeof(T).Name}' to object '{instance.name}'", instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2616e8d08c645498cb1486552014786
|
||||
timeCreated: 1763915451
|
||||
Reference in New Issue
Block a user