コード例 #1
0
    void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            instance = this;
        }

        DontDestroyOnLoad(this.gameObject);
    }
コード例 #2
0
    /// <summary>
    /// Use singleton pattern to keep music playing between scenes.
    /// </sumnary>
    void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            instance = this;
        }

        DontDestroyOnLoad(this.gameObject);

        /* Keep music playing on a not beauty way. Change it to use singleton in the future!
         * GameObject[] objs = GameObject.FindGameObjectsWithTag("Music");
         * if(objs.Length > 1)
         *      Destroy(this.gameObject);
         * DontDestroyOnLoad(this.gameObject); */
    }