泛型单例

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

public class MonoSlingleton<T> : MonoBehaviour
    where T : Component
{

    private static T t;
    public static T insitance
    {
        get
        {
            if (t == null)
                t = GameObject.FindObjectOfType
                    (typeof(T)) as T;
            if (t == null)
            {
                GameObject go = new GameObject();
                t = go.AddComponent<T>();
                go.name = t.name;
                DontDestroyOnLoad(go);

            }
            return t;

        }
    }
    public virtual void Init()
    {

    }
}

猜你喜欢

转载自blog.csdn.net/fuliyefly/article/details/78289195