CrossHair.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. /* 准心对象 */
  7. public class CrossHair : MonoBehaviour
  8. {
  9. public static CrossHair ins;
  10. private Image image = null;
  11. private bool open = false;
  12. private bool visiable = false;
  13. private bool onlyShow = true;
  14. private GameObject infraredGuiderObj;
  15. void Awake()
  16. {
  17. ins = this;
  18. }
  19. void OnDestroy()
  20. {
  21. if (ins == this) ins = null;
  22. }
  23. void Start()
  24. {
  25. this.image = this.GetComponent<Image>();
  26. // this.open = UserSettings.ins.openCrossHair;
  27. this.visiable = this.image.enabled;
  28. //this.SetVisiable(false);
  29. SetOpen(UserSettings.ins.openCrossHair);
  30. //如果是靶子场景,把准心改为黑色
  31. if (SceneManager.GetActiveScene().name.Equals("Game")) {
  32. this.image.color = Color.black;
  33. infraredGuiderObj = FindObjectOfType<InfraredGuider>()?.gameObject;
  34. }
  35. }
  36. void Update()
  37. {
  38. //如果是新手指引的情况,或者校准时候,一定
  39. //infraredGuiderObj != null && infraredGuiderObj.activeSelf == true ||
  40. //准心校准时候延迟一秒
  41. if (AutoResetView.ins != null)
  42. {
  43. SetVisiable(true);
  44. }
  45. else {
  46. //按照条件显示图标
  47. if (open) SetVisiable(onlyShow && ArmBow.ins && ArmBow.ins.IsCanShoot());
  48. }
  49. }
  50. void SetVisiable(bool value)
  51. {
  52. if (this.visiable == value) return;
  53. this.visiable = value;
  54. this.GetComponent<Image>().enabled = this.visiable;
  55. }
  56. public void SetOpen(bool open) {
  57. this.open = open;
  58. if (!this.open) {
  59. SetVisiable(false);
  60. }
  61. }
  62. public bool GetOpen() {
  63. return this.open;
  64. }
  65. public void SetOnlyShow(bool onlyShow)
  66. {
  67. this.onlyShow = onlyShow;
  68. if (!this.onlyShow)
  69. {
  70. SetVisiable(false);
  71. }
  72. }
  73. public bool GetOnlyShow()
  74. {
  75. return this.onlyShow;
  76. }
  77. public RaycastHit GetRaycastHit() {
  78. float maxDistance = 100;
  79. int layerMask = 1 << 8; //TargetLayerMask
  80. RaycastHit raycastHit;
  81. Ray ray;
  82. if (Camera.main.name.EndsWith("Fixed")) {
  83. ray = new Ray(BowCamera.ins.transform.position, BowCamera.ins.transform.forward);
  84. } else {
  85. ray = Camera.main.ScreenPointToRay(this.transform.position, Camera.MonoOrStereoscopicEye.Mono);
  86. }
  87. Physics.Raycast(ray.origin, ray.direction, out raycastHit, maxDistance, layerMask);
  88. return raycastHit;
  89. }
  90. #region 固定相机更新
  91. RectTransform _parentRTF;
  92. RectTransform parentRTF {
  93. get {
  94. if (!_parentRTF) {
  95. _parentRTF = transform.parent.GetComponent<RectTransform>();
  96. }
  97. return _parentRTF;
  98. }
  99. }
  100. Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
  101. Vector3 targetPosition;
  102. public void UpdatePostionWhenFixedCamera() {
  103. if (!visiable) return;
  104. Transform transform = BowCamera.ins.transform;
  105. Ray ray = new Ray(transform.position, transform.forward);
  106. RaycastHit hitInfo;
  107. if (Physics.Raycast(ray, out hitInfo, 100)) {
  108. targetPosition = hitInfo.point;
  109. } else {
  110. targetPosition = transform.position + transform.forward * 100f;
  111. }
  112. Vector3 v3 = Camera.main.WorldToViewportPoint(targetPosition) - centerPoint;
  113. v3.x *= parentRTF.rect.width;
  114. v3.y *= parentRTF.rect.height;
  115. v3.z = 0;
  116. this.transform.localPosition = v3;
  117. action_UpdatePostionWhenFixedCamera?.Invoke();
  118. }
  119. public System.Action action_UpdatePostionWhenFixedCamera;
  120. #endregion
  121. }