PinchToScale.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if ENABLE_INPUT_SYSTEM
  5. using UnityEngine.InputSystem;
  6. #endif
  7. namespace Imagine.WebAR
  8. {
  9. public class PinchToScale : MonoBehaviour
  10. {
  11. [SerializeField] Transform scaleTransform;
  12. Vector3 origScale, startScale;
  13. Vector2 touch0StartPos, touch1StartPos;
  14. [SerializeField] float minScale = 0.1f;
  15. [SerializeField] float maxScale = 10f;
  16. bool isPinching = false;
  17. void Awake(){
  18. origScale = scaleTransform.localScale;
  19. }
  20. void Start()
  21. {
  22. #if ENABLE_LEGACY_INPUT_MANAGER
  23. Input.multiTouchEnabled = true;
  24. #endif
  25. }
  26. #if ENABLE_INPUT_SYSTEM
  27. void Update()
  28. {
  29. if (Touchscreen.current != null)
  30. {
  31. var activeTouchCount = 0;
  32. for (int i = 0; i < Touchscreen.current.touches.Count; i++)
  33. {
  34. var touchPhase = Touchscreen.current.touches[i].phase.ReadValue();
  35. if (touchPhase == UnityEngine.InputSystem.TouchPhase.Began ||
  36. touchPhase == UnityEngine.InputSystem.TouchPhase.Moved ||
  37. touchPhase == UnityEngine.InputSystem.TouchPhase.Stationary)
  38. {
  39. activeTouchCount++;
  40. }
  41. }
  42. if(activeTouchCount < 2){
  43. return;
  44. }
  45. }
  46. if (Touchscreen.current != null)
  47. {
  48. var touch0 = Touchscreen.current.touches[0];
  49. var touch1 = Touchscreen.current.touches[1];
  50. if (touch0.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Began ||
  51. touch1.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Began)
  52. {
  53. touch0StartPos = touch0.position.ReadValue();
  54. touch1StartPos = touch1.position.ReadValue();
  55. isPinching = true;
  56. startScale = scaleTransform.localScale;
  57. Debug.Log("Start Pinch");
  58. }
  59. else if (touch0.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Ended ||
  60. touch1.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Ended ||
  61. touch0.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Canceled ||
  62. touch1.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Canceled)
  63. {
  64. isPinching = false;
  65. Debug.Log("End Pinch");
  66. }
  67. if (isPinching)
  68. {
  69. var dStart = (touch1StartPos - touch0StartPos).magnitude;
  70. var pos0 = touch0.position.ReadValue();
  71. var pos1 = touch1.position.ReadValue();
  72. var d = (pos1 - pos0).magnitude;
  73. // var scale = Mathf.Clamp(d / dStart, minScale, maxScale);
  74. // scaleTransform.localScale = startScale * scale;
  75. scaleTransform.localScale = startScale * d / dStart;
  76. scaleTransform.localScale = new Vector3(
  77. Mathf.Clamp(scaleTransform.localScale.x, origScale.x * minScale, origScale.x * maxScale),
  78. Mathf.Clamp(scaleTransform.localScale.y, origScale.y * minScale, origScale.y * maxScale),
  79. Mathf.Clamp(scaleTransform.localScale.z, origScale.z * minScale, origScale.z * maxScale)
  80. );
  81. //Debug.Log("Pinch " + scale.ToString("0.00") + "x");
  82. }
  83. }
  84. else
  85. {
  86. isPinching = false;
  87. }
  88. }
  89. #else
  90. void Update()
  91. {
  92. if (Input.touchCount == 2)
  93. {
  94. var touch0 = Input.GetTouch(0);
  95. var touch1 = Input.GetTouch(1);
  96. if (touch0.phase == TouchPhase.Began ||
  97. touch1.phase == TouchPhase.Began)
  98. {
  99. touch0StartPos = touch0.position;
  100. touch1StartPos = touch1.position;
  101. isPinching = true;
  102. startScale = scaleTransform.localScale;
  103. Debug.Log("Start Pinch");
  104. }
  105. else if (touch0.phase == TouchPhase.Ended ||
  106. touch1.phase == TouchPhase.Ended ||
  107. touch0.phase == TouchPhase.Canceled ||
  108. touch1.phase == TouchPhase.Canceled)
  109. {
  110. isPinching = false;
  111. Debug.Log("End Pinch");
  112. }
  113. if (isPinching)
  114. {
  115. var dStart = (touch1StartPos - touch0StartPos).magnitude;
  116. var pos0 = touch0.position;
  117. var pos1 = touch1.position;
  118. var d = (pos1 - pos0).magnitude;
  119. // var scale = Mathf.Clamp(d / dStart, minScale, maxScale);
  120. // scaleTransform.localScale = startScale * scale;
  121. scaleTransform.localScale = startScale * d / dStart;
  122. scaleTransform.localScale = new Vector3(
  123. Mathf.Clamp(scaleTransform.localScale.x, origScale.x * minScale, origScale.x * maxScale),
  124. Mathf.Clamp(scaleTransform.localScale.y, origScale.y * minScale, origScale.y * maxScale),
  125. Mathf.Clamp(scaleTransform.localScale.z, origScale.z * minScale, origScale.z * maxScale)
  126. );
  127. //Debug.Log("Pinch " + scale.ToString("0.00") + "x");
  128. }
  129. }
  130. else
  131. {
  132. isPinching = false;
  133. }
  134. }
  135. #endif
  136. public void Reset()
  137. {
  138. scaleTransform.localScale = origScale;
  139. }
  140. }
  141. }