CrossHair.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /* 准心对象 */
  6. public class CrossHair : MonoBehaviour
  7. {
  8. public static CrossHair ins;
  9. private Image image = null;
  10. private bool open = false;
  11. private bool visiable = false;
  12. void Awake()
  13. {
  14. ins = this;
  15. }
  16. void Start()
  17. {
  18. this.image = this.GetComponent<Image>();
  19. this.open = UserSettings.ins.openCrossHair;
  20. this.visiable = this.image.enabled;
  21. this.SetVisiable(false);
  22. }
  23. void Update()
  24. {
  25. if (open) SetVisiable(ArmBow.ins && ArmBow.ins.IsCanShoot());
  26. }
  27. void SetVisiable(bool value)
  28. {
  29. if (this.visiable == value) return;
  30. this.visiable = value;
  31. this.GetComponent<Image>().enabled = this.visiable;
  32. }
  33. public void SetOpen(bool open) {
  34. this.open = open;
  35. if (!this.open) {
  36. SetVisiable(false);
  37. }
  38. }
  39. public bool GetOpen() {
  40. return this.open;
  41. }
  42. public RaycastHit GetRaycastHit() {
  43. float maxDistance = 100;
  44. int layerMask = 1 << 8;
  45. RaycastHit hit;
  46. Ray ray = Camera.main.ScreenPointToRay(this.transform.position, Camera.MonoOrStereoscopicEye.Mono);
  47. bool raycastResult = Physics.Raycast(ray.origin, ray.direction, out hit, maxDistance, layerMask);
  48. // if (raycastResult) return hit.point;
  49. return hit;
  50. }
  51. }