Web_Rot/Assets/Scripts/ObjectOrbitController.cs
2026-04-08 11:44:20 +08:00

201 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectOrbitController : MonoBehaviour
{
#region
//[Header("旋转设置")]
//[Range(50f, 500f)] public float rotateSpeed = 150f;
//[Header("缩放设置")]
//[Range(0.1f, 5f)] public float zoomSpeed = 0.5f;
//[Range(0.1f, 10f)] public float minScale = 0.5f;
//[Range(0.1f, 10f)] public float maxScale = 2f;
//private Vector3 _initialScale; // 初始缩放
//private Vector2 _lastMousePos; // 上一帧鼠标位置
//private void Start()
//{
// _initialScale = transform.localScale;
// isShuangZhi = false;
//}
//bool isShuangZhi;
//private void Update()
//{
// if (Input.touchCount == 2)
// {
// isShuangZhi = true;
// }
// // 1. 鼠标左键旋转物体
// if (Input.GetMouseButtonDown(0))
// {
// _lastMousePos = (Vector2)Input.mousePosition;
// }
// else if (Input.GetMouseButton(0))
// {
// Vector2 delta = (Vector2)Input.mousePosition - _lastMousePos;
// _lastMousePos = (Vector2)Input.mousePosition;
// // 绕Y轴左右拖、X轴上下拖旋转
// transform.Rotate(Vector3.up, -delta.x * rotateSpeed * Time.deltaTime, Space.World);
// transform.Rotate(Vector3.right, delta.y * rotateSpeed * Time.deltaTime, Space.World);
// }
// // 2. 滚轮缩放物体
// float scroll = Input.GetAxis("Mouse ScrollWheel");
// if (scroll != 0)
// {
// Vector3 newScale = transform.localScale + Vector3.one * scroll * zoomSpeed;
// // 限制缩放范围
// newScale = new Vector3(
// Mathf.Clamp(newScale.x, minScale, maxScale),
// Mathf.Clamp(newScale.y, minScale, maxScale),
// Mathf.Clamp(newScale.z, minScale, maxScale)
// );
// transform.localScale = newScale;
// }
//}
#endregion
[Header("旋转速度")]
[Range(50f, 500f)] public float rotateSpeed = 150f;
[Header("缩放设置")]
[Range(0.1f, 5f)] public float zoomSpeed = 0.5f;
[Range(0.1f, 10f)] public float minScale = 0.5f;
[Range(0.1f, 10f)] public float maxScale = 2f;
private Vector3 _initialScale; // 初始缩放
private Vector2 _lastMousePos; // 上一帧鼠标/触摸位置
private float _lastTouchDistance; // 上一帧双指距离
private bool _isFirstTouchInCurrentGesture; // 标记当前双指手势是否是首次触摸
private void Start()
{
_initialScale = transform.localScale;
}
private bool isShuangZhi;
private void Update()
{
// 处理触摸输入
HandleTouchInput();
// 处理鼠标输入(在没有触摸时生效)
if (Input.touchCount == 0)
{
HandleMouseInput();
}
}
/// <summary>
/// 处理触摸屏输入
/// </summary>
private void HandleTouchInput()
{
// 单指触摸 - 旋转
if (Input.touchCount == 1)
{
isShuangZhi = false;
_isFirstTouchInCurrentGesture = true; // 重置双指手势标记
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
_lastMousePos = touch.position;
}
else if (touch.phase == TouchPhase.Moved)
{
Vector2 delta = touch.position - _lastMousePos;
_lastMousePos = touch.position;
// 绕Y轴和X轴旋转与鼠标控制逻辑一致
transform.Rotate(Vector3.up, -delta.x * rotateSpeed * Time.deltaTime, Space.World);
transform.Rotate(Vector3.right, delta.y * rotateSpeed * Time.deltaTime, Space.World);
}
}
// 双指触摸 - 缩放
else if (Input.touchCount == 2)
{
isShuangZhi = true;
Touch touch0 = Input.GetTouch(0);
Touch touch1 = Input.GetTouch(1);
// 仅在触摸移动时处理缩放
if (touch0.phase == TouchPhase.Moved || touch1.phase == TouchPhase.Moved)
{
// 获取当前双指距离
float currentDistance = Vector2.Distance(touch0.position, touch1.position);
// 仅在当前双指手势的首次触摸时初始化距离(解决跳变问题)
if (_isFirstTouchInCurrentGesture)
{
_lastTouchDistance = currentDistance;
_isFirstTouchInCurrentGesture = false; // 标记为非首次
}
else
{
// 双指张开→distanceDelta正→模型放大双指收缩→distanceDelta负→模型缩小
float distanceDelta = currentDistance - _lastTouchDistance;
// 应用缩放(基于当前模型大小,而非重置)
Vector3 newScale = transform.localScale + Vector3.one * distanceDelta * 0.01f * zoomSpeed;
// 限制缩放范围
newScale = new Vector3(
Mathf.Clamp(newScale.x, minScale, maxScale),
Mathf.Clamp(newScale.y, minScale, maxScale),
Mathf.Clamp(newScale.z, minScale, maxScale)
);
transform.localScale = newScale;
// 更新上一帧距离(保留当前距离,用于下次计算)
_lastTouchDistance = currentDistance;
}
}
// 双指离开时标记为"手势结束"但保留_lastTouchDistance的值
else if (touch0.phase == TouchPhase.Ended || touch1.phase == TouchPhase.Ended)
{
_isFirstTouchInCurrentGesture = true;
}
}
else
{
isShuangZhi = false;
// 移除「重置_lastTouchDistance为0」的逻辑 → 保留上次双指距离,避免跳变
// _lastTouchDistance = 0; // 这行是导致跳变的核心原因,直接删除
_isFirstTouchInCurrentGesture = true;
}
}
/// <summary>
/// 处理鼠标输入
/// </summary>
private void HandleMouseInput()
{
// 鼠标左键旋转
if (Input.GetMouseButtonDown(0))
{
_lastMousePos = (Vector2)Input.mousePosition;
}
else if (Input.GetMouseButton(0))
{
Vector2 delta = (Vector2)Input.mousePosition - _lastMousePos;
_lastMousePos = (Vector2)Input.mousePosition;
transform.Rotate(Vector3.up, -delta.x * rotateSpeed * Time.deltaTime, Space.World);
transform.Rotate(Vector3.right, delta.y * rotateSpeed * Time.deltaTime, Space.World);
}
// 鼠标滚轮缩放
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll != 0)
{
Vector3 newScale = transform.localScale + Vector3.one * scroll * zoomSpeed;
newScale = new Vector3(
Mathf.Clamp(newScale.x, minScale, maxScale),
Mathf.Clamp(newScale.y, minScale, maxScale),
Mathf.Clamp(newScale.z, minScale, maxScale)
);
transform.localScale = newScale;
}
}
}