InputHelper.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. /// <summary>
  5. /// Input helper.
  6. /// SOURCE: http://answers.unity3d.com/answers/956579/view.html
  7. /// </summary>
  8. public static class InputHelper
  9. {
  10. private static TouchCreator lastFakeTouch;
  11. public static List<Touch> GetTouches()
  12. {
  13. List<Touch> touches = new List<Touch>();
  14. touches.AddRange(Input.touches);
  15. // Uncomment if you want it only to allow mouse swipes in the Unity Editor
  16. //#if UNITY_EDITOR
  17. if (lastFakeTouch == null)
  18. {
  19. lastFakeTouch = new TouchCreator();
  20. }
  21. if (Input.GetMouseButtonDown(0))
  22. {
  23. lastFakeTouch.phase = TouchPhase.Began;
  24. lastFakeTouch.deltaPosition = new Vector2(0, 0);
  25. lastFakeTouch.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
  26. lastFakeTouch.fingerId = 0;
  27. }
  28. else if (Input.GetMouseButtonUp(0))
  29. {
  30. lastFakeTouch.phase = TouchPhase.Ended;
  31. Vector2 newPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
  32. lastFakeTouch.deltaPosition = newPosition - lastFakeTouch.position;
  33. lastFakeTouch.position = newPosition;
  34. lastFakeTouch.fingerId = 0;
  35. }
  36. else if (Input.GetMouseButton(0))
  37. {
  38. lastFakeTouch.phase = TouchPhase.Moved;
  39. Vector2 newPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
  40. lastFakeTouch.deltaPosition = newPosition - lastFakeTouch.position;
  41. lastFakeTouch.position = newPosition;
  42. lastFakeTouch.fingerId = 0;
  43. }
  44. else
  45. {
  46. lastFakeTouch = null;
  47. }
  48. if (lastFakeTouch != null)
  49. {
  50. touches.Add(lastFakeTouch.Create());
  51. }
  52. // Uncomment if you want it only to allow mouse swipes in the Unity Editor
  53. //#endif
  54. return touches;
  55. }
  56. }