1
This commit is contained in:
38
unity/Assets/Script/Tools/Utils/MonoSingleton.cs
Normal file
38
unity/Assets/Script/Tools/Utils/MonoSingleton.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
|
||||
{
|
||||
public bool global=true;
|
||||
private static T instance;
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (instance == null)
|
||||
instance = FindObjectOfType<T>();
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
void Awake()
|
||||
{
|
||||
if (global)
|
||||
{
|
||||
if (instance != null && instance != this.gameObject.GetComponent<T>())
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
return;
|
||||
}
|
||||
DontDestroyOnLoad(this.gameObject);
|
||||
instance = this.gameObject.GetComponent<T>();
|
||||
|
||||
}
|
||||
this.OnStart();
|
||||
}
|
||||
|
||||
protected virtual void OnStart()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user