您当前的位置: 首页 >  unity

云小川

暂无认证

  • 5浏览

    0关注

    78博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity 单例类模板+对象池的简单使用(记录方便拷贝使用)

云小川 发布时间:2022-01-20 13:19:20 ,浏览量:5

一.单例类模板

using UnityEngine;
/// 
/// 通用Mono单例模板
/// 
/// 
public abstract class MonoSingleton : MonoBehaviour where T : MonoSingleton
{
    private static T ms_instance;

    public static T Instance
    {
        get
        {
            if (ms_instance == null)
            {
                ms_instance = Instantiate();
            }

            return ms_instance;
        }
    }

    protected static T Instantiate()
    {
        if (ms_instance != null) return ms_instance;

        // 在场景中查找T类型的Mono类
        ms_instance = (T)FindObjectOfType(typeof(T));
        if (FindObjectsOfType(typeof(T)).Length > 1)
        {
            return ms_instance;
        }

        if (ms_instance != null) return ms_instance;

        // 创建GameObject实例
        var singleton = new GameObject("[Singleton]" + typeof(T).Name);
        Do
关注
打赏
1663746399
查看更多评论
立即登录/注册

微信扫码登录

0.0333s