#nullable enable using UnityEngine; namespace UVC.Extension { /// /// Transform 클래스에 대한 확장 메소드를 제공하는 정적 클래스입니다. /// public static class TransformEx { /// /// 하위 자식까지 확인해서 이름이 같은 오브젝트를 찾는다. /// /// 검색을 시작할 부모 Transform /// 찾고자 하는 자식 GameObject의 이름 /// 찾은 자식 Transform 또는 찾지 못한 경우 null /// /// /// // 사용 예시 /// Transform parentTransform = gameObject.transform; /// Transform childTransform = parentTransform.FindChildren("TargetChild"); /// /// if (childTransform != null) /// { /// // 찾은 자식 오브젝트 사용 /// childTransform.gameObject.SetActive(true); /// } /// /// public static Transform? FindChildren(this Transform t, string name) { Transform? child = null; var children = t.GetComponentsInChildren(true); int len = children.Length; //첫번째 자식은 자기 자신이므로 제외 for (int i = 1; i < len; i++) { Transform c = children[i]; if (c.name == name) { child = c; break; } } return child; } /// /// 직계 자식 Transform을 반환합니다. /// /// 직계 자식을 검색할 변환입니다. 일 수 없습니다. /// 지정된 변환의 직계 자식을 나타내는 객체의 배열입니다. /// 변환에 자식이 없으면 배열은 비어 있습니다. public static Transform[] GetChildren(this Transform t) { Transform[] children = new Transform[t.childCount]; for (int i = 0; i < t.childCount; i++) { children[i] = t.GetChild(i); } return children; } /// /// 직계 자식에서 T를 찾아 반환합니다. /// /// 검색할 구성 요소의 유형입니다. 에서 파생되어야 합니다. /// 자식 구성 요소를 검색할 변환입니다. /// 변환의 자식에서 발견된 유형의 구성 요소 배열입니다. 자식에 /// 유형의 구성 요소가 없는 경우 해당 배열 항목은 입니다. public static T[] GetChildren(this Transform t) where T : Component { T[] children = new T[t.childCount]; for (int i = 0; i < t.childCount; i++) { var temp = t.GetChild(i).GetComponent(); if(temp != null) children[i] = temp; } return children; } /// /// Transform의 X 좌표만 변경합니다. /// /// 대상 Transform /// 설정할 X 좌표 값 /// 변경된 Transform (메소드 체이닝 지원) /// /// /// // 사용 예시 /// transform.SetPositionX(5f).SetPositionZ(3f); /// /// public static Transform SetPositionX(this Transform t, float x) { Vector3 position = t.position; position.x = x; t.position = position; return t; } /// /// Transform의 Y 좌표만 변경합니다. /// /// 대상 Transform /// 설정할 Y 좌표 값 /// 변경된 Transform (메소드 체이닝 지원) /// /// /// // 사용 예시 /// transform.SetPositionY(5f).SetPositionZ(3f); /// /// public static Transform SetPositionY(this Transform t, float y) { Vector3 position = t.position; position.y = y; t.position = position; return t; } /// /// Transform의 Z 좌표만 변경합니다. /// /// 대상 Transform /// 설정할 Z 좌표 값 /// 변경된 Transform (메소드 체이닝 지원) /// /// /// // 사용 예시 /// transform.SetPositionX(5f).SetPositionZ(3f); /// /// public static Transform SetPositionZ(this Transform t, float z) { Vector3 position = t.position; position.z = z; t.position = position; return t; } /// /// 하위 자식 중에서 특정 태그를 가진 오브젝트를 모두 찾습니다. /// /// 검색을 시작할 부모 Transform /// 찾고자 하는 태그 /// 찾은 Transform 배열 /// /// /// // 사용 예시 /// Transform[] enemies = parentTransform.FindChildrenWithTag("Enemy"); /// foreach (Transform enemy in enemies) /// { /// // 찾은 적 오브젝트 처리 /// enemy.gameObject.SetActive(false); /// } /// /// public static Transform[] FindChildrenWithTag(this Transform t, string tag) { System.Collections.Generic.List result = new System.Collections.Generic.List(); FindChildrenWithTagRecursive(t, tag, result); return result.ToArray(); } private static void FindChildrenWithTagRecursive(Transform parent, string tag, System.Collections.Generic.List result) { int childCount = parent.childCount; for (int i = 0; i < childCount; i++) { Transform child = parent.GetChild(i); if (child.CompareTag(tag)) { result.Add(child); } FindChildrenWithTagRecursive(child, tag, result); } } /// /// Transform을 기본 상태로 초기화합니다. (위치: 원점, 회전: 0, 크기: 1) /// /// 초기화할 Transform /// 초기화된 Transform (메소드 체이닝 지원) /// /// /// // 사용 예시 /// transform.ResetTransform(); /// /// public static Transform ResetTransform(this Transform t) { t.localPosition = Vector3.zero; t.localRotation = Quaternion.identity; t.localScale = Vector3.one; return t; } /// /// 지정된 의 로컬 위치를 월드 공간 좌표로 변환합니다. /// /// 로컬 위치를 변환할 입니다. /// 로컬 위치의 월드 공간 좌표를 나타내는 입니다. public static Vector3 ToWorldPosition(this Transform t) { return t.TransformPoint(t.localPosition); } } }