unity 泛型单例

// CSharp单例

public abstract class CSharpSingletion<T> where T : new()

{
    private static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new T();
            }
            return instance;
        }
    }

}

// 继承于monobehavior 的单例写法

public class MyGestureListener : MonoBehaviour
{
    public static MyGestureListener Instance;
    private void Awake()
    {
        Instance = this;

    }

}

猜你喜欢

转载自blog.csdn.net/qq_39097425/article/details/81066455