TapToReposition.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine.EventSystems;
  7. #if ENABLE_INPUT_SYSTEM
  8. using UnityEngine.InputSystem;
  9. #endif
  10. namespace Imagine.WebAR{
  11. public class TapToReposition : MonoBehaviour
  12. {
  13. [DllImport("__Internal")] private static extern void WebGLSetViewportPos(string vStr);
  14. private Vector2 startTapPos;
  15. private WorldTracker wt;
  16. private bool clicking = false;
  17. void Start()
  18. {
  19. wt = FindObjectOfType<WorldTracker>();
  20. }
  21. #if ENABLE_INPUT_SYSTEM
  22. void Update()
  23. {
  24. if( EventSystem.current.IsPointerOverGameObject() ||
  25. !wt.mainObject.activeInHierarchy
  26. ){
  27. return;
  28. }
  29. if ((Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame) ||
  30. (Touchscreen.current != null && Touchscreen.current.primaryTouch.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Began))
  31. {
  32. if (Application.isMobilePlatform && Touchscreen.current != null){
  33. startTapPos = Touchscreen.current.primaryTouch.position.ReadValue();
  34. }
  35. else if(Mouse.current != null)
  36. {
  37. startTapPos = Mouse.current.position.ReadValue();
  38. }
  39. clicking = true;
  40. }
  41. else if (clicking && ((Mouse.current != null && Mouse.current.leftButton.wasReleasedThisFrame) ||
  42. (Touchscreen.current != null && Touchscreen.current.primaryTouch.phase.ReadValue() == UnityEngine.InputSystem.TouchPhase.Ended)))
  43. {
  44. var endTapPos = new Vector2();
  45. if (Application.isMobilePlatform && Touchscreen.current != null){
  46. endTapPos = Touchscreen.current.primaryTouch.position.ReadValue();
  47. }
  48. else if(Mouse.current != null)
  49. {
  50. endTapPos = Mouse.current.position.ReadValue();
  51. }
  52. var distance = Vector2.Distance(startTapPos, endTapPos);
  53. if(distance < 10){
  54. WebGLSetViewportPos(
  55. endTapPos.x / Screen.width + "," +
  56. endTapPos.y / Screen.height
  57. );
  58. }
  59. // else{
  60. // Debug.Log("distance is too big = " + distance);
  61. // }
  62. clicking = false;
  63. }
  64. }
  65. #else
  66. void Update()
  67. {
  68. if( EventSystem.current.IsPointerOverGameObject() ||
  69. !wt.mainObject.activeInHierarchy
  70. )
  71. return;
  72. if (Input.GetMouseButtonDown(0))
  73. {
  74. startTapPos = Input.mousePosition;
  75. }
  76. else if (Input.GetMouseButtonUp(0))
  77. {
  78. if(Vector2.Distance(startTapPos, Input.mousePosition) < 3){
  79. WebGLSetViewportPos(
  80. Input.mousePosition.x / Screen.width + "," +
  81. Input.mousePosition.y / Screen.height
  82. );
  83. }
  84. }
  85. }
  86. #endif
  87. }
  88. }