ABOUT ME

Today
Yesterday
Total
  • Resource Manager
    유니티 2022. 2. 3. 16:42

    1. Resources 폴더

    프리펩을 포함한 모든 리소소들은 Resources 폴더를 만들어서 관리하는 것이 편하다.

    그리고 Resources폴더를 만들면 Resource클래스를 이용하여 필요한 리소스들을 코드로 불러 올 수 있다.

     

     

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ResourceManager
    {
        public T Load<T>(string path) where T : Object
        {
            return Resources.Load<T>(path);
        }
    
        public GameObject Instantiate(string path, Transform parent = null)
        {
            GameObject prefab = Load<GameObject>($"Prefabs/{path}");
            if( prefab == null)
            {
                Debug.Log($"Failed to load prefab : {path}");
                return null;
            }
    
            return Object.Instantiate(prefab, parent);
        }
    
        public void Destroy(GameObject go)
        {
            if (go == null)
                return;
            Object.Destroy(go);
        }
    
    }

    기존의 Instantiate와 Destroy를 쓰면 오류가 났을 때 추적이 어렵기 때문에 Resourece Manager로 래핑작업을 해주었다.

    '유니티' 카테고리의 다른 글

    Collision과 Trigger  (0) 2022.02.03
    RigidBody와 Collider  (0) 2022.02.03
    프리펩(Prefab)  (0) 2022.02.03
    InputManager  (0) 2022.02.03
    플레이어 설정  (0) 2022.02.03
Designed by Tistory.