Unity 单例模式 (跳转场景数据依然可用)

using System;
using UnityEngine;

public class GameManager : MonoBehaviour
{

    private static GameManager _instance;
    public static GameManager Instance
    {
        get { return ObjectInstance.Create(ref _instance); }
    }
}

public class ObjectInstance
{
    public static T Create<T>(ref T instance)
    {
        if (instance == null)
        {
            instance = Activator.CreateInstance<T>();
        }
        return instance;
    }
}
发布了40 篇原创文章 · 获赞 36 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/yzx5452830/article/details/81906469