Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class ExampleBehaviour : MonoBehaviour
  4. {
  5. private float mTouchBeginTime;
  6. private bool mTouchEnded = true;
  7.  
  8. void Update()
  9. {
  10. DetectInput();
  11. }
  12.  
  13. private void DetectInput()
  14. {
  15. if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) {
  16. // On mobile phones
  17. if (Input.touchCount == 1) {
  18. Touch touch = Input.GetTouch(0);
  19. if (touch.phase == TouchPhase.Began) {
  20. mTouchBeginTime = Time.time;
  21. mTouchEnded = false;
  22. } else if (touch.phase == TouchPhase.Moved) {
  23. mTouchEnded = true;
  24. } else if (touch.phase == TouchPhase.Stationary) {
  25. if (!mTouchEnded && Time.time - mTouchBeginTime > 1f) {
  26. OnLongPress();
  27. mTouchEnded = true;
  28. }
  29. } else if (touch.phase == TouchPhase.Ended) {
  30. if (!mTouchEnded && Time.time - mTouchBeginTime <= 1f) {
  31. OnShortPress();
  32. mTouchEnded = true;
  33. }
  34. }
  35. }
  36. } else {
  37. // On PC
  38. if (Input.GetMouseButtonDown(0)) {
  39. mTouchBeginTime = Time.time;
  40. mTouchEnded = false;
  41. }
  42.  
  43. if (!mTouchEnded && Time.time - mTouchBeginTime > 1f) {
  44. OnLongPress();
  45. mTouchEnded = true;
  46. }
  47.  
  48. if (Input.GetMouseButtonUp(0)) {
  49. if (!mTouchEnded && Time.time - mTouchBeginTime <= 1f) {
  50. OnShortPress();
  51. mTouchEnded = true;
  52. }
  53. }
  54. }
  55. }
  56.  
  57. private void OnShortPress()
  58. {
  59. // Handle short press ...
  60. }
  61.  
  62. private void OnLongPress()
  63. {
  64. // Handle long press ...
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement