unity中单例模式的简单实现

unity中单例模式的简单实现

看注释,已经很详细了

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameFacate : MonoBehaviour
{
    //声明一个私有的实例
    private static GameFacate _instance;
	// 给一个访问方法
    public static GameFacate Instance
    {
        get { return _instance; }
    }

	//Awake是unity中最先执行的方法
	//一些属性的初始话在里面设置很好
    void Awake()
    {
    	//如果不为空,删除,用以前的instance
    	// 因为之前的实例可能已经进行了一些操作了
        if (_instance != null)
        {
            Destroy(this.gameObject);
            return;
        }
		// 为空,直接返回本身
        _instance = this;
    }

}

发布了201 篇原创文章 · 获赞 210 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_40666620/article/details/104909362