TransformEx扩展类

写了一个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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值