泛型单例

时间:2019-05-27 12:59:28   收藏:0   阅读:91
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
    }
}

原文:https://www.cnblogs.com/kingBook/p/10930017.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!