using UnityEngine;
namespace UnityEngine
{
[UnityEngine.Scripting.Preserve]
public static class UnityEngineTransformExtension
{
///
/// 查找子节点的名称符合的 。
///
/// 对象。
/// 子节点的名称
public static Transform FindChildName(this Transform transform, string name)
{
var child = transform.Find(name);
if (child.IsNotNull())
{
return child;
}
var childCount = transform.childCount;
for (int i = 0; i < childCount; i++)
{
var t = transform.GetChild(i);
if (t.name.EqualsFast(name))
{
return t;
}
t = t.FindChildName(name);
if (t.IsNotNull())
{
return t;
}
}
return null;
}
///
/// 设置绝对位置的 x 坐标。
///
/// 对象。
/// x 坐标值。
public static void SetPositionX(this Transform transform, float newValue)
{
Vector3 v = transform.position;
v.x = newValue;
transform.position = v;
}
///
/// 设置绝对位置的 y 坐标。
///
/// 对象。
/// y 坐标值。
public static void SetPositionY(this Transform transform, float newValue)
{
Vector3 v = transform.position;
v.y = newValue;
transform.position = v;
}
///
/// 设置绝对位置的 z 坐标。
///
/// 对象。
/// z 坐标值。
public static void SetPositionZ(this Transform transform, float newValue)
{
Vector3 v = transform.position;
v.z = newValue;
transform.position = v;
}
///
/// 增加绝对位置的 x 坐标。
///
/// 对象。
/// x 坐标值增量。
public static void AddPositionX(this Transform transform, float deltaValue)
{
Vector3 v = transform.position;
v.x += deltaValue;
transform.position = v;
}
///
/// 增加绝对位置的 y 坐标。
///
/// 对象。
/// y 坐标值增量。
public static void AddPositionY(this Transform transform, float deltaValue)
{
Vector3 v = transform.position;
v.y += deltaValue;
transform.position = v;
}
///
/// 增加绝对位置的 z 坐标。
///
/// 对象。
/// z 坐标值增量。
public static void AddPositionZ(this Transform transform, float deltaValue)
{
Vector3 v = transform.position;
v.z += deltaValue;
transform.position = v;
}
///
/// 设置相对位置的 x 坐标。
///
/// 对象。
/// x 坐标值。
public static void SetLocalPositionX(this Transform transform, float newValue)
{
Vector3 v = transform.localPosition;
v.x = newValue;
transform.localPosition = v;
}
///
/// 设置相对位置的 y 坐标。
///
/// 对象。
/// y 坐标值。
public static void SetLocalPositionY(this Transform transform, float newValue)
{
Vector3 v = transform.localPosition;
v.y = newValue;
transform.localPosition = v;
}
///
/// 设置相对位置的 z 坐标。
///
/// 对象。
/// z 坐标值。
public static void SetLocalPositionZ(this Transform transform, float newValue)
{
Vector3 v = transform.localPosition;
v.z = newValue;
transform.localPosition = v;
}
///
/// 增加相对位置的 x 坐标。
///
/// 对象。
/// x 坐标值。
public static void AddLocalPositionX(this Transform transform, float deltaValue)
{
Vector3 v = transform.localPosition;
v.x += deltaValue;
transform.localPosition = v;
}
///
/// 增加相对位置的 y 坐标。
///
/// 对象。
/// y 坐标值。
public static void AddLocalPositionY(this Transform transform, float deltaValue)
{
Vector3 v = transform.localPosition;
v.y += deltaValue;
transform.localPosition = v;
}
///
/// 增加相对位置的 z 坐标。
///
/// 对象。
/// z 坐标值。
public static void AddLocalPositionZ(this Transform transform, float deltaValue)
{
Vector3 v = transform.localPosition;
v.z += deltaValue;
transform.localPosition = v;
}
///
/// 设置相对尺寸的 x 分量。
///
/// 对象。
/// x 分量值。
public static void SetLocalScaleX(this Transform transform, float newValue)
{
Vector3 v = transform.localScale;
v.x = newValue;
transform.localScale = v;
}
///
/// 设置相对尺寸的 y 分量。
///
/// 对象。
/// y 分量值。
public static void SetLocalScaleY(this Transform transform, float newValue)
{
Vector3 v = transform.localScale;
v.y = newValue;
transform.localScale = v;
}
///
/// 设置相对尺寸的 z 分量。
///
/// 对象。
/// z 分量值。
public static void SetLocalScaleZ(this Transform transform, float newValue)
{
Vector3 v = transform.localScale;
v.z = newValue;
transform.localScale = v;
}
///
/// 增加相对尺寸的 x 分量。
///
/// 对象。
/// x 分量增量。
public static void AddLocalScaleX(this Transform transform, float deltaValue)
{
Vector3 v = transform.localScale;
v.x += deltaValue;
transform.localScale = v;
}
///
/// 增加相对尺寸的 y 分量。
///
/// 对象。
/// y 分量增量。
public static void AddLocalScaleY(this Transform transform, float deltaValue)
{
Vector3 v = transform.localScale;
v.y += deltaValue;
transform.localScale = v;
}
///
/// 增加相对尺寸的 z 分量。
///
/// 对象。
/// z 分量增量。
public static void AddLocalScaleZ(this Transform transform, float deltaValue)
{
Vector3 v = transform.localScale;
v.z += deltaValue;
transform.localScale = v;
}
///
/// 二维空间下使 指向指向目标点的算法,使用世界坐标。
///
/// 对象。
/// 要朝向的二维坐标点。
/// 假定其 forward 向量为 。
public static void LookAt2D(this Transform transform, Vector2 lookAtPoint2D)
{
Vector3 vector = lookAtPoint2D.ToVector3() - transform.position;
vector.y = 0f;
if (vector.magnitude > 0f)
{
transform.rotation = Quaternion.LookRotation(vector.normalized, Vector3.up);
}
}
}
}