๋ฐ์ํ
๐งฐ MMPersistentSingleton์ ์์ํ ํด๋์ค์ Instace๋ฅผ ๊ฐ์ ธ์ค๋ฉด ์๋์ผ๋ก DontDestroy์์ญ์ ํด๋น ํด๋์ค๊ฐ ์์ฑ๋จ
๐งฐ ๊ทธ๋ฅ MMSingleton ํด๋์ค๋ DontDestroyOnLoad ์์
GameManager.cs
public class GameManager : MMPersistentSingleton<GameManager>,
void Awake()
{
GameManager.Instance.MaximumLives = 1;
}
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 ํด์
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์์ ์์ด์ง
๋ฐ์ํ
'Unity > TopdownEngine' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฌด๊ธฐ ์กฐ์ค ๊ฐ๋ ๋ณ๊ฒฝ (0) | 2023.05.11 |
---|---|
์์ง ์ฃผ์ ๋ผ์ดํ์ฌ์ดํด(LifeCycle) (0) | 2023.05.01 |
Weapon ๋ฅ๋ ฅ์น ๋์ ๋ณ๊ฒฝ ๊ณํ (0) | 2023.02.15 |
์นด๋ฉ๋ผ๊ฐ ์ฃผ์ธ๊ณต์ ๋ฐ๋ผ๊ฐ๊ฒ ๋ง๋ค๊ธฐ (1) | 2023.02.01 |
DialogBox๊ฐ ๋ฉ์ํ๊ฒ ๋์ฌ ๋ ํ์ (0) | 2023.01.30 |