コード例 #1
0
        /// <summary>
        /// Call in awake or earlier.
        /// </summary>
        protected void SetupSingleton()
        {
            if (_instance == null)
            {
                _instance = (T)this;
            }
            else
            {
#if DEBUG
                Debug.LogError($"Instance of {typeof(T)} already existed!");
#else
                throw SingletonAlreadyExistsException.CreateException(typeof(T));
#endif
                return;
            }
        }
コード例 #2
0
        /// <summary>
        /// Call in awake or earlier.
        /// </summary>
        protected void SetupSingleton(bool DontDestroyOnLoad = false)
        {
            if (_instance == null)
            {
                _instance = (T)this;
            }
            else
            {
#if DEBUG
                Debug.LogError($"Instance of {typeof(T)} already existed!");
#else
                throw SingletonAlreadyExistsException.CreateException(typeof(T));
#endif
                Destroy(gameObject);
                return;
            }
            if (DontDestroyOnLoad)
            {
                SetProtected();
            }
        }