using UnityEngine;
namespace X10D.Unity;
///
/// Extension methods for .
///
public static class TransformExtensions
{
///
/// Rotates this transform so the forward vector points at another game object.
///
/// The transform whose rotation will be changed.
/// The game object to look at.
///
/// is .
/// -or-
/// is .
///
public static void LookAt(this Transform transform, GameObject target)
{
if (transform == null)
{
throw new ArgumentNullException(nameof(transform));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
transform.LookAt(target.transform);
}
///
/// Rotates this transform so the forward vector points at another game object.
///
/// The transform 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 Transform transform, GameObject target, Vector3 worldUp)
{
if (transform == null)
{
throw new ArgumentNullException(nameof(transform));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
transform.LookAt(target.transform, worldUp);
}
///
/// Sets the parent of this transform.
///
/// The transform whose parent to change.
/// The new parent.
///
/// is .
/// -or-
/// is .
///
public static void SetParent(this Transform transform, GameObject parent)
{
if (transform == null)
{
throw new ArgumentNullException(nameof(transform));
}
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
transform.transform.SetParent(parent.transform);
}
///
/// Sets the parent of this transform.
///
/// The transform 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 Transform transform, GameObject parent, bool worldPositionStays)
{
if (transform == null)
{
throw new ArgumentNullException(nameof(transform));
}
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
transform.SetParent(parent.transform, worldPositionStays);
}
}