Files
EnglewoodLAB/Assets/Scripts/UVC/Util/GameObjectUtil.cs

50 lines
2.1 KiB
C#

using UnityEngine;
namespace UVC.Util
{
/// <summary>
/// Unity에서 프리팹 경로를 통해 GameObject를 생성하거나 특정 컴포넌트를 가져오는 유틸리티 클래스입니다.
/// </summary>
public static class GameObjectUtil
{
/// <summary>
/// 주어진 프리팹 경로를 통해 GameObject를 생성합니다.
/// </summary>
/// <param name="prefabPath">프리팹의 Resources 경로</param>
/// <param name="parent">생성된 GameObject의 부모 Transform (선택 사항)</param>
/// <returns>생성된 GameObject</returns>
/// <example>
/// <code>
/// string prefabPath = "Prefabs/MyPrefab";
/// GameObject newObject = prefabPath.GetGameObjectFromPrefabPath();
/// </code>
/// </example>
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);
}
/// <summary>
/// 주어진 프리팹 경로를 통해 특정 타입의 컴포넌트를 가져옵니다.
/// </summary>
/// <typeparam name="T">가져올 컴포넌트 타입</typeparam>
/// <param name="prefabPath">프리팹의 Resources 경로</param>
/// <param name="parent">생성된 GameObject의 부모 Transform (선택 사항)</param>
/// <returns>생성된 GameObject에서 가져온 컴포넌트</returns>
/// <example>
/// <code>
/// string prefabPath = "Prefabs/MyPrefab";
/// MyComponent component = prefabPath.GetComponentFromPrefabPath<MyComponent>();
/// </code>
/// </example>
public static T GetComponentFromPrefabPath<T>(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<T>();
}
}
}