CrossHair.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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) SetVisiable(onlyShow && ArmBow.ins && ArmBow.ins.IsCanShoot());
  52. }
  53. }
  54. void SetVisiable(bool value)
  55. {
  56. if (this.visiable == value) return;
  57. this.visiable = value;
  58. this.GetComponent<Image>().enabled = this.visiable;
  59. }
  60. public void SetOpen(bool open) {
  61. this.open = open;
  62. if (!this.open) {
  63. SetVisiable(false);
  64. }
  65. }
  66. public bool GetOpen() {
  67. return this.open;
  68. }
  69. public void SetOnlyShow(bool onlyShow)
  70. {
  71. this.onlyShow = onlyShow;
  72. if (!this.onlyShow)
  73. {
  74. SetVisiable(false);
  75. }
  76. }
  77. public bool GetOnlyShow()
  78. {
  79. return this.onlyShow;
  80. }
  81. public RaycastHit GetRaycastHit() {
  82. float maxDistance = 100;
  83. int layerMask = 1 << 8; //TargetLayerMask
  84. RaycastHit raycastHit;
  85. Ray ray;
  86. if (Camera.main.name.EndsWith("Fixed")) {
  87. ray = new Ray(BowCamera.ins.transform.position, BowCamera.ins.transform.forward);
  88. } else {
  89. ray = Camera.main.ScreenPointToRay(this.transform.position, Camera.MonoOrStereoscopicEye.Mono);
  90. }
  91. Physics.Raycast(ray.origin, ray.direction, out raycastHit, maxDistance, layerMask);
  92. return raycastHit;
  93. }
  94. #region 固定相机更新
  95. RectTransform _parentRTF;
  96. RectTransform parentRTF {
  97. get {
  98. if (!_parentRTF) {
  99. _parentRTF = transform.parent.GetComponent<RectTransform>();
  100. }
  101. return _parentRTF;
  102. }
  103. }
  104. Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
  105. Vector3 targetPosition;
  106. public void UpdatePostionWhenFixedCamera() {
  107. if (!visiable) return;
  108. Transform transform = BowCamera.ins.transform;
  109. Ray ray = new Ray(transform.position, transform.forward);
  110. RaycastHit hitInfo;
  111. if (Physics.Raycast(ray, out hitInfo, 100)) {
  112. targetPosition = hitInfo.point;
  113. } else {
  114. targetPosition = transform.position + transform.forward * 100f;
  115. }
  116. Vector3 v3 = Camera.main.WorldToViewportPoint(targetPosition) - centerPoint;
  117. v3.x *= parentRTF.rect.width;
  118. v3.y *= parentRTF.rect.height;
  119. v3.z = 0;
  120. this.transform.localPosition = v3;
  121. action_UpdatePostionWhenFixedCamera?.Invoke();
  122. }
  123. public System.Action action_UpdatePostionWhenFixedCamera;
  124. #endregion
  125. }