SwipeToRotateY.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. #if ENABLE_INPUT_SYSTEM
  6. using UnityEngine.InputSystem;
  7. #endif
  8. namespace Imagine.WebAR
  9. {
  10. public class SwipeToRotateY : MonoBehaviour
  11. {
  12. [SerializeField] private Transform rotTransform;
  13. //TODO: implement using Screen.DPI / Canvas.innerWidth.Height API grabber
  14. private Vector2 startDragPos;
  15. private bool isDragging = false;
  16. private Quaternion origRot, startRot;
  17. [SerializeField] private float sensitivity = 20;
  18. void Awake()
  19. {
  20. origRot = rotTransform.rotation;
  21. }
  22. #if ENABLE_INPUT_SYSTEM
  23. void Update()
  24. {
  25. if (Touchscreen.current != null)
  26. {
  27. var activeTouchCount = 0;
  28. for (int i = 0; i < Touchscreen.current.touches.Count; i++)
  29. {
  30. var touchPhase = Touchscreen.current.touches[i].phase.ReadValue();
  31. if (touchPhase == UnityEngine.InputSystem.TouchPhase.Began ||
  32. touchPhase == UnityEngine.InputSystem.TouchPhase.Moved ||
  33. touchPhase == UnityEngine.InputSystem.TouchPhase.Stationary)
  34. {
  35. activeTouchCount++;
  36. }
  37. }
  38. if(activeTouchCount > 1){
  39. isDragging = false;
  40. return;
  41. }
  42. // var touchPhase = Touchscreen.current.primaryTouch.phase.ReadValue();
  43. // Debug.Log($"primaryTouch {Touchscreen.current.primaryTouch.phase.ReadValue()} Began={touchPhase == UnityEngine.InputSystem.TouchPhase.Began}");
  44. }
  45. if ((Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame) ||
  46. (Touchscreen.current != null && Touchscreen.current.primaryTouch.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Began))
  47. {
  48. // Debug.Log("clicked!");
  49. if (Application.isMobilePlatform && Touchscreen.current != null){
  50. // Debug.Log($"primaryTouch Began at {Touchscreen.current.primaryTouch.position.ReadValue()}");
  51. startDragPos = Touchscreen.current.primaryTouch.position.ReadValue();
  52. // Debug.Log("touch here!" + startDragPos);
  53. }
  54. else if(Mouse.current != null)
  55. {
  56. startDragPos = Mouse.current.position.ReadValue();
  57. // Debug.Log("mouse here!" + startDragPos);
  58. }
  59. startRot = rotTransform.rotation;
  60. isDragging = true;
  61. }
  62. else if ((Mouse.current != null && Mouse.current.leftButton.wasReleasedThisFrame) ||
  63. (Touchscreen.current != null && Touchscreen.current.primaryTouch.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Ended))
  64. {
  65. isDragging = false;
  66. }
  67. if (isDragging)
  68. {
  69. Vector2 curDragPos = new Vector2();
  70. if (Application.isMobilePlatform && Touchscreen.current != null){
  71. curDragPos = Touchscreen.current.primaryTouch.position.ReadValue();
  72. // Debug.Log($"touch curDragPos = {curDragPos.x}");
  73. }
  74. else if(Mouse.current != null)
  75. {
  76. curDragPos = Mouse.current.position.ReadValue();
  77. // Debug.Log($"mouse curDragPos = {curDragPos.x}");
  78. }
  79. var x = curDragPos.x - startDragPos.x;
  80. var a = x * sensitivity * -1;
  81. rotTransform.rotation = startRot * Quaternion.AngleAxis(a, Vector3.up);
  82. }
  83. }
  84. #else
  85. void Update()
  86. {
  87. if (Input.touchCount > 1)
  88. {
  89. isDragging = false;
  90. return;
  91. }
  92. if (Input.GetMouseButtonDown(0))
  93. {
  94. startDragPos = Input.mousePosition;
  95. startRot = rotTransform.rotation;
  96. isDragging = true;
  97. }
  98. else if (Input.GetMouseButtonUp(0))
  99. {
  100. isDragging = false;
  101. }
  102. if (isDragging)
  103. {
  104. var curDragPos = Input.mousePosition;
  105. var x = curDragPos.x - startDragPos.x;
  106. var a = x * sensitivity * -1;
  107. rotTransform.rotation = startRot * Quaternion.AngleAxis(a, Vector3.up);
  108. }
  109. }
  110. #endif
  111. public void Reset()
  112. {
  113. rotTransform.rotation = origRot;
  114. }
  115. }
  116. }