InputUtils.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. namespace Shapes2D {
  2. using UnityEngine;
  3. using System.Collections;
  4. public class InputUtils {
  5. public static Vector3 InputToWorldPosition(Vector2 inputPos) {
  6. Vector3 pos = new Vector3(inputPos.x, inputPos.y,
  7. -Camera.main.transform.position.z);
  8. return Camera.main.ScreenToWorldPoint(pos);
  9. }
  10. static bool WasMouseDown(int button) {
  11. return Input.GetMouseButtonDown(button);
  12. }
  13. static bool WasFingerDown() {
  14. foreach (Touch t in Input.touches) {
  15. if (t.phase == TouchPhase.Began)
  16. return true;
  17. }
  18. return false;
  19. }
  20. static Vector2 FirstTouchPosition() {
  21. foreach (Touch t in Input.touches) {
  22. if (t.phase == TouchPhase.Began || t.phase == TouchPhase.Moved
  23. || t.phase == TouchPhase.Stationary)
  24. return t.position;
  25. }
  26. throw new System.InvalidOperationException("No touch exists.");
  27. }
  28. public static bool MouseDownOrTap() {
  29. #pragma warning disable 0162
  30. #if UNITY_EDITOR
  31. return WasMouseDown(0);
  32. #endif
  33. #if UNITY_IPHONE || UNITY_ANDROID
  34. return WasFingerDown();
  35. #endif
  36. return WasMouseDown(0);
  37. #pragma warning restore 0162
  38. }
  39. public static Vector2 MouseOrTapPosition() {
  40. #pragma warning disable 0162
  41. #if UNITY_EDITOR
  42. return Input.mousePosition;
  43. #endif
  44. #if UNITY_IPHONE || UNITY_ANDROID
  45. return FirstTouchPosition();
  46. #endif
  47. return Input.mousePosition;
  48. #pragma warning restore 0162
  49. }
  50. }
  51. }