ParticleMgrCtrl.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using GameFramework.Event;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.Playables;
  8. public class ParticleMgrCtrl : MonoBehaviour
  9. {
  10. public static ParticleMgrCtrl _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. public Vector3 targetPosition; // 旋转点
  18. private Vector3 lastMousePosition; // 上一帧的鼠标位置
  19. private Vector3 currentVelocity; // 当前旋转速度
  20. private float distance = 30.0f; // 初始摄像机距离
  21. private float targetDistance;
  22. private bool isRotating = false; // 是否正在旋转
  23. private bool isOnUI = false;
  24. public GameObject[] particleList;
  25. public int currentModelIndex = 0;
  26. public PlayableDirector director;
  27. public int currentBaseStudyType { get; private set; }
  28. private bool havePlay = false;
  29. private void Awake()
  30. {
  31. _Instance = this;
  32. particleList = new GameObject[this.transform.childCount];
  33. for (int i = 0; i < this.transform.childCount; i++)
  34. {
  35. particleList[i] = this.transform.GetChild(i).gameObject;
  36. }
  37. }
  38. void Start()
  39. {
  40. targetDistance = distance;
  41. GameMain.Event.Subscribe(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy);
  42. GameMain.Event.Subscribe(Change3DTopoTimeLineEvent.EventId, OnDealTimeline);
  43. GameMain.Event.Subscribe(Stop3DTopoTimeLineEvent.EventId, OnStopTimeline);
  44. }
  45. private void OnDealTimeline(object sender, GameEventArgs e)
  46. {
  47. Change3DTopoTimeLineEvent args = (Change3DTopoTimeLineEvent)e;
  48. switch (args.type)
  49. {
  50. case 3:
  51. director.Play();
  52. havePlay = true;
  53. director.stopped += (director) => { if (havePlay) { havePlay = false; GameMain.Event.Fire(this,Stop3DTopoTimeLineEvent.Create()); } };
  54. break;
  55. }
  56. }
  57. private void OnStopTimeline(object sender, GameEventArgs e)
  58. {
  59. Stop3DTopoTimeLineEvent args = (Stop3DTopoTimeLineEvent)e;
  60. if (havePlay)
  61. {
  62. director.time = director.duration - 4;
  63. havePlay = false;
  64. }
  65. }
  66. private void OnChangeBaseStudy(object sender, GameEventArgs e)
  67. {
  68. ChangeBaseStudyTypeEvent args = (ChangeBaseStudyTypeEvent)e;
  69. if (args.type != currentBaseStudyType)
  70. {
  71. switch (args.type)
  72. {
  73. case 0:
  74. this.gameObject.SetActive(false);
  75. break;
  76. case 1:
  77. this.gameObject.SetActive(true);
  78. break;
  79. case 2:
  80. this.gameObject.SetActive(false);
  81. break;
  82. case 3:
  83. this.gameObject.SetActive(false);
  84. break;
  85. }
  86. currentBaseStudyType = args.type;
  87. }
  88. }
  89. // Update is called once per frame
  90. void Update()
  91. {
  92. RotationCamera();
  93. }
  94. private void OnDestroy()
  95. {
  96. GameMain.Event.Unsubscribe(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy);
  97. GameMain.Event.Unsubscribe(Change3DTopoTimeLineEvent.EventId, OnDealTimeline);
  98. GameMain.Event.Unsubscribe(Stop3DTopoTimeLineEvent.EventId, OnStopTimeline);
  99. }
  100. public void ResetCamera(Vector3 newPos)
  101. {
  102. Vector3 dir = newPos - targetPosition;
  103. targetPosition = newPos;
  104. targetDistance = distance;
  105. Camera.main.transform.position += dir;
  106. }
  107. void RotationCamera()
  108. {
  109. // 获取鼠标输入
  110. Vector3 currentMousePosition = Input.mousePosition;
  111. if (EventSystem.current.IsPointerOverGameObject())
  112. {
  113. isOnUI = true;
  114. }
  115. else
  116. {
  117. isOnUI = false;
  118. }
  119. if (Input.GetMouseButtonDown(0) && !isOnUI)
  120. {
  121. lastMousePosition = currentMousePosition;
  122. isRotating = true;
  123. }
  124. else if (Input.GetMouseButtonUp(0))
  125. {
  126. isRotating = false;
  127. }
  128. // 如果正在旋转
  129. if (isRotating)
  130. {
  131. // 计算鼠标移动量
  132. Vector3 mouseDelta = currentMousePosition - lastMousePosition;
  133. // 计算摄像机绕点的旋转
  134. float rotationX = mouseDelta.y * rotationSpeed * Time.deltaTime;
  135. float rotationY = -mouseDelta.x * rotationSpeed * Time.deltaTime;
  136. // 应用旋转阻尼效果
  137. rotationX *= rotationDamping;
  138. rotationY *= rotationDamping;
  139. // 计算新的旋转速度
  140. currentVelocity = new Vector3(rotationX, rotationY, 0);
  141. // 应用旋转
  142. Camera.main.transform.RotateAround(targetPosition, Vector3.up, currentVelocity.y);
  143. Camera.main.transform.RotateAround(targetPosition, Camera.main.transform.right, currentVelocity.x);
  144. // 更新鼠标位置
  145. lastMousePosition = currentMousePosition;
  146. }
  147. else
  148. {
  149. // 增强旋转阻尼效果
  150. currentVelocity *= rotationDamping;
  151. Camera.main.transform.RotateAround(targetPosition, Vector3.up, currentVelocity.y);
  152. Camera.main.transform.RotateAround(targetPosition, Camera.main.transform.right, currentVelocity.x);
  153. }
  154. if (!isOnUI)
  155. {
  156. // 使用鼠标中键来控制摄像机距离
  157. float scroll = Input.GetAxis("Mouse ScrollWheel");
  158. targetDistance = Mathf.Clamp(targetDistance - scroll * zoomSpeed, minDistance, maxDistance);
  159. }
  160. // 应用缩放阻尼效果来平滑改变摄像机距离
  161. distance = Mathf.Lerp(distance, targetDistance, Time.deltaTime * zoomDamping);
  162. // 更新摄像机位置
  163. Vector3 dirToTarget = (Camera.main.transform.position - targetPosition).normalized;
  164. Camera.main.transform.position = targetPosition + dirToTarget * distance;
  165. }
  166. }