Files
EnglewoodLAB/Assets/Scripts/Extensions/TransformExtension.cs
SOOBEEN HAN f1894889ee <refactor> Octopus Twin 템플릿 적용
- 기능 외 UI 구조만 적용
- 프로젝트에 걸맞는 UI는 재작업 필요
2026-02-23 18:20:09 +09:00

167 lines
5.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace EnglewoodLAB.Extensions
{
public static class TransformExtension
{
public static T GetOrAddComponent<T>(this Transform root) where T : Component
{
if (root.TryGetComponent<T>(out var result))
{
return result;
}
return root.gameObject.AddComponent<T>();
}
public static T Find<T>(this Transform root, string name) where T : UnityEngine.Component
{
for (int i = 0; i < root.childCount; ++i)
{
var child = root.GetChild(i);
if (child.TryGetComponent<T>(out var result))
{
if (child.name == name)
{
return result;
}
}
result = child.Find<T>(name);
if (result != null)
{
return result;
}
}
return null;
}
public static T GetComponentInChildren<T>(this Transform root, string name) where T : Component
{
for (int i = 0; i < root.childCount; ++i)
{
var c = root.GetChild(i);
if (c.TryGetComponent<T>(out var result))
{
if (c.gameObject.name == name)
return result;
}
result = c.GetComponentInChildren<T>(name);
if (result != null)
{
return result;
}
}
return null;
}
public static bool TryGetComponentInChildren<T>(this Transform root, string name, out T result) where T : Component
{
if (root.TryGetComponent<T>(out result))
{
if (result.name.Equals(name))
{
return true;
}
}
for (int i = 0; i < root.childCount; ++i)
{
var children = root.GetChild(i);
if (children.TryGetComponent<T>(out result))
{
if (result.name.Equals(name))
return true;
}
if (children.TryGetComponentInChildren<T>(name, out result))
{
if (result.name.Equals(name))
{
return true;
}
}
}
return false;
}
public static bool TryGetComponentsInChildren<T>(this Transform root, out T[] result) where T : Component
{
result = root.GetComponentsInChildren<T>();
if (result != null && result.Length != 0)
{
return true;
}
return false;
}
public static bool TryGetComponentInChildren<T>(this Transform root, out T result) where T : Component
{
if (root.TryGetComponent<T>(out result))
{
return true;
}
for (int i = 0; i < root.childCount; ++i)
{
var children = root.GetChild(i);
if (children.TryGetComponent<T>(out result))
{
return true;
}
if (children.TryGetComponentInChildren<T>(out result))
{
return true;
}
}
return false;
}
public static bool TryGetComponentInParent<T>(this Transform root, out T result)
{
var curr = root.parent;
while (curr != null)
{
if (curr.TryGetComponent<T>(out result))
{
return true;
}
curr = curr.parent;
}
result = default(T);
return false;
}
public static bool Contains<T>(this Transform root, T target) where T : Component
{
var comps = root.GetComponentsInChildren<T>();
if (comps.Contains(target))
{
return true;
}
return false;
}
public static Vector3 GetMeshCenter(this Transform root)
{
{
Renderer[] renderers = root.GetComponentsInChildren<Renderer>(true);
if (renderers == null || renderers.Length == 0)
{
return Vector3.zero;
}
// 첫 번째 Renderer의 bounds로 초기 바운딩 박스를 설정
Bounds combinedBounds = renderers[0].bounds;
// 나머지 Renderer들의 bounds를 combinedBounds에 포함시킴
for (int i = 1; i < renderers.Length; i++)
{
combinedBounds.Encapsulate(renderers[i].bounds);
}
// 결합된 바운딩 박스의 중심점을 반환
return combinedBounds.center;
}
}
}
}