TwoFingerPan.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Runtime.InteropServices;
  5. #if ENABLE_INPUT_SYSTEM
  6. using UnityEngine.InputSystem;
  7. #endif
  8. namespace Imagine.WebAR
  9. {
  10. public class TwoFingerPan : MonoBehaviour
  11. {
  12. [DllImport("__Internal")] private static extern void WebGLSetViewportPos(string vStr);
  13. [SerializeField] Transform panTransform;
  14. [SerializeField] Camera cam;
  15. Vector3 origPos;// startPos;
  16. Vector2 startViewportPos;
  17. Vector2 touch0StartPos, touch1StartPos;
  18. // [SerializeField] float sensitivity = 0.01f;
  19. bool isPanning = false;
  20. void Start()
  21. {
  22. #if ENABLE_LEGACY_INPUT_MANAGER
  23. Input.multiTouchEnabled = true;
  24. #endif
  25. origPos = panTransform.localPosition;
  26. }
  27. #if ENABLE_INPUT_SYSTEM
  28. void Update()
  29. {
  30. if (Touchscreen.current != null)
  31. {
  32. var activeTouchCount = 0;
  33. for (int i = 0; i < Touchscreen.current.touches.Count; i++)
  34. {
  35. var touchPhase = Touchscreen.current.touches[i].phase.ReadValue();
  36. if (touchPhase == UnityEngine.InputSystem.TouchPhase.Began ||
  37. touchPhase == UnityEngine.InputSystem.TouchPhase.Moved ||
  38. touchPhase == UnityEngine.InputSystem.TouchPhase.Stationary)
  39. {
  40. activeTouchCount++;
  41. }
  42. }
  43. if(activeTouchCount < 2){
  44. return;
  45. }
  46. }
  47. if (Touchscreen.current != null)
  48. {
  49. var touch0 = Touchscreen.current.touches[0];
  50. var touch1 = Touchscreen.current.touches[1];
  51. if (touch0.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Began ||
  52. touch1.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Began)
  53. {
  54. touch0StartPos = touch0.position.ReadValue();
  55. touch1StartPos = touch1.position.ReadValue();
  56. isPanning = true;
  57. // startPos = panTransform.localPosition;
  58. startViewportPos = cam.WorldToViewportPoint(panTransform.position);
  59. Debug.Log("Start Pan");
  60. }
  61. else if (touch0.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Ended ||
  62. touch1.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Ended ||
  63. touch0.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Canceled ||
  64. touch1.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Canceled)
  65. {
  66. isPanning = false;
  67. Debug.Log("End Pan");
  68. }
  69. if (isPanning)
  70. {
  71. var centerStart = (touch1StartPos + touch0StartPos) / 2;
  72. var pos0 = touch0.position.ReadValue();
  73. var pos1 = touch1.position.ReadValue();
  74. var centerEnd = (pos1 + pos0) / 2;
  75. var offset = centerEnd - centerStart;
  76. offset.x /= Screen.width;
  77. offset.y /= Screen.height;
  78. var newViewportPos = startViewportPos + offset;
  79. WebGLSetViewportPos(
  80. newViewportPos.x + "," +
  81. newViewportPos.y
  82. );
  83. // var worldOffset = cam.right * offset.x + cam.up * offset.y;
  84. // panTransform.localPosition = startPos + worldOffset * sensitivity * -1;
  85. // //Debug.Log("Move " + offset + "->" + worldOffset);
  86. }
  87. }
  88. else
  89. {
  90. isPanning = false;
  91. }
  92. }
  93. #else
  94. void Update()
  95. {
  96. if (Input.touchCount == 2)
  97. {
  98. var touch0 = Input.GetTouch(0);
  99. var touch1 = Input.GetTouch(1);
  100. if (touch0.phase == TouchPhase.Began ||
  101. touch1.phase == TouchPhase.Began)
  102. {
  103. touch0StartPos = touch0.position;
  104. touch1StartPos = touch1.position;
  105. isPanning = true;
  106. // startPos = panTransform.localPosition;
  107. startViewportPos = cam.WorldToViewportPoint(panTransform.position);
  108. Debug.Log("Start Pan");
  109. }
  110. else if (touch0.phase == TouchPhase.Ended ||
  111. touch1.phase == TouchPhase.Ended ||
  112. touch0.phase == TouchPhase.Canceled ||
  113. touch1.phase == TouchPhase.Canceled)
  114. {
  115. isPanning = false;
  116. Debug.Log("End Pan");
  117. }
  118. if (isPanning)
  119. {
  120. var centerStart = (touch1StartPos + touch0StartPos) / 2;
  121. var pos0 = touch0.position;
  122. var pos1 = touch1.position;
  123. var centerEnd = (pos1 + pos0) / 2;
  124. var offset = centerEnd - centerStart;
  125. offset.x /= Screen.width;
  126. offset.y /= Screen.height;
  127. var newViewportPos = startViewportPos + offset;
  128. WebGLSetViewportPos(
  129. newViewportPos.x + "," +
  130. newViewportPos.y
  131. );
  132. // var worldOffset = cam.right * offset.x + cam.up * offset.y;
  133. // panTransform.localPosition = startPos + worldOffset * sensitivity * -1;
  134. // //Debug.Log("Move " + offset + "->" + worldOffset);
  135. }
  136. }
  137. else
  138. {
  139. isPanning = false;
  140. }
  141. }
  142. #endif
  143. public void Reset()
  144. {
  145. panTransform.localPosition = origPos;
  146. }
  147. }
  148. }