mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-12-19 08:53:15 +00:00
35 lines
850 B
C#
35 lines
850 B
C#
|
using System.Linq;
|
|||
|
|
|||
|
namespace UnityEngine
|
|||
|
{
|
|||
|
public abstract class SingletonComponent<TComponent> : MonoBehaviour
|
|||
|
where TComponent : SingletonComponent<TComponent>
|
|||
|
{
|
|||
|
private static TComponent _sInstance;
|
|||
|
|
|||
|
public static TComponent Instance
|
|||
|
{
|
|||
|
get { return _sInstance ?? (_sInstance = FindObjectOfType<TComponent>()); }
|
|||
|
}
|
|||
|
|
|||
|
// ReSharper disable once UnusedMember.Local
|
|||
|
private void Awake()
|
|||
|
{
|
|||
|
if (FindObjectsOfType<TComponent>().Any(x => x != this && x.isActiveAndEnabled))
|
|||
|
{
|
|||
|
DestroyImmediate(this);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
_sInstance = (TComponent)this;
|
|||
|
|
|||
|
DontDestroyOnLoad(gameObject);
|
|||
|
|
|||
|
OnAwake();
|
|||
|
}
|
|||
|
|
|||
|
protected virtual void OnAwake()
|
|||
|
{
|
|||
|
}
|
|||
|
}
|
|||
|
}
|