CrossHair.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 onlyShow = true;
  13. private bool visiable = false;
  14. void Awake()
  15. {
  16. ins = this;
  17. }
  18. void OnDestroy()
  19. {
  20. if (ins == this) ins = null;
  21. }
  22. void Start()
  23. {
  24. this.image = this.GetComponent<Image>();
  25. this.open = UserSettings.ins.openCrossHair;
  26. this.visiable = this.image.enabled;
  27. this.SetVisiable(false);
  28. //如果是靶子场景,把准心改为黑色
  29. if (SceneManager.GetActiveScene().name.Equals("Game")) {
  30. this.image.color = Color.black;
  31. }
  32. }
  33. void Update()
  34. {
  35. if (open) SetVisiable(onlyShow && ArmBow.ins && ArmBow.ins.IsCanShoot());
  36. }
  37. void SetVisiable(bool value)
  38. {
  39. if (this.visiable == value) return;
  40. this.visiable = value;
  41. this.GetComponent<Image>().enabled = this.visiable;
  42. }
  43. public void SetOpen(bool open) {
  44. this.open = open;
  45. if (!this.open) {
  46. SetVisiable(false);
  47. }
  48. }
  49. public void SetOnlyShow(bool onlyShow) {
  50. this.onlyShow = onlyShow;
  51. if (!this.onlyShow)
  52. {
  53. SetVisiable(false);
  54. }
  55. }
  56. public bool GetOnlyShow()
  57. {
  58. return this.onlyShow;
  59. }
  60. public bool GetOpen() {
  61. return this.open;
  62. }
  63. public RaycastHit GetRaycastHit() {
  64. float maxDistance = 100;
  65. int layerMask = 1 << 8; //TargetLayerMask
  66. RaycastHit raycastHit;
  67. Ray ray;
  68. if (Camera.main.name.EndsWith("Fixed")) {
  69. ray = new Ray(BowCamera.ins.transform.position, BowCamera.ins.transform.forward);
  70. } else {
  71. ray = Camera.main.ScreenPointToRay(this.transform.position, Camera.MonoOrStereoscopicEye.Mono);
  72. }
  73. Physics.Raycast(ray.origin, ray.direction, out raycastHit, maxDistance, layerMask);
  74. return raycastHit;
  75. }
  76. #region 固定相机更新
  77. RectTransform _parentRTF;
  78. RectTransform parentRTF {
  79. get {
  80. if (!_parentRTF) {
  81. _parentRTF = transform.parent.GetComponent<RectTransform>();
  82. }
  83. return _parentRTF;
  84. }
  85. }
  86. Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
  87. Vector3 targetPosition;
  88. public void UpdatePostionWhenFixedCamera() {
  89. if (!visiable) return;
  90. Transform transform = BowCamera.ins.transform;
  91. Ray ray = new Ray(transform.position, transform.forward);
  92. RaycastHit hitInfo;
  93. if (Physics.Raycast(ray, out hitInfo, 100)) {
  94. targetPosition = hitInfo.point;
  95. } else {
  96. targetPosition = transform.position + transform.forward * 100f;
  97. }
  98. Vector3 v3 = Camera.main.WorldToViewportPoint(targetPosition) - centerPoint;
  99. v3.x *= parentRTF.rect.width;
  100. v3.y *= parentRTF.rect.height;
  101. v3.z = 0;
  102. this.transform.localPosition = v3;
  103. action_UpdatePostionWhenFixedCamera?.Invoke();
  104. }
  105. public System.Action action_UpdatePostionWhenFixedCamera;
  106. #endregion
  107. }