写了一个Transform的扩展类,可以单独设置Position的x或者直接设置Scale的整体缩放大小。
如果对静态类不了解的可以参考这里:https://www.jianshu.com/p/17ee1c7f23a5
先看一下调用方式:
using UnityEngine;
public class Test : MonoBehaviour
{
Transform player;
// Start is called before the first frame update
void Start()
{
player = this.transform;
player.SetLocalScale(5);
player.Reset();
}
}
扩展类如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Transform扩展类
/// </summary>
public static partial class TransformEx
{
/// <summary>
/// 设置位置x
/// </summary>
/// <param name="transform"></param>
/// <param name="x">x</param>
public static void SetPositionX(this Transform transform, float x)
{
var localPos = transform.localPosition;
localPos.x = x;
transform.localPosition = localPos;
}
/// <summary>
/// 设置位置y
/// </summary>
/// <param name="transform"></param>
/// <param name="y">y</param>
public static void SetPositionY(this Transform transform, float y)
{
var localPos = transform.localPosition;
localPos.y = y;
transform.localPosition = localPos;
}
/// <summary>
/// 设置位置z
/// </summary>
/// <param name="transform"></param>
/// <param name="z">z</param>
public static void SetPositionZ(this Transform transform, float z)
{
var localPos = transform.localPosition;
localPos.z = z;
transform.localPosition = localPos;
}
/// <summary>
/// 设置欧拉角x
/// </summary>
/// <param name="transform"></param>
/// <param name="x">x</param>
public static void SetEulerAnglesX(this Transform transform, float x)
{
var localEulerAngles = transform.localEulerAngles;
localEulerAngles.x = x;
transform.localEulerAngles = localEulerAngles;
}
/// <summary>
/// 设置欧拉角y
/// </summary>
/// <param name="transform"></param>
/// <param name="y">y</param>
public static void SetEulerAnglesY(this Transform transform, float y)
{
var localEulerAngles = transform.localEulerAngles;
localEulerAngles.y = y;
transform.localPosition = localEulerAngles;
}
/// <summary>
/// 设置欧拉角z
/// </summary>
/// <param name="transform"></param>
/// <param name="z">z</param>
public static void SetEulerAnglesZ(this Transform transform, float z)
{
var localEulerAngles = transform.localEulerAngles;
localEulerAngles.z = z;
transform.localEulerAngles = localEulerAngles;
}
/// <summary>
/// 设置大小x
/// </summary>
/// <param name="transform"></param>
/// <param name="x">x</param>
public static void SetLocalScaleX(this Transform transform, float x)
{
var localScale = transform.localScale;
localScale.x = x;
transform.localScale = localScale;
}
/// <summary>
/// 设置大小y
/// </summary>
/// <param name="transform"></param>
/// <param name="y">y</param>
public static void SetLocalScaleY(this Transform transform, float y)
{
var localScale = transform.localScale;
localScale.y = y;
transform.localScale = localScale;
}
/// <summary>
/// 设置大小z
/// </summary>
/// <param name="transform"></param>
/// <param name="z">z</param>
public static void SetLocalScaleZ(this Transform transform, float z)
{
var localScale = transform.localScale;
localScale.z = z;
transform.localScale = localScale;
}
/// <summary>
/// 设置大小
/// </summary>
/// <param name="transform"></param>
/// <param name="scale">scale</param>
public static void SetLocalScale(this Transform transform, float scale)
{
var localScale = Vector3.one * scale;
transform.localScale = localScale;
}
/// <summary>
/// 设置大小缩放倍数
/// </summary>
/// <param name="transform"></param>
/// <param name="multiple">缩放倍数</param>
public static void SetLocalScaleMultiple(this Transform transform, float multiple)
{
var localScale = transform.localScale * multiple;
transform.localScale = localScale;
}
/// <summary>
/// 重置位置、角度、缩放大小
/// </summary>
/// <param name="transform"></param>
public static void Reset(this Transform transform)
{
transform.localPosition = Vector3.zero;
transform.localScale = Vector3.one;
transform.localRotation = Quaternion.identity;
}
}