Unity/TopdownEngine

Scene ์ด๋™ ๊ฐ„์— ์œ ์ง€๋˜๋Š” GameObject

lipnus 2023. 3. 10. 23:19
๋ฐ˜์‘ํ˜•

๐Ÿงฐ MMPersistentSingleton์„ ์ƒ์†ํ•œ ํด๋ž˜์Šค์˜ Instace๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉด ์ž๋™์œผ๋กœ DontDestroy์˜์—ญ์— ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋จ

 

๐Ÿงฐ ๊ทธ๋ƒฅ MMSingleton ํด๋ž˜์Šค๋Š” DontDestroyOnLoad ์—†์Œ

 

GameManager.cs

public class GameManager : MMPersistentSingleton<GameManager>,

 

void Awake()
{
    GameManager.Instance.MaximumLives = 1;
}

์ด๋ ‡๊ฒŒ ์ž๋™์œผ๋กœ DontDestroyOnLoad์˜์—ญ์— ์ƒ์„ฑ

 

MMPersistentSingleton.cs

	public class MMPersistentSingleton<T> : MonoBehaviour	where T : Component
	{   
        protected virtual void Awake ()
        {
            InitializeSingleton();
        }

        protected virtual void InitializeSingleton()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            if (AutomaticallyUnparentOnAwake)
            {
                this.transform.SetParent(null);
            }

            if (_instance == null)
            {
                //If I am the first instance, make me the Singleton
                _instance = this as T;
                DontDestroyOnLoad (transform.gameObject);
                _enabled = true;
            }
            else
            {
                //If a Singleton already exists and you find
                //another reference in scene, destroy it!
                if(this != _instance)
                {
                    Destroy(this.gameObject);
                }
            }
        }
        
        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = FindObjectOfType<T> ();
                    if (_instance == null)
                    {
                        GameObject obj = new GameObject ();
                        obj.name = typeof(T).Name + "_AutoCreated";
                        _instance = obj.AddComponent<T> ();
                    }
                }
                return _instance;
            }
        }
        
        ...

 

๐Ÿ’Ž MMPersistantSingleton ๋งŒ ์ƒ์†ํ•˜๋ฉด DontDestroyOnLoad ์˜์—ญ์— ์ƒ๊น€

 

 

Character์˜ ์”ฌ๊ฐ„ ๋ฐ์ดํ„ฐ ์œ ์ง€

Character๊ฐ€ ๋“ค์–ด์žˆ๋Š” GameObject์— CharacterPersistence ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์œผ๋ฉด ๋จ

 

public class CharacterPersistence : CharacterAbility, MMEventListener<MMGameEvent>, MMEventListener<TopDownEngineEvent>
{
    public bool Initialized { get; set; }

    protected override void Initialization()
    {
        base.Initialization();

        if (AbilityAuthorized)
        {
            DontDestroyOnLoad(this.gameObject);
        }

        Initialized = true;
    }

    public virtual void OnMMEvent(MMGameEvent gameEvent)
    {
        if (gameEvent.EventName == "Save")
        {
            SaveCharacter();
        }
    }

    protected virtual void SaveCharacter()
    {
        if (!AbilityAuthorized)
        {
            return;
        }
        GameManager.Instance.PersistentCharacter = _character;
    }
    
    ...

 

 

์ฃผ์ธ๊ณต์˜ Persistent ํ•ด์ œ

GameOver์— ๋œจ๋Š” ์žฌ์‹œ์ž‘๋ฉ”๋‰ด์—์„œ ์‹คํ–‰๋˜๋Š” ์ปดํฌ๋„ŒํŠธ

 

LevelSelector.cs

protected virtual void LoadScene(string newSceneName)
{
    if (DestroyPersistentCharacter)
    {
        GameManager.Instance.DestroyPersistentCharacter();
    }

    if (GameManager.Instance.Paused)
    {
        TopDownEngineEvent.Trigger(TopDownEngineEventTypes.UnPause, null);
    }

    if (DoNotUseLevelManager)
    {
        MMAdditiveSceneLoadingManager.LoadScene(newSceneName);    
    }
    else
    {
        LevelManager.Instance.GotoLevel(newSceneName);   
    }
}

 

 

GameManager.Instance.DestroyPersistentCharacter();

์—ฌ๊ธฐ๋ถ€๋ถ„์—์„œ ์ปจํŠธ๋กค

 

 

GameManager.cs

public virtual void DestroyPersistentCharacter()
{
   if (PersistentCharacter != null)
   {
      Destroy(PersistentCharacter.gameObject);
      SetPersistentCharacter(null);
   }
   

   if (LevelManager.Instance.Players[0] != null)
   {
      if (LevelManager.Instance.Players[0].gameObject.MMGetComponentNoAlloc<CharacterPersistence>() != null)
      {
         Destroy(LevelManager.Instance.Players[0].gameObject);  
      }
   }
}

์ง์ ‘ Destroy์‹œ์ผœ์ฃผ๋ฉด DontDestroyOnLoad์—์„œ ์—†์–ด์ง

๋ฐ˜์‘ํ˜•