using UnityEngine; namespace X10D.Unity; /// /// Extension methods for . /// public static class GameObjectExtensions { /// /// Rotates the transform component of this game object so the forward vector points at another game object. /// /// The game object whose rotation will be changed. /// The game object to look at. /// /// is . /// -or- /// is . /// public static void LookAt(this GameObject gameObject, GameObject target) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } gameObject.transform.LookAt(target.transform); } /// /// Rotates the transform component of this game object so the forward vector points at . /// /// The game object whose rotation will be changed. /// The point to look at. /// is . public static void LookAt(this GameObject gameObject, Vector3 target) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } gameObject.transform.LookAt(target); } /// /// Rotates the transform component of this game object so the forward vector points at a specified transform. /// /// The game object whose rotation will be changed. /// The transform to look at. /// /// is . /// -or- /// is . /// public static void LookAt(this GameObject gameObject, Transform target) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } gameObject.transform.LookAt(target); } /// /// Rotates the transform component of this game object so the forward vector points at another game object. /// /// The game object whose rotation will be changed. /// The game object to look at. /// A vector specifying the upward direction. /// /// is . /// -or- /// is . /// public static void LookAt(this GameObject gameObject, GameObject target, Vector3 worldUp) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } gameObject.transform.LookAt(target.transform, worldUp); } /// /// Rotates the transform component of this game object so the forward vector points at . /// /// The game object whose rotation will be changed. /// The point to look at. /// A vector specifying the upward direction. /// is . public static void LookAt(this GameObject gameObject, Vector3 target, Vector3 worldUp) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } gameObject.transform.LookAt(target, worldUp); } /// /// Rotates the transform component of this game object so the forward vector points at a specified transform. /// /// The game object whose rotation will be changed. /// The transform to look at. /// A vector specifying the upward direction. /// /// is . /// -or- /// is . /// public static void LookAt(this GameObject gameObject, Transform target, Vector3 worldUp) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } gameObject.transform.LookAt(target, worldUp); } /// /// Sets the parent of this game object. /// /// The game object whose parent to change. /// The new parent. /// /// is . /// -or- /// is . /// public static void SetParent(this GameObject gameObject, GameObject parent) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } if (parent == null) { throw new ArgumentNullException(nameof(parent)); } gameObject.transform.SetParent(parent.transform); } /// /// Sets the parent of this game object. /// /// The game object whose parent to change. /// The new parent. /// /// is . /// -or- /// is . /// public static void SetParent(this GameObject gameObject, Transform parent) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } if (parent == null) { throw new ArgumentNullException(nameof(parent)); } gameObject.transform.SetParent(parent); } /// /// Sets the parent of this game object. /// /// The game object whose parent to change. /// The new parent. /// /// to modify the parent-relative position, scale and rotation such that the object keeps the same /// world space position, rotation and scale as before; otherwise, . /// /// /// is . /// -or- /// is . /// public static void SetParent(this GameObject gameObject, GameObject parent, bool worldPositionStays) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } if (parent == null) { throw new ArgumentNullException(nameof(parent)); } gameObject.transform.SetParent(parent.transform, worldPositionStays); } /// /// Sets the parent of this game object. /// /// The game object whose parent to change. /// The new parent. /// /// to modify the parent-relative position, scale and rotation such that the object keeps the same /// world space position, rotation and scale as before; otherwise, . /// /// /// is . /// -or- /// is . /// public static void SetParent(this GameObject gameObject, Transform parent, bool worldPositionStays) { if (gameObject == null) { throw new ArgumentNullException(nameof(gameObject)); } if (parent == null) { throw new ArgumentNullException(nameof(parent)); } gameObject.transform.SetParent(parent, worldPositionStays); } }