Unity对象旋转功能

      今天上的是一个我们会经常使用的功能---物体旋转(包括UI对象和游戏对象)。使用方法很简单,将脚本挂在目标对象上,在Inspector面板上添加和选中具体参数即可,如下图所示:


      下面上功能代码;还有一份编辑器代码。

using UnityEngine;

/// <summary>
/// 直接挂在需要旋转的对象上
/// 提供旋转的功能
/// </summary>
public class RotateFunction : MonoBehaviour
{

    /// <summary>
    /// 旋转模式
    /// </summary>
    public RotateMode MyRotateMode;
    /// <summary>
    /// 旋转轴
    /// </summary>
    public RotateAxle MyRotate_Axle;

    /// <summary>
    /// 绕某目标旋转时的目标对象
    /// </summary>
    public GameObject RotateTargetObj;

    Transform mytransform;
    void Start()
    {
        mytransform = this.transform;
    }
    //旋转的速度
    public float RotateSpeed = 360f;

    void FixedUpdate()
    {
        //自转
        RotateAroundSelf();
        //绕某点转
        RotateAroundTarget();
    }

    /// <summary>
    /// 自传
    /// </summary>
    void RotateAroundSelf()
    {
        if (MyRotateMode == RotateMode.Rotate_Self)
        {
            switch (MyRotate_Axle)
            {
                case RotateAxle.X:
                    mytransform.Rotate(new Vector3(1f, 0, 0) * RotateSpeed * Time.deltaTime, Space.Self);
                    break;
                case RotateAxle.Y:
                    mytransform.Rotate(new Vector3(0, 1f, 0) * RotateSpeed * Time.deltaTime, Space.Self);
                    break;
                case RotateAxle.Z:
                    mytransform.Rotate(new Vector3(0, 0, 1f) * RotateSpeed * Time.deltaTime, Space.Self);
                    break;
            }
        }
    }

    /// <summary>
    /// 绕目标旋转
    /// </summary>
    void RotateAroundTarget()
    {
        if (MyRotateMode == RotateMode.Rotate_Target && RotateTargetObj != null)
        {
            switch (MyRotate_Axle)
            {
                case RotateAxle.X:
                    mytransform.RotateAround(RotateTargetObj.transform.position, Vector3.right, RotateSpeed * Time.deltaTime);
                    break;
                case RotateAxle.Y:
                    mytransform.RotateAround(RotateTargetObj.transform.position, Vector3.up, RotateSpeed * Time.deltaTime);
                    break;
                case RotateAxle.Z:
                    mytransform.RotateAround(RotateTargetObj.transform.position, Vector3.forward, RotateSpeed * Time.deltaTime);
                    break;
            }
        }
    }
}

下面上功能编辑器脚本

using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(RotateFunction))]
public class RotateFunctionEditor : Editor
{

    public RotateFunction m_RotateFunction = null;
    public void OnEnable()
    {
        m_RotateFunction = (RotateFunction)target;
    }

    public override void OnInspectorGUI()
    {
        bool isChanged = false;

        //绘制MyRotateMode
        RotateMode oldMyRotateMode = m_RotateFunction.MyRotateMode;
        m_RotateFunction.MyRotateMode = (RotateMode)EditorGUILayout.EnumPopup("MyRotateMode", m_RotateFunction.MyRotateMode);
        if (oldMyRotateMode != m_RotateFunction.MyRotateMode)
            isChanged = true;

        //根据选择的RotateMode来确定RotateTarget
        if (m_RotateFunction.MyRotateMode == RotateMode.Rotate_Target)
        {
            GameObject oidObj = m_RotateFunction.RotateTargetObj;
            m_RotateFunction.RotateTargetObj = (GameObject)EditorGUILayout.ObjectField("RotateTargetObj", m_RotateFunction.RotateTargetObj, typeof(GameObject), true);
            if (oidObj != m_RotateFunction.RotateTargetObj)
                isChanged = true;
        }

        //绘制MyRotate_Axle
        RotateAxle oldMyRotate_Axle = m_RotateFunction.MyRotate_Axle;
        m_RotateFunction.MyRotate_Axle = (RotateAxle)EditorGUILayout.EnumPopup("MyRotate_Axle", m_RotateFunction.MyRotate_Axle);
        if (oldMyRotate_Axle != m_RotateFunction.MyRotate_Axle)
            isChanged = true;

        //绘制Rotate Speed
        float oldRotateSpeed = m_RotateFunction.RotateSpeed;
        m_RotateFunction.RotateSpeed = EditorGUILayout.FloatField("Rotate Speed", m_RotateFunction.RotateSpeed);
        if (oldRotateSpeed != m_RotateFunction.RotateSpeed)
            isChanged = true;

        if (isChanged)
            EditorUtility.SetDirty(m_RotateFunction);
    }
}
#endif


       其实功能脚本倒没啥技术含量,值得初学者看看的是第二份是功能的编辑器脚本!







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值