PortalsCountDownUI.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. public class PortalsCountDownUI : MonoBehaviour
  6. {
  7. public CameraController cameraController;
  8. public Vector3 portalPos; //传送门位置
  9. public TextMeshProUGUI[] text; //倒计时数字
  10. public GameObject[] effect; //UI指示器
  11. public GameObject[] countDown; //text和effect的父物体
  12. public Vector2 parentRect; //父物体Rect Transform的长宽的一半
  13. public float hypotenuse; //父物体Rect Transform斜边的长度
  14. public float myRect; //自己的边长的一半
  15. private void Start()
  16. {
  17. hypotenuse = parentRect.magnitude;
  18. cameraController = GameObject.Find("View").GetComponent<CameraController>();
  19. }
  20. private void Update()
  21. {
  22. refreshPortalUIPos(cameraController.mainCamera, 0,
  23. -parentRect + new Vector2(myRect, myRect),
  24. parentRect - new Vector2(myRect, myRect));
  25. if (countDown[1].activeSelf)
  26. {
  27. countDown[1].SetActive(false);
  28. }
  29. //if (cameraController.isSplit)
  30. //{
  31. // Camera camera0 = cameraController.player0Camera;
  32. // Camera camera1 = cameraController.player1Camera;
  33. // refreshPortalUIPos(
  34. // camera0, 0,
  35. // new Vector2(25 + (camera0.rect.x - 0.5f) * parentRect.x * 2 + myRect,
  36. // - parentRect.y + myRect),
  37. // new Vector2(-25 + camera0.rect.x * parentRect.x * 2 - myRect,
  38. // parentRect.y - myRect));
  39. // refreshPortalUIPos(
  40. // camera1, 1,
  41. // new Vector2(25 + (camera1.rect.x - 0.5f) * parentRect.x * 2 + myRect,
  42. // - parentRect.y + myRect),
  43. // new Vector2(-25 + camera1.rect.x * parentRect.x * 2 - myRect,
  44. // parentRect.y - myRect));
  45. //}
  46. //else
  47. //{
  48. // refreshPortalUIPos(cameraController.mainCamera, 0,
  49. // - parentRect + new Vector2(myRect, myRect),
  50. // parentRect - new Vector2(myRect, myRect));
  51. // if (countDown[1].activeSelf)
  52. // {
  53. // countDown[1].SetActive(false);
  54. // }
  55. //}
  56. }
  57. //刷新指示箭头显示
  58. void refreshPortalUIPos(Camera camera,int id,Vector2 RectClampMin,Vector2 RectClampMax)
  59. {
  60. //计算指示箭头的方向
  61. Vector3 pos = camera.transform.position;
  62. float angle = Mathf.Atan2(portalPos.y - pos.y, portalPos.x - pos.x) * Mathf.Rad2Deg - 90;
  63. effect[id].transform.rotation = Quaternion.Euler(Vector3.forward * angle);
  64. //判断传送门是否在屏幕内
  65. Vector3 viewportPoint = camera.WorldToViewportPoint(portalPos);
  66. if (viewportPoint.x >= 0 && viewportPoint.y >= 0
  67. && viewportPoint.x <= camera.rect.width && viewportPoint.y <= 1
  68. && viewportPoint.z > camera.nearClipPlane)
  69. {
  70. if (countDown[id].activeSelf)
  71. {
  72. countDown[id].SetActive(false);
  73. }
  74. }
  75. else
  76. {
  77. if (!countDown[id].activeSelf)
  78. {
  79. countDown[id].SetActive(true);
  80. }
  81. //计算指示箭头的位置
  82. Vector3 target = new Vector3(portalPos.x - pos.x, portalPos.y - pos.y, 0);
  83. target = target.normalized * hypotenuse;
  84. target.x = Mathf.Clamp(target.x, RectClampMin.x, RectClampMax.x);
  85. target.y = Mathf.Clamp(target.y, RectClampMin.y, RectClampMax.y);
  86. countDown[id].transform.localPosition = target;
  87. }
  88. }
  89. }