Abstract generic class derived from MonoBehaviour.
Has lazy initialization with creating instance in runtime.
public class MySingleton : MonoSingleton<MySingleton>
{
// Used instead of Awake()
protected override void Construct() { }
// Used instead of OnDestroy()
protected override void Destruct() { }
public void DoSome()
{
// Do something
}
}public class MyClass
{
public void MyMethod()
{
MySingleton.I.DoSome();
}
}Alternative MonoSingleton instancing
using OlegHcp.SingleScripts;
using UnityEngine;
[Creating]
public class MySingleton : MonoSingleton<MySingleton>
{
// Called on Awake
protected override void Construct() { }
// Called on OnDestroy
protected override void Destruct() { }
private class CreatingAttribute : CreateInstanceAttribute
{
public override void Create()
{
var aaset = Resources.Load(/*prefab path*/);
UnityEngine.Object.Instantiate(asset);
}
}
}Same as MonoSingleton but script have to be saved in scene before using. The instance property just looking for it in scene when called first time.
public class MySingleton : SingleBehaviour<MySingleton>
{
...Same as SingleBehaviour just has rectTransform property
Abstract generic class derived from ScriptableObject.
Has lazy initialization with creating instance in runtime.
Can be modified with CreateInstanceAttribute
Like SingleBehaviour should be loaded manualy. The instance property just looking for it in scene when called first time.