CameraBirdSec.cs 9.2 KB

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