Advertisement
Guest User

Untitled

a guest
May 1st, 2016
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. public static class ControlsScript
  2. {
  3. private const float MIN_SWIPE_TIME = 0.2f;
  4. private static float _TouchTime = 0;
  5. private static bool _IsSwiping;
  6. public static bool IsCameraMoving;
  7. public static bool SelectNewPosition(UnityEngine.Camera camera, ref Vector3 position)
  8. {
  9. if(_IsSwiping || IsCameraMoving)
  10. {
  11. return false;
  12. }
  13. #if UNITY_IOS || UNITY_ANDROID
  14. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
  15. {
  16. position = camera.ScreenToWorldPoint(Input.GetTouch(0).position);
  17. return true;
  18. }
  19. #endif
  20. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
  21. if (Input.GetMouseButtonUp(0))
  22. {
  23. position = camera.ScreenToWorldPoint(Input.mousePosition);
  24. return true;
  25. }
  26. #endif
  27. return false;
  28. }
  29. public static bool Select(UnityEngine.Camera camera, GameObject character)
  30. {
  31. #if UNITY_IOS || UNITY_ANDROID
  32. if (Input.touchCount > 0)
  33. {
  34. return CastRay(camera, character);
  35. }
  36. #endif
  37. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
  38. if (Input.GetMouseButtonUp(0))
  39. {
  40. return CastRay(camera, character);
  41. }
  42. #endif
  43. return false;
  44. }
  45. private static bool CastRay(UnityEngine.Camera camera, GameObject character)
  46. {
  47. Ray ray = camera.ScreenPointToRay(Input.mousePosition);
  48. RaycastHit hit;
  49. if (Physics.Raycast(ray, out hit, 100))
  50. {
  51.  
  52. return hit.collider.gameObject == character;
  53. }
  54. return false;
  55. }
  56. public static void DetectSwipe(ref Vector3 startPosition,
  57. ref Vector3 currentPosition, ref bool isSwiping)
  58. {
  59. if (IsCameraMoving)
  60. {
  61. _IsSwiping = false;
  62. return;
  63. }
  64. #if UNITY_IOS || UNITY_ANDROID
  65. if (Input.touchCount > 0)
  66. {
  67. var touch = Input.GetTouch(0);
  68. _TouchTime += Time.deltaTime;
  69. switch (touch.phase)
  70. {
  71. case TouchPhase.Began:
  72. ClearTouch(ref isSwiping);
  73. startPosition = UnityEngine.Camera.main.ScreenToWorldPoint(touch.position);
  74. break;
  75. case TouchPhase.Moved:
  76. SwipeMove(ref startPosition, ref currentPosition, ref isSwiping);
  77. break;
  78. case TouchPhase.Ended:
  79. ClearTouch(ref isSwiping);
  80. break;
  81. }
  82. }
  83. #endif
  84. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
  85. if (Input.GetMouseButtonDown(0))
  86. {
  87. startPosition = UnityEngine.Camera.main.ScreenToWorldPoint(Input.mousePosition);
  88. ClearTouch(ref isSwiping);
  89. }
  90. if (Input.GetMouseButton(0))
  91. {
  92. _TouchTime += Time.deltaTime;
  93. SwipeMove(ref startPosition, ref currentPosition, ref isSwiping);
  94. }
  95. if (Input.GetMouseButtonUp(0))
  96. {
  97. ClearTouch(ref isSwiping);
  98. }
  99. #endif
  100. _IsSwiping = isSwiping;
  101. }
  102. private static void ClearTouch(ref bool isSwiping)
  103. {
  104. isSwiping = false;
  105. _TouchTime = 0;
  106. }
  107. private static void SwipeMove(ref Vector3 startPosition,
  108. ref Vector3 currentPosition, ref bool isSwiping)
  109. {
  110. currentPosition = UnityEngine.Camera.main.ScreenToWorldPoint(Input.mousePosition);
  111. if (_TouchTime > MIN_SWIPE_TIME)
  112. {
  113. isSwiping = true;
  114. }
  115. if (_TouchTime > MIN_SWIPE_TIME * 1.5f)
  116. {
  117. startPosition = currentPosition;
  118. _TouchTime = 0;
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement