CrossHair.cs 3.8 KB

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