CameraBirdSec.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. void Start()
  37. {
  38. // 计算偏移量
  39. offset = transform.position - target.position;
  40. }
  41. private bool GetPointerOverUIElement(out GameObject uiElement)
  42. {
  43. uiElement = null;
  44. PointerEventData pointerEventData = new PointerEventData(EventSystem.current)
  45. {
  46. position = Input.mousePosition
  47. };
  48. RaycastResult raycastResult = new RaycastResult();
  49. List<RaycastResult> results = new List<RaycastResult>();
  50. GraphicRaycaster raycaster = canvas.GetComponent<GraphicRaycaster>();
  51. if (raycaster != null)
  52. {
  53. raycaster.Raycast(pointerEventData, results);
  54. if (results.Count > 0)
  55. {
  56. raycastResult = results[0];
  57. uiElement = raycastResult.gameObject;
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. private bool IsPointerOverUIElement()
  64. {
  65. bool specialUI = false;
  66. if (GetPointerOverUIElement(out GameObject uiElement))
  67. {
  68. // 获取 UI 元素的类型
  69. if (uiElement.TryGetComponent<Button>(out Button button))
  70. {
  71. if (uiElement.transform.TryGetComponent<RuntimePoint>(out RuntimePoint rp)) {
  72. specialUI = true;
  73. }
  74. }
  75. }
  76. // 检查当前鼠标位置是否在 UI 上
  77. return EventSystem.current.IsPointerOverGameObject() && !specialUI;
  78. }
  79. void LateUpdate()
  80. {
  81. onUI = false;
  82. if (IsPointerOverUIElement()) onUI = true;
  83. translateSpeed = currentDistance / 1000.0f;
  84. // 鼠标滚轮控制摄像头远近
  85. if (Input.GetAxis("Mouse ScrollWheel") != 0 && !onUI)
  86. {
  87. if (detailLod)
  88. {
  89. scrollSensitivity = math.clamp(currentDistance, 0, 10);
  90. // 更新当前距离
  91. currentDistance -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
  92. // 确保距离在允许的范围内
  93. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  94. //this.GetComponent<DepthFog>().Height = 400000 / currentDistance;
  95. OnDistanceChange?.Invoke(currentDistance);
  96. }
  97. else
  98. {
  99. OnDistanceChange?.Invoke(Input.GetAxis("Mouse ScrollWheel"));
  100. }
  101. if (Input.GetAxis("Mouse ScrollWheel") > 0)
  102. {
  103. //Debug.Log($"{Input.mousePosition} {Screen.width*0.5f}:{Screen.height*0.5f}");
  104. float offSetX = Screen.width * 0.5f-Input.mousePosition.x;
  105. float offSetY = Screen.height * 0.5f-Input.mousePosition.y;
  106. Vector3 moveDirection = Vector3.down * offSetY*translateSpeed + Vector3.left * offSetX*translateSpeed;
  107. // 平滑移动摄像头到目标位置
  108. float cameraDistanceRange = Mathf.Clamp(currentDistance * 1.0f / maxDistance * 1.0f, 0.5f, 0.9f);
  109. target.position += moveDirection.normalized*cameraDistanceRange;
  110. //限制移动范围
  111. if (fixMoveRange)
  112. {
  113. Vector3 finalPos=target.localPosition;
  114. finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
  115. finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
  116. target.localPosition = finalPos;
  117. }
  118. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, 0, 0)) * offset.normalized * currentDistance;
  119. // 摄像头平滑移动到目标位置
  120. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed*2);
  121. }
  122. }
  123. // 鼠标左键拖拽平移摄像头
  124. if (Input.GetMouseButtonDown(0) && !onUI)
  125. {
  126. beginDrag?.Invoke();
  127. isDragging = true;
  128. lastMousePosition = Input.mousePosition;
  129. }
  130. else if (Input.GetMouseButtonUp(0))
  131. {
  132. isDragging = false;
  133. }
  134. if (isDragging)
  135. {
  136. // 计算鼠标移动的偏移量
  137. Vector3 mouseOffset = Input.mousePosition - lastMousePosition;
  138. // 根据偏移量计算摄像头的目标位置
  139. Vector3 moveDirection = Vector3.down * mouseOffset.y * translateSpeed + Vector3.left * mouseOffset.x * translateSpeed;
  140. // 平滑移动摄像头到目标位置
  141. target.position += moveDirection;
  142. //限制移动范围
  143. if (fixMoveRange)
  144. {
  145. Vector3 finalPos=target.localPosition;
  146. finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
  147. finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
  148. target.localPosition = finalPos;
  149. }
  150. // 计算目标位置
  151. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, 0, 0)) * offset.normalized * currentDistance;
  152. // 摄像头平滑移动到目标位置
  153. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed * 2);
  154. }
  155. else
  156. {
  157. if (isRotate)
  158. {
  159. Blink();
  160. }
  161. else
  162. {
  163. // 计算目标位置
  164. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  165. // 摄像头平滑移动到目标位置
  166. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed);
  167. //if (Vector3.Distance(transform.position, targetPosition) < 1) {
  168. // transform.position = targetPosition;
  169. //}
  170. }
  171. }
  172. lastMousePosition = Input.mousePosition;
  173. }
  174. public void Blink()
  175. {
  176. // 计算目标位置
  177. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  178. // 摄像头平滑移动到目标位置
  179. transform.position = targetPosition;
  180. //摄像头朝向目标对象
  181. transform.rotation = Quaternion.LookRotation(target.position - this.transform.position, Vector3.up);
  182. }
  183. public void SetRange(float _minX,float _maxX,float _minY,float _maxY)
  184. {
  185. min_X = _minX;
  186. max_X = _maxX;
  187. min_Y = _minY;
  188. max_Y = _maxY;
  189. //限制移动范围
  190. if (fixMoveRange)
  191. {
  192. Vector3 finalPos=target.localPosition;
  193. finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
  194. finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
  195. target.localPosition = finalPos;
  196. }
  197. }
  198. public void SetCameraToCenterFade(Vector3 centerPos,float distance = 300)
  199. {
  200. MeshRenderer fader = this.transform.GetChild(0).GetComponent<MeshRenderer>();
  201. fader.gameObject.SetActive(true);
  202. fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
  203. {
  204. fader.material.DOColor(Color.clear, 1.5f).onComplete = () =>
  205. {
  206. fader.gameObject.SetActive(false);
  207. };
  208. };
  209. this.target.localPosition = centerPos;
  210. DOTween.To(()=>this.currentDistance,x=>this.currentDistance=x, distance,0.5f);
  211. this.Blink();
  212. }
  213. void Update()
  214. {
  215. }
  216. }