using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// 在某个ui范围内旋转3D物体(挂在谁身上在谁的范围内)
/// </summary>
public class SpinWithMouse : MonoBehaviour, IDragHandler
{ 
    /// <summary>
    /// 要旋轉的三維物體的Transform組件
    /// </summary>
    public Transform target;
    /// <summary>
    /// 旋转速度
    /// </summary>
    public float speed = 1f;  
    void Start()
    { 
        if (target == null) target = transform;
    }
    public void OnDrag(PointerEventData eventData)
    { 
        // 控制上下移动的旋转(不需要可设为0) 控制左右移动时的旋转 
        target.localRotation = Quaternion.Euler(0.5f * eventData.delta.y * speed, -0.5f * eventData.delta.x * speed, 0f) * target.localRotation;
    }
}

本文地址:https://blog.csdn.net/weixin_43535270/article/details/109242707