UI上滑动旋转指定对象

挂载在UI上的脚本:

using UnityEngine;
using DG.Tweening;
using UnityEngine.EventSystems;

/// 
/// 此脚本挂载在UI上,鼠标在UI上滑动时旋转指定对象
/// 
public class UITouchRotateTarget : MonoBehaviour, IDragHandler
{
    /// 
    /// 要旋转的对象
    /// 
    public Transform target = null;
    /// 
    /// 旋转速度(3-5效果最佳)
    /// 
    public float speed = 5.0f;

    //DOTween过渡时间(为1时效果最佳)
    private float m_time = 1.0f;
    //缓存Vector3,避免频繁New
    private Vector3 m_rot = Vector3.zero;

    public void OnDrag(PointerEventData eventData)
    {
        if (target == null) return;
        float y = eventData.delta.x;
        m_rot.y = -y * speed;
        target.DOLocalRotate(m_rot, m_time, RotateMode.LocalAxisAdd).SetEase(Ease.OutCubic);
    }

}

效果:

GIF 2022-10-25 20-29-50.gif

你可能感兴趣的:(UI上滑动旋转指定对象)