CrossHair.cs 3.7 KB

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