将该脚本挂载到父物体上
public class ShowModel : MonoBehaviour
{
public Transform rotateTransform;
[Range(0.1f,1)]
public float rotationalSpeed = 0.5f;
private bool _isRotational;
private Vector3 _startPosition;
private Vector3 _startRotation;
private void Update()
{
if (Input.GetMouseButton(0) && !_isRotational)
{
if (!HandleMouseClick()) {return;}
_isRotational = true;
_startPosition = Input.mousePosition;
_startRotation = rotateTransform.eulerAngles;
}
if (Input.GetMouseButtonUp(0))
{
_isRotational = false;
rotateTransform.DORotate(new Vector3(0,0,0),0.5f);
}
if (_isRotational)
{
var currentMouse = Input.mousePosition;
var x = _startPosition.x - currentMouse.x;
rotateTransform.eulerAngles = _startRotation + new Vector3(0, x * rotationalSpeed, 0);
}
}
private bool HandleMouseClick()
{
// 在这里检查点击的位置是否在某个UI元素上
// 使用射线检测来判断点击位置是否在UI上
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, results);
//换成自己UI对应的layer
return results[0].gameObject.layer == 10;
}
}