MachineMgrCtrl.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using GameFramework.Event;
  5. using Unity.VisualScripting;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. public class MachineMgrCtrl : MonoBehaviour
  9. {
  10. public static MachineMgrCtrl _Instance;
  11. public float rotationSpeed = 8.0f; // 摄像机绕点旋转速度
  12. public float rotationDamping = 0.96f; // 旋转阻尼系数,控制旋转阻尼效果
  13. public float minDistance = 2.0f; // 最小距离
  14. public float maxDistance = 30.0f; // 最大距离
  15. public float zoomSpeed = 3f; // 缩放速度
  16. public float zoomDamping = 0.96f; // 缩放阻尼系数,控制缩放阻尼效果
  17. private Vector3 targetPosition; // 旋转点
  18. private Vector3 lastMousePosition; // 上一帧的鼠标位置
  19. private Vector3 currentVelocity; // 当前旋转速度
  20. private float distance = 5.0f; // 初始摄像机距离
  21. private float targetDistance;
  22. private bool isRotating = false; // 是否正在旋转
  23. private bool isOnUI = false;
  24. public GameObject[] machineList;
  25. public int currentModelIndex = 0;
  26. public int currentBaseStudyType { get; private set; }
  27. private void Awake()
  28. {
  29. _Instance = this;
  30. machineList = new GameObject[this.transform.childCount];
  31. for (int i = 0; i < this.transform.childCount; i++)
  32. {
  33. machineList[i] = this.transform.GetChild(i).gameObject;
  34. }
  35. }
  36. void Start()
  37. {
  38. targetDistance = distance;
  39. GameMain.Event.Subscribe(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy);
  40. GameMain.Event.Subscribe(ChangeDevicesModelEvent.EventId, OnChangeModel);
  41. GameMain.Event.Subscribe(ChangeModelTypeEvent.EventId, OnChangeModelType);
  42. }
  43. private void OnChangeBaseStudy(object sender, GameEventArgs e)
  44. {
  45. ChangeBaseStudyTypeEvent args = (ChangeBaseStudyTypeEvent)e;
  46. if (args.type != currentBaseStudyType)
  47. {
  48. switch (args.type)
  49. {
  50. case 0:
  51. this.gameObject.SetActive(true);
  52. break;
  53. case 1:
  54. this.gameObject.SetActive(false);
  55. break;
  56. case 2:
  57. this.gameObject.SetActive(false);
  58. break;
  59. case 3:
  60. this.gameObject.SetActive(false);
  61. break;
  62. }
  63. currentBaseStudyType = args.type;
  64. }
  65. }
  66. // Update is called once per frame
  67. void Update()
  68. {
  69. RotationCamera();
  70. }
  71. private void OnDestroy()
  72. {
  73. if (GameMain.Event.Check(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy))
  74. {
  75. GameMain.Event.Unsubscribe(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy);
  76. }
  77. if (GameMain.Event.Check(ChangeDevicesModelEvent.EventId, OnChangeModel))
  78. {
  79. GameMain.Event.Unsubscribe(ChangeDevicesModelEvent.EventId, OnChangeModel);
  80. }
  81. if (GameMain.Event.Check(ChangeModelTypeEvent.EventId, OnChangeModelType))
  82. {
  83. GameMain.Event.Unsubscribe(ChangeModelTypeEvent.EventId, OnChangeModelType);
  84. }
  85. }
  86. private void OnChangeModel(object sender, GameEventArgs e)
  87. {
  88. ChangeDevicesModelEvent args = (ChangeDevicesModelEvent)e;
  89. ResetCamera();
  90. for (int i = 0; i < machineList.Length; i++)
  91. {
  92. if (machineList[i].name != args.data.modelName)
  93. {
  94. machineList[i].SetActive(false);
  95. }
  96. else
  97. {
  98. HttpGlobal.SendLogToServer("参观了设备:" + args.data.name);
  99. machineList[i].SetActive(true);
  100. var ani = machineList[i].GetComponent<Animator>();
  101. if (ani != null)
  102. {
  103. ani.SetFloat("speed", 0);
  104. ani.Play(machineList[i].name, 0, 0);
  105. }
  106. currentModelIndex = i;
  107. }
  108. }
  109. }
  110. private void OnChangeModelType(object sender, GameEventArgs e)
  111. {
  112. ChangeModelTypeEvent args = (ChangeModelTypeEvent)e;
  113. var ani = machineList[currentModelIndex].GetComponent<Animator>();
  114. if (ani != null)
  115. {
  116. ani.SetFloat("speed", args.type == 0 ? -1 : 1);
  117. Debug.Log(ani.speed);
  118. ani.Play(machineList[currentModelIndex].name, 0, args.type == 0 ? 1 : 0);
  119. }
  120. }
  121. public void ResetCamera()
  122. {
  123. targetPosition = Vector3.zero;
  124. targetDistance = distance;
  125. }
  126. void RotationCamera()
  127. {
  128. // 获取鼠标输入
  129. Vector3 currentMousePosition = Input.mousePosition;
  130. if (EventSystem.current.IsPointerOverGameObject())
  131. {
  132. isOnUI = true;
  133. }
  134. else
  135. {
  136. isOnUI = false;
  137. }
  138. if (Input.GetMouseButtonDown(0) && !isOnUI)
  139. {
  140. lastMousePosition = currentMousePosition;
  141. isRotating = true;
  142. }
  143. else if (Input.GetMouseButtonUp(0))
  144. {
  145. isRotating = false;
  146. }
  147. // 如果正在旋转
  148. if (isRotating)
  149. {
  150. // 计算鼠标移动量
  151. Vector3 mouseDelta = currentMousePosition - lastMousePosition;
  152. // 计算摄像机绕点的旋转
  153. float rotationX = mouseDelta.y * rotationSpeed * Time.deltaTime;
  154. float rotationY = -mouseDelta.x * rotationSpeed * Time.deltaTime;
  155. // 应用旋转阻尼效果
  156. rotationX *= rotationDamping;
  157. rotationY *= rotationDamping;
  158. // 计算新的旋转速度
  159. currentVelocity = new Vector3(rotationX, rotationY, 0);
  160. // 应用旋转
  161. Camera.main.transform.RotateAround(targetPosition, Vector3.up, currentVelocity.y);
  162. Camera.main.transform.RotateAround(targetPosition, Camera.main.transform.right, currentVelocity.x);
  163. // 更新鼠标位置
  164. lastMousePosition = currentMousePosition;
  165. }
  166. else
  167. {
  168. // 增强旋转阻尼效果
  169. currentVelocity *= rotationDamping;
  170. Camera.main.transform.RotateAround(targetPosition, Vector3.up, currentVelocity.y);
  171. Camera.main.transform.RotateAround(targetPosition, Camera.main.transform.right, currentVelocity.x);
  172. }
  173. if (!isOnUI)
  174. {
  175. // 使用鼠标中键来控制摄像机距离
  176. float scroll = Input.GetAxis("Mouse ScrollWheel");
  177. targetDistance = Mathf.Clamp(targetDistance - scroll * zoomSpeed, minDistance, maxDistance);
  178. }
  179. // 应用缩放阻尼效果来平滑改变摄像机距离
  180. distance = Mathf.Lerp(distance, targetDistance, Time.deltaTime * zoomDamping);
  181. // 更新摄像机位置
  182. Vector3 dirToTarget = (Camera.main.transform.position - targetPosition).normalized;
  183. Camera.main.transform.position = targetPosition + dirToTarget * distance;
  184. }
  185. }