Unity不销毁的单例写法

2021/09/07 Unity3D 共 261 字,约 1 分钟
康

Unity不销毁的单例写法


    // Use this for initialization
    void Awake()
    {

        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(this);
            name = "UserManager";

        }
        else if (this != instance)
        {
            string sceneName = SceneManager.GetActiveScene().name;
            Debug.Log("删除场景" + sceneName + "的" + name);
            Destroy(gameObject);
        }
    }

image

文档信息

Search

    Table of Contents