Files
TaiWan/Assets/Roaming/Extenders/ModelPlayerEx/Scripts/ModelController.cs
2025-10-31 15:20:38 +08:00

101 lines
3.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ModelController : MonoBehaviour
{
public Transform imgModel;
public GameObject model; // 要交互的模型
public Transform cameraTransform;
public float rotateSpeed = 2f; // 旋转速度
public float scaleSpeed = 0.1f; // 缩放速度
public float maxScale = 5f;
public float minScale = 1f;
public static bool CanControl = true;
private Vector3 oldPosition;
private Vector3 oldRotation;
// 旋转使用变量
Vector3 mPrevPos = Vector3.zero;
Vector3 mPosDelta = Vector3.zero;
// 缩放使用变量
Vector2 finger1Position;
Vector2 finger2Position;
float scroll;
float currentScale = 1f;
Vector3 tmpPos;
bool isTouchDown;
bool isFirst = true;
float x, y;
private void Update()
{
if (!ModelPlayerEx.Instance.IsDetailShow && CanControl &&
model != null && ModelPlayerEx.Instance.ModelSeted)
{
if (Input.GetMouseButton(0) || (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved))
{
//if (isFirst)
//{
// isFirst = false;
// mPrevPos = Input.mousePosition;
//}
//mPrevPos = Input.mousePosition;
//mPosDelta = Input.mousePosition - mPrevPos;
x = Input.GetAxis("Mouse X") * rotateSpeed;
y = Input.GetAxis("Mouse Y") * rotateSpeed;
model.transform.Rotate(Camera.main.transform.up, -x * rotateSpeed, Space.World);
model.transform.Rotate(Camera.main.transform.right, y * rotateSpeed, Space.World);
}
//mPrevPos = Input.mousePosition;
if (Input.touchCount == 2)
{
Touch touch1 = Input.GetTouch(0);
Touch touch2 = Input.GetTouch(1);
// 更新手指位置
finger1Position = touch1.position;
finger2Position = touch2.position;
// 计算当前手指距离和上一帧手指距离的差值,来计算缩放比例
float distance = Vector2.Distance(finger1Position, finger2Position);
float previousDistance = Vector2.Distance(finger1Position - touch1.deltaPosition, finger2Position - touch2.deltaPosition);
scroll = (previousDistance - distance) / 5f;
}
else
{
scroll = Input.GetAxis("Mouse ScrollWheel");
}
if (scroll >= 0.1f)
{
currentScale *= (1f + scaleSpeed);
if (currentScale > maxScale)
currentScale = maxScale;
//model.transform.localScale = Vector3.one * currentScale;
imgModel.transform.localScale = Vector3.one * currentScale;
}
else if (scroll <= -0.1f)
{
currentScale /= (1f + scaleSpeed);
if (currentScale < minScale)
currentScale = minScale;
//model.transform.localScale = Vector3.one * currentScale;
imgModel.transform.localScale = Vector3.one * currentScale;
}
}
}
}