CrossHair.cs 2.9 KB

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