泛型单例
Posted kingbook
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了泛型单例相关的知识,希望对你有一定的参考价值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class BaseApp<T>:MonoBehaviour where T:class,new(){
protected static T _instance;
public static T getInstance(){
return _instance;
}
private void Awake(){
_instance=this as T;
}
private void OnDestroy(){
_instance=null;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App:BaseApp<App>{
public void sayHello(){
Debug.Log("Hello");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test:MonoBehaviour {
private void Start(){
App.getInstance().sayHello();//output: Hello
}
}
以上是关于泛型单例的主要内容,如果未能解决你的问题,请参考以下文章