using System; using System.Collections; using System.Collections.Generic; using GameFramework.Event; using Unity.VisualScripting; using UnityEngine; using UnityEngine.EventSystems; public class MachineMgrCtrl : MonoBehaviour { public static MachineMgrCtrl _Instance; public float rotationSpeed = 8.0f; // 摄像机绕点旋转速度 public float rotationDamping = 0.96f; // 旋转阻尼系数,控制旋转阻尼效果 public float minDistance = 2.0f; // 最小距离 public float maxDistance = 30.0f; // 最大距离 public float zoomSpeed = 3f; // 缩放速度 public float zoomDamping = 0.96f; // 缩放阻尼系数,控制缩放阻尼效果 private Vector3 targetPosition; // 旋转点 private Vector3 lastMousePosition; // 上一帧的鼠标位置 private Vector3 currentVelocity; // 当前旋转速度 private float distance = 5.0f; // 初始摄像机距离 private float targetDistance; private bool isRotating = false; // 是否正在旋转 private bool isOnUI = false; public GameObject[] machineList; public int currentModelIndex = 0; public int currentBaseStudyType { get; private set; } private void Awake() { _Instance = this; machineList = new GameObject[this.transform.childCount]; for (int i = 0; i < this.transform.childCount; i++) { machineList[i] = this.transform.GetChild(i).gameObject; } } void Start() { targetDistance = distance; GameMain.Event.Subscribe(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy); GameMain.Event.Subscribe(ChangeDevicesModelEvent.EventId, OnChangeModel); GameMain.Event.Subscribe(ChangeModelTypeEvent.EventId, OnChangeModelType); } private void OnChangeBaseStudy(object sender, GameEventArgs e) { ChangeBaseStudyTypeEvent args = (ChangeBaseStudyTypeEvent)e; if (args.type != currentBaseStudyType) { switch (args.type) { case 0: this.gameObject.SetActive(true); break; case 1: this.gameObject.SetActive(false); break; case 2: this.gameObject.SetActive(false); break; case 3: this.gameObject.SetActive(false); break; } currentBaseStudyType = args.type; } } // Update is called once per frame void Update() { RotationCamera(); } private void OnDestroy() { if (GameMain.Event.Check(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy)) { GameMain.Event.Unsubscribe(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy); } if (GameMain.Event.Check(ChangeDevicesModelEvent.EventId, OnChangeModel)) { GameMain.Event.Unsubscribe(ChangeDevicesModelEvent.EventId, OnChangeModel); } if (GameMain.Event.Check(ChangeModelTypeEvent.EventId, OnChangeModelType)) { GameMain.Event.Unsubscribe(ChangeModelTypeEvent.EventId, OnChangeModelType); } } private void OnChangeModel(object sender, GameEventArgs e) { ChangeDevicesModelEvent args = (ChangeDevicesModelEvent)e; ResetCamera(); for (int i = 0; i < machineList.Length; i++) { if (machineList[i].name != args.data.modelName) { machineList[i].SetActive(false); } else { HttpGlobal.SendLogToServer("参观了设备:" + args.data.name); machineList[i].SetActive(true); var ani = machineList[i].GetComponent(); if (ani != null) { ani.SetFloat("speed", 0); ani.Play(machineList[i].name, 0, 0); } currentModelIndex = i; } } } private void OnChangeModelType(object sender, GameEventArgs e) { ChangeModelTypeEvent args = (ChangeModelTypeEvent)e; var ani = machineList[currentModelIndex].GetComponent(); if (ani != null) { ani.SetFloat("speed", args.type == 0 ? -1 : 1); Debug.Log(ani.speed); ani.Play(machineList[currentModelIndex].name, 0, args.type == 0 ? 1 : 0); } } public void ResetCamera() { targetPosition = Vector3.zero; targetDistance = distance; } void RotationCamera() { // 获取鼠标输入 Vector3 currentMousePosition = Input.mousePosition; if (EventSystem.current.IsPointerOverGameObject()) { isOnUI = true; } else { isOnUI = false; } if (Input.GetMouseButtonDown(0) && !isOnUI) { lastMousePosition = currentMousePosition; isRotating = true; } else if (Input.GetMouseButtonUp(0)) { isRotating = false; } // 如果正在旋转 if (isRotating) { // 计算鼠标移动量 Vector3 mouseDelta = currentMousePosition - lastMousePosition; // 计算摄像机绕点的旋转 float rotationX = mouseDelta.y * rotationSpeed * Time.deltaTime; float rotationY = -mouseDelta.x * rotationSpeed * Time.deltaTime; // 应用旋转阻尼效果 rotationX *= rotationDamping; rotationY *= rotationDamping; // 计算新的旋转速度 currentVelocity = new Vector3(rotationX, rotationY, 0); // 应用旋转 Camera.main.transform.RotateAround(targetPosition, Vector3.up, currentVelocity.y); Camera.main.transform.RotateAround(targetPosition, Camera.main.transform.right, currentVelocity.x); // 更新鼠标位置 lastMousePosition = currentMousePosition; } else { // 增强旋转阻尼效果 currentVelocity *= rotationDamping; Camera.main.transform.RotateAround(targetPosition, Vector3.up, currentVelocity.y); Camera.main.transform.RotateAround(targetPosition, Camera.main.transform.right, currentVelocity.x); } if (!isOnUI) { // 使用鼠标中键来控制摄像机距离 float scroll = Input.GetAxis("Mouse ScrollWheel"); targetDistance = Mathf.Clamp(targetDistance - scroll * zoomSpeed, minDistance, maxDistance); } // 应用缩放阻尼效果来平滑改变摄像机距离 distance = Mathf.Lerp(distance, targetDistance, Time.deltaTime * zoomDamping); // 更新摄像机位置 Vector3 dirToTarget = (Camera.main.transform.position - targetPosition).normalized; Camera.main.transform.position = targetPosition + dirToTarget * distance; } }