Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class ControlsScript
- {
- private const float MIN_SWIPE_TIME = 0.2f;
- private static float _TouchTime = 0;
- private static bool _IsSwiping;
- public static bool IsCameraMoving;
- public static bool SelectNewPosition(UnityEngine.Camera camera, ref Vector3 position)
- {
- if(_IsSwiping || IsCameraMoving)
- {
- return false;
- }
- #if UNITY_IOS || UNITY_ANDROID
- if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
- {
- position = camera.ScreenToWorldPoint(Input.GetTouch(0).position);
- return true;
- }
- #endif
- #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
- if (Input.GetMouseButtonUp(0))
- {
- position = camera.ScreenToWorldPoint(Input.mousePosition);
- return true;
- }
- #endif
- return false;
- }
- public static bool Select(UnityEngine.Camera camera, GameObject character)
- {
- #if UNITY_IOS || UNITY_ANDROID
- if (Input.touchCount > 0)
- {
- return CastRay(camera, character);
- }
- #endif
- #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
- if (Input.GetMouseButtonUp(0))
- {
- return CastRay(camera, character);
- }
- #endif
- return false;
- }
- private static bool CastRay(UnityEngine.Camera camera, GameObject character)
- {
- Ray ray = camera.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, 100))
- {
- return hit.collider.gameObject == character;
- }
- return false;
- }
- public static void DetectSwipe(ref Vector3 startPosition,
- ref Vector3 currentPosition, ref bool isSwiping)
- {
- if (IsCameraMoving)
- {
- _IsSwiping = false;
- return;
- }
- #if UNITY_IOS || UNITY_ANDROID
- if (Input.touchCount > 0)
- {
- var touch = Input.GetTouch(0);
- _TouchTime += Time.deltaTime;
- switch (touch.phase)
- {
- case TouchPhase.Began:
- ClearTouch(ref isSwiping);
- startPosition = UnityEngine.Camera.main.ScreenToWorldPoint(touch.position);
- break;
- case TouchPhase.Moved:
- SwipeMove(ref startPosition, ref currentPosition, ref isSwiping);
- break;
- case TouchPhase.Ended:
- ClearTouch(ref isSwiping);
- break;
- }
- }
- #endif
- #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
- if (Input.GetMouseButtonDown(0))
- {
- startPosition = UnityEngine.Camera.main.ScreenToWorldPoint(Input.mousePosition);
- ClearTouch(ref isSwiping);
- }
- if (Input.GetMouseButton(0))
- {
- _TouchTime += Time.deltaTime;
- SwipeMove(ref startPosition, ref currentPosition, ref isSwiping);
- }
- if (Input.GetMouseButtonUp(0))
- {
- ClearTouch(ref isSwiping);
- }
- #endif
- _IsSwiping = isSwiping;
- }
- private static void ClearTouch(ref bool isSwiping)
- {
- isSwiping = false;
- _TouchTime = 0;
- }
- private static void SwipeMove(ref Vector3 startPosition,
- ref Vector3 currentPosition, ref bool isSwiping)
- {
- currentPosition = UnityEngine.Camera.main.ScreenToWorldPoint(Input.mousePosition);
- if (_TouchTime > MIN_SWIPE_TIME)
- {
- isSwiping = true;
- }
- if (_TouchTime > MIN_SWIPE_TIME * 1.5f)
- {
- startPosition = currentPosition;
- _TouchTime = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement