Unity&C#/Unity

[Unity] Resources.UnloadAsset()

리네엔 2024. 10. 16. 21:51

Resources.UnloadAsset()

  • 로드된 에셋을 언로드 하는 메소드
  • 이미 인스턴스화된 게임 오브젝트에는 영향을 미치지 않음.
public class UIManager
{
    private Dictionary<string,GameObject> _cache;
    // 싱글톤 초기화
    public void OpenPopupUI(string path)
    {
        if(!_cache.ContainKey(path))
        {
            _cache.add(string,GameManager.Instantiate(Resources.Load<GameObject>($"Prefabs/{path}"));
        }
        /*캐쉬에서 object를 꺼내서 intantiate시키는 코드*/
    }
    public void ClearCache()
    {
        foreach(var key in _cache.getkeys)
        {
            Resources.UnloadAsset(_cache[key]);
        }
    }
}
  • 이렇게 Instantiate한 GameObject를 Unload하는 것은 안됨.
  • 이는 Destroy()를 활용해야함

'Unity&C# > Unity' 카테고리의 다른 글

[Unity] Input system - multitap value issue  (0) 2024.10.15
[Unity 2D] Sprite Flip  (0) 2024.10.14
[Unity] Frame  (0) 2024.10.14
data path  (0) 2023.09.25
[input system] 작성중  (0) 2022.09.20