CrossHairOutBoundChecker.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class CrossHairOutBoundChecker : MonoBehaviour
  6. {
  7. [SerializeField] GameObject outTip;
  8. string[] tips = {
  9. "瞄准方向已超出视野范围,请将弓往右移动!",
  10. "瞄准方向已超出视野范围,请将弓往左移动!",
  11. "瞄准方向已超出视野范围,请将弓往下移动!",
  12. "瞄准方向已超出视野范围,请将弓往上移动!",
  13. };
  14. int tipIndex = -1;
  15. void Update()
  16. {
  17. Rect pr = CrossHair.ins.parentRTF.rect;
  18. Vector3 lp = CrossHair.ins.transform.localPosition;
  19. int newTipIndex = -1;
  20. if (lp.x < -pr.width / 2) {
  21. newTipIndex = 0;
  22. } else if (lp.x > pr.width / 2) {
  23. newTipIndex = 1;
  24. } else if (lp.y > pr.height / 2) {
  25. newTipIndex = 2;
  26. } else if (lp.y < -pr.height / 2) {
  27. newTipIndex = 3;
  28. }
  29. if (newTipIndex != tipIndex) {
  30. tipIndex = newTipIndex;
  31. if (tipIndex >= 0) {
  32. outTip.GetComponent<Text>().text = tips[tipIndex];
  33. }
  34. }
  35. if (newTipIndex >= 0) {
  36. if (!outTip.activeSelf) outTip.SetActive(true);
  37. } else {
  38. if (outTip.activeSelf) outTip.SetActive(false);
  39. }
  40. }
  41. }