CrossHairOutBoundChecker1.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /* 检测器-检测游戏射击准心是否出屏幕边界 (打鸭子、塔防关卡、水果大人) */
  6. public class CrossHairOutBoundChecker1 : MonoBehaviour
  7. {
  8. [SerializeField] GameObject outTip;
  9. string[] tips = null;
  10. int tipIndex = -1;
  11. void Awake()
  12. {
  13. outTip.SetActive(false);
  14. }
  15. void Start() {
  16. tips = new string[]{
  17. TextAutoLanguage2.GetTextByKey("game_crosshair_outbound_0"),
  18. TextAutoLanguage2.GetTextByKey("game_crosshair_outbound_1"),
  19. TextAutoLanguage2.GetTextByKey("game_crosshair_outbound_2"),
  20. TextAutoLanguage2.GetTextByKey("game_crosshair_outbound_3"),
  21. };
  22. }
  23. void Update()
  24. {
  25. int newTipIndex = GetNetTipIndex();
  26. if (newTipIndex != tipIndex) {
  27. tipIndex = newTipIndex;
  28. if (tipIndex >= 0) {
  29. outTip.GetComponent<Text>().text = tips[tipIndex];
  30. }
  31. }
  32. if (newTipIndex >= 0) {
  33. if (!outTip.activeSelf) outTip.SetActive(true);
  34. } else {
  35. if (outTip.activeSelf) outTip.SetActive(false);
  36. }
  37. }
  38. [SerializeField] public RectTransform crossHair;
  39. public System.Func<int> FetchOutBoundIndex;
  40. int GetNetTipIndex()
  41. {
  42. if (crossHair)
  43. {
  44. if (crossHair.gameObject.activeInHierarchy)
  45. {
  46. Vector3 pos = crossHair.position;
  47. if (pos.x < 3)
  48. {
  49. return 0;
  50. }
  51. if (pos.x > Screen.width - 3)
  52. {
  53. return 1;
  54. }
  55. if (pos.y > Screen.height - 3)
  56. {
  57. return 2;
  58. }
  59. if (pos.y < 3)
  60. {
  61. return 3;
  62. }
  63. }
  64. }
  65. else if (FetchOutBoundIndex != null)
  66. {
  67. return FetchOutBoundIndex.Invoke();
  68. }
  69. return -1;
  70. }
  71. }