CameraBirdSec.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using DG.Tweening;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public class CameraBirdSec : MonoBehaviour
  9. {
  10. public Transform target; // 目标对象,摄像头将朝向此对象
  11. public float smoothSpeed = 0.125f; // 摄像头平滑移动的速度
  12. public float scrollSensitivity = 5f; // 鼠标滚轮的灵敏度
  13. public float rotateSpeed = 0.25f;
  14. public float translateSpeed = 3f;
  15. public float minDistance = 5f; // 摄像头与目标对象的最小距离
  16. public float maxDistance = 50f; // 摄像头与目标对象的最大距离
  17. public float currentDistance; // 当前摄像头与目标对象的距离
  18. private Vector3 offset; // 摄像头与目标对象的偏移量
  19. private Vector3 velocity = Vector3.zero; // 摄像头移动的速度
  20. private bool isDragging = false; // 是否正在拖拽
  21. private Vector3 lastMousePosition; // 上一帧鼠标位置
  22. public Canvas canvas;
  23. public float rotateYAngle = 0.0f;
  24. public float rotateXAngle = 0.0f;
  25. public Action<float> OnDistanceChange;
  26. public float min_X;
  27. public float max_X;
  28. public float min_Y;
  29. public float max_Y;
  30. public bool fixMoveRange = false;
  31. bool isRotate = false;
  32. bool onUI = false;
  33. void Start()
  34. {
  35. // 初始化当前距离为初始偏移量的距离
  36. currentDistance = Vector3.Distance(transform.position, target.position);
  37. // 确保初始距离在允许的范围内
  38. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  39. // 计算偏移量
  40. offset = transform.position - target.position;
  41. OnDistanceChange?.Invoke(currentDistance);
  42. }
  43. private bool GetPointerOverUIElement(out GameObject uiElement)
  44. {
  45. uiElement = null;
  46. PointerEventData pointerEventData = new PointerEventData(EventSystem.current)
  47. {
  48. position = Input.mousePosition
  49. };
  50. RaycastResult raycastResult = new RaycastResult();
  51. List<RaycastResult> results = new List<RaycastResult>();
  52. GraphicRaycaster raycaster = canvas.GetComponent<GraphicRaycaster>();
  53. if (raycaster != null)
  54. {
  55. raycaster.Raycast(pointerEventData, results);
  56. if (results.Count > 0)
  57. {
  58. raycastResult = results[0];
  59. uiElement = raycastResult.gameObject;
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. private bool IsPointerOverUIElement()
  66. {
  67. bool specialUI = false;
  68. if (GetPointerOverUIElement(out GameObject uiElement))
  69. {
  70. // 获取 UI 元素的类型
  71. if (uiElement.TryGetComponent<Button>(out Button button))
  72. {
  73. if (uiElement.transform.parent.TryGetComponent<RuntimePoint>(out RuntimePoint rp)) {
  74. specialUI = true;
  75. }
  76. }
  77. }
  78. // 检查当前鼠标位置是否在 UI 上
  79. return EventSystem.current.IsPointerOverGameObject() && !specialUI;
  80. }
  81. void LateUpdate()
  82. {
  83. onUI = false;
  84. if (IsPointerOverUIElement()) onUI = true;
  85. // 鼠标滚轮控制摄像头远近
  86. if (Input.GetAxis("Mouse ScrollWheel") != 0 && !onUI)
  87. {
  88. // 更新当前距离
  89. currentDistance -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
  90. // 确保距离在允许的范围内
  91. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  92. //this.GetComponent<DepthFog>().Height = 400000 / currentDistance;
  93. OnDistanceChange?.Invoke(currentDistance);
  94. }
  95. if (Input.GetMouseButton(2))
  96. {
  97. rotateXAngle -= Input.GetAxis("Mouse Y") * rotateSpeed;
  98. rotateXAngle = Mathf.Clamp(rotateXAngle, -36f, 36f);
  99. isRotate = true;
  100. }
  101. else {
  102. isRotate = false;
  103. }
  104. // 鼠标左键拖拽平移摄像头
  105. if (Input.GetMouseButtonDown(0) && !onUI)
  106. {
  107. isDragging = true;
  108. lastMousePosition = Input.mousePosition;
  109. }
  110. else if (Input.GetMouseButtonUp(0))
  111. {
  112. isDragging = false;
  113. }
  114. if (isDragging)
  115. {
  116. // 计算鼠标移动的偏移量
  117. Vector3 mouseOffset = Input.mousePosition - lastMousePosition;
  118. // 根据偏移量计算摄像头的目标位置
  119. Vector3 moveDirection = Vector3.down * mouseOffset.y * translateSpeed + Vector3.left * mouseOffset.x * translateSpeed;
  120. // 平滑移动摄像头到目标位置
  121. target.position += moveDirection;
  122. //限制移动范围
  123. if (fixMoveRange)
  124. {
  125. Vector3 finalPos=target.localPosition;
  126. finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
  127. finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
  128. target.localPosition = finalPos;
  129. }
  130. // 计算目标位置
  131. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, 0, 0)) * offset.normalized * currentDistance;
  132. // 摄像头平滑移动到目标位置
  133. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed * 2);
  134. }
  135. else
  136. {
  137. if (isRotate)
  138. {
  139. Blink();
  140. }
  141. else
  142. {
  143. // 计算目标位置
  144. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  145. // 摄像头平滑移动到目标位置
  146. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed);
  147. }
  148. }
  149. lastMousePosition = Input.mousePosition;
  150. }
  151. public void Blink()
  152. {
  153. // 计算目标位置
  154. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  155. // 摄像头平滑移动到目标位置
  156. transform.position = targetPosition;
  157. //摄像头朝向目标对象
  158. transform.rotation = Quaternion.LookRotation(target.position - this.transform.position, Vector3.up);
  159. }
  160. public void SetRange(float _minX,float _maxX,float _minY,float _maxY)
  161. {
  162. min_X = _minX;
  163. max_X = _maxX;
  164. min_Y = _minY;
  165. max_Y = _maxY;
  166. //限制移动范围
  167. if (fixMoveRange)
  168. {
  169. Vector3 finalPos=target.localPosition;
  170. finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
  171. finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
  172. target.localPosition = finalPos;
  173. }
  174. }
  175. public void SetCameraToCenterFade(Vector3 centerPos,float distance = 300)
  176. {
  177. MeshRenderer fader = this.transform.GetChild(0).GetComponent<MeshRenderer>();
  178. fader.gameObject.SetActive(true);
  179. fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
  180. {
  181. fader.material.DOColor(Color.clear, 1.5f).onComplete = () =>
  182. {
  183. fader.gameObject.SetActive(false);
  184. };
  185. };
  186. this.target.localPosition = centerPos;
  187. DOTween.To(()=>this.currentDistance,x=>this.currentDistance=x, distance,0.5f);
  188. this.Blink();
  189. }
  190. void Update()
  191. {
  192. }
  193. }