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);
}
}
기존 Object 객체에서 사용되는 함수들을 Monobehaviour 없이 구현하기 위해(Managers 스크립트에서 new를 통해 인스턴스화 하기 위해) 새로 구현을 한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Managers : MonoBehaviour
{
static Managers s_Instance;
static Managers Instance { get { Init(); return s_Instance; } }
InputManager _input = new InputManager();
ResourceManager _resource = new ResourceManager();
public static InputManager Input { get { return Instance._input; } }
public static ResourceManager Resource { get { return Instance._resource;} }
// Start is called before the first frame update
void Start()
{
Init();
}
// Update is called once per frame
void Update()
{
_input.OnUpdate();
}
static void Init()
{
if (s_Instance == null)
{
GameObject go = GameObject.Find("@Managers");
if (go == null)
{
go = new GameObject { name = "@Managers" };
go.AddComponent<Managers>();
}
DontDestroyOnLoad(go);
s_Instance = go.GetComponent<Managers>();
}
}
}
기존 Managers에서 ResourceManager의 인스턴스화를 추가했다.