using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SingletonMonoBehaviour<T> : MonoBehaviour
{
protected static T instance;
public static T Instance
{
get
{
return instance;
}
}
public virtual void OnEnable()
{
instance = GetComponent<T>();
}
public virtual void OnDisable()
{
instance = default(T);
}
}
使用方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : SingletonMonoBehaviour<Test>
{
public bool check;
void Start()
{
Test.instance.check = true;
}
// Update is called once per frame
void Update()
{
}
}