FireInputController.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5. /// <summary>
  6. /// Fire input controller is responsible for detecting clicks through NGUI.
  7. /// This resides on a gameObject with a box collider inside the UI hierarchy.
  8. /// The collider should be at the back of the UI hierarchy. This will allow
  9. /// pickups and buttons to have higher priority than firing.
  10. /// </summary>
  11. //[RequireComponent(typeof(BoxCollider))]
  12. public class FireInputController : MonoBehaviour
  13. {
  14. bool _isTakingInput = true;
  15. bool _hasMouseBeenPressed;
  16. private PointerEventData _data;
  17. public Canvas Canvas;
  18. static CanvasScaler _canvasScaler;
  19. public static Vector3 UnscaleEventDelta(Vector3 vec)
  20. {
  21. Vector2 referenceResolution = _canvasScaler.referenceResolution;
  22. Vector2 currentResolution = new Vector2(Screen.width, Screen.height);
  23. float widthRatio = currentResolution.x / referenceResolution.x;
  24. float heightRatio = currentResolution.y / referenceResolution.y;
  25. float ratio = Mathf.Lerp(widthRatio, heightRatio, _canvasScaler.matchWidthOrHeight);
  26. return vec / ratio;
  27. }
  28. void Awake()
  29. {
  30. _canvasScaler = Canvas.GetComponent<CanvasScaler>();
  31. NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
  32. NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  33. NotificationCenter.AddListener(OnStartReplay, NotificationType.StartReplay);
  34. NotificationCenter.AddListener(OnEndReplay, NotificationType.EndReplay);
  35. }
  36. void OnDestroy()
  37. {
  38. NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
  39. NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  40. NotificationCenter.RemoveListener(OnStartReplay, NotificationType.StartReplay);
  41. NotificationCenter.RemoveListener(OnEndReplay, NotificationType.EndReplay);
  42. }
  43. public void OnPress(bool isPressed)
  44. {
  45. if (isPressed &&
  46. _isTakingInput)
  47. {
  48. #if LEVEL_EDITOR
  49. //ignore clicks for the level editor controls
  50. if ((Input.mousePosition.x < 250 && Input.mousePosition.y > Screen.height - 200) ||
  51. (Input.mousePosition.y < 50)) //for the scroll bar
  52. {
  53. return;
  54. }
  55. #endif
  56. //process it in FixedUpdate for recording ticks purposes and playing back 100% reliably
  57. _hasMouseBeenPressed = true;
  58. }
  59. }
  60. public void OnBeginDrag(PointerEventData data)
  61. {
  62. _data = data;
  63. Debug.Log("OnBeginDrag: " + data.position);
  64. }
  65. public void CustomFixedUpdate()
  66. {
  67. if (_hasMouseBeenPressed/* && _data!=null*/)
  68. {
  69. var vec = UnscaleEventDelta(Input.mousePosition);
  70. NotificationCenter.Post(NotificationType.Fire, new Vector2(vec.x, vec.y));
  71. _hasMouseBeenPressed = false;
  72. }
  73. }
  74. void OnGameStart(Notification note)
  75. {
  76. _isTakingInput = false;
  77. }
  78. void OnStartSpawningEnemies(Notification note)
  79. {
  80. _isTakingInput = true;
  81. }
  82. void OnStartReplay(Notification note)
  83. {
  84. _isTakingInput = false;
  85. }
  86. void OnEndReplay(Notification note)
  87. {
  88. _isTakingInput = true;
  89. }
  90. }