ArrowTips_EyeMode.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 朝向提示UI
  6. /// </summary>
  7. public class ArrowTips_EyeMode : MonoBehaviour
  8. {
  9. private Transform _arrow_Rot;
  10. public bool _openTips = false;
  11. public Transform _target;
  12. public Camera _camera;
  13. private float targetAngle;
  14. public float lerpIndex = 0.9f;
  15. private void Awake()
  16. {
  17. Init();
  18. }
  19. public void Init()
  20. {
  21. _arrow_Rot = this.transform.GetChild(0).Find("Arrow_Eye_Rot");
  22. if (!_openTips)
  23. {
  24. _arrow_Rot.gameObject.SetActive(false);
  25. }
  26. }
  27. // Update is called once per frame
  28. void Update()
  29. {
  30. if (_openTips)
  31. {
  32. ArrowController();
  33. }
  34. }
  35. public void ArrowController()
  36. {
  37. if (_target == null) return;
  38. if (!_target.gameObject.activeSelf && _arrow_Rot.gameObject.activeSelf)
  39. {
  40. _arrow_Rot.gameObject.SetActive(false);
  41. }
  42. else if (_target.gameObject.activeSelf && !_arrow_Rot.gameObject.activeSelf)
  43. {
  44. _arrow_Rot.gameObject.SetActive(true);
  45. }
  46. if (!_target.gameObject.activeSelf) return;
  47. //物体转化为摄像机屏幕坐标
  48. Vector3 screenPointInV3 = _camera.WorldToScreenPoint(_target.position);
  49. Vector2 screenPoint = new Vector2(screenPointInV3.x, screenPointInV3.y);
  50. if (screenPointInV3.z <= 0) //物体在摄像机后方
  51. {
  52. Vector3 screenCenterPos = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
  53. float angle = 90;
  54. if (Vector3.Cross(Vector3.up, (screenPointInV3 - screenCenterPos)).z < 0)
  55. {
  56. angle = -angle;
  57. }
  58. targetAngle = -angle;
  59. }
  60. else if (!_camera.pixelRect.Contains(screenPoint)) //摄像机正面且不在画面中
  61. {
  62. Vector3 screenCenterPos = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
  63. float angle = Vector3.Angle(Vector3.up, (screenPointInV3 - screenCenterPos));
  64. if (Vector3.Cross(Vector3.up, (screenPointInV3 - screenCenterPos)).z > 0)
  65. {
  66. angle = -angle;
  67. }
  68. targetAngle = -angle;
  69. if (!_arrow_Rot.gameObject.activeSelf)
  70. {
  71. _arrow_Rot.gameObject.SetActive(true);
  72. _arrow_Rot.transform.localRotation = Quaternion.Euler(0, 0, targetAngle);
  73. }
  74. }
  75. else
  76. {
  77. _arrow_Rot.gameObject.SetActive(false);
  78. }
  79. //_Arrow朝向是反的
  80. _arrow_Rot.transform.localRotation = Quaternion.Lerp(_arrow_Rot.transform.localRotation, Quaternion.Euler(0, 0, targetAngle), lerpIndex);
  81. }
  82. public void OpenEyeTips(Transform t)
  83. {
  84. _target = t;
  85. if (t != null)
  86. {
  87. _openTips = true;
  88. }
  89. }
  90. public void CloseEyeTips()
  91. {
  92. _target = null;
  93. _openTips = false;
  94. }
  95. }