using UnityEngine;
namespace UVC.Util
{
///
/// Unity에서 프리팹 경로를 통해 GameObject를 생성하거나 특정 컴포넌트를 가져오는 유틸리티 클래스입니다.
///
public static class GameObjectUtil
{
///
/// 주어진 프리팹 경로를 통해 GameObject를 생성합니다.
///
/// 프리팹의 Resources 경로
/// 생성된 GameObject의 부모 Transform (선택 사항)
/// 생성된 GameObject
///
///
/// string prefabPath = "Prefabs/MyPrefab";
/// GameObject newObject = prefabPath.GetGameObjectFromPrefabPath();
///
///
public static GameObject GetGameObjectFromPrefabPath(this string prefabPath, Transform parent = null)
{
GameObject prefab = Resources.Load(prefabPath, typeof(GameObject)) as GameObject;
return UnityEngine.Object.Instantiate(prefab, parent);
}
///
/// 주어진 프리팹 경로를 통해 특정 타입의 컴포넌트를 가져옵니다.
///
/// 가져올 컴포넌트 타입
/// 프리팹의 Resources 경로
/// 생성된 GameObject의 부모 Transform (선택 사항)
/// 생성된 GameObject에서 가져온 컴포넌트
///
///
/// string prefabPath = "Prefabs/MyPrefab";
/// MyComponent component = prefabPath.GetComponentFromPrefabPath();
///
///
public static T GetComponentFromPrefabPath(this string prefabPath, Transform parent = null)
{
GameObject prefab = Resources.Load(prefabPath, typeof(GameObject)) as GameObject;
GameObject go = UnityEngine.Object.Instantiate(prefab, parent);
return go.GetComponent();
}
}
}