CrossHair.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. //弓箭还是默认黑色图标
  33. if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  34. {
  35. this.image.color = Color.black;
  36. }
  37. infraredGuiderObj = FindObjectOfType<InfraredGuider>()?.gameObject;
  38. }
  39. }
  40. void Update()
  41. {
  42. //如果是新手指引的情况,或者校准时候,一定
  43. //infraredGuiderObj != null && infraredGuiderObj.activeSelf == true ||
  44. //准心校准时候延迟一秒
  45. if (AutoResetView.ins != null)
  46. {
  47. SetVisiable(true);
  48. }
  49. else {
  50. //按照条件显示图标
  51. if (open)
  52. {
  53. SetVisiable(onlyShow && ArmBow.ins && ArmBow.ins.IsCanShoot());
  54. }
  55. else
  56. {
  57. SetVisiable(false);
  58. }
  59. }
  60. }
  61. void SetVisiable(bool value)
  62. {
  63. if (this.visiable == value) return;
  64. this.visiable = value;
  65. this.GetComponent<Image>().enabled = this.visiable;
  66. }
  67. public void SetOpen(bool open) {
  68. this.open = open;
  69. if (!this.open) {
  70. SetVisiable(false);
  71. }
  72. }
  73. public bool GetOpen() {
  74. return this.open;
  75. }
  76. public void SetOnlyShow(bool onlyShow)
  77. {
  78. this.onlyShow = onlyShow;
  79. if (!this.onlyShow)
  80. {
  81. SetVisiable(false);
  82. }
  83. }
  84. public bool GetOnlyShow()
  85. {
  86. return this.onlyShow;
  87. }
  88. public RaycastHit GetRaycastHit() {
  89. float maxDistance = 100;
  90. int layerMask = 1 << 8; //TargetLayerMask
  91. RaycastHit raycastHit;
  92. Ray ray;
  93. if (Camera.main.name.EndsWith("Fixed")) {
  94. ray = new Ray(BowCamera.ins.transform.position, BowCamera.ins.transform.forward);
  95. } else {
  96. ray = Camera.main.ScreenPointToRay(this.transform.position, Camera.MonoOrStereoscopicEye.Mono);
  97. }
  98. Physics.Raycast(ray.origin, ray.direction, out raycastHit, maxDistance, layerMask);
  99. return raycastHit;
  100. }
  101. #region 固定相机更新
  102. RectTransform _parentRTF;
  103. RectTransform parentRTF {
  104. get {
  105. if (!_parentRTF) {
  106. _parentRTF = transform.parent.GetComponent<RectTransform>();
  107. }
  108. return _parentRTF;
  109. }
  110. }
  111. Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
  112. Vector3 targetPosition;
  113. public void UpdatePostionWhenFixedCamera() {
  114. if (!visiable) return;
  115. Transform transform = BowCamera.ins.transform;
  116. Ray ray = new Ray(transform.position, transform.forward);
  117. RaycastHit hitInfo;
  118. if (Physics.Raycast(ray, out hitInfo, 100)) {
  119. targetPosition = hitInfo.point;
  120. } else {
  121. targetPosition = transform.position + transform.forward * 100f;
  122. }
  123. Vector3 v3 = Camera.main.WorldToViewportPoint(targetPosition) - centerPoint;
  124. v3.x *= parentRTF.rect.width;
  125. v3.y *= parentRTF.rect.height;
  126. v3.z = 0;
  127. this.transform.localPosition = v3;
  128. action_UpdatePostionWhenFixedCamera?.Invoke();
  129. }
  130. public System.Action action_UpdatePostionWhenFixedCamera;
  131. #endregion
  132. }