使用场景

当需要使用单例模式的时候,如Player类、Manager类,直接继承该方法即可。
大概...会省去10行左右的代码...

代码部分

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseManager<T> : MonoBehaviour where T : BaseManager<T>
{
public static T instance;

private void Awake()
{
    if (instance == null)
    {
        instance = (T)this;
    }
    DontDestroyOnLoad(gameObject);
}

}