ZoomPinch.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. namespace Google.Maps.Examples.Shared {
  3. /// <summary>
  4. /// This class implements a zoom for mobile which allows the user to control the field of view
  5. /// for a perspective camera or the orthographic size for an orthographic camera with two fingers.
  6. /// </summary>
  7. public class ZoomPinch : MonoBehaviour {
  8. #region properties
  9. /// <summary>
  10. /// The rate of change every frame of the field of view (angle) in perspective mode.
  11. /// </summary>
  12. [Tooltip("The rate of change of the field of view in perspective mode every frame.")]
  13. public float PerspectiveZoomSpeed = 0.5f;
  14. /// <summary>
  15. /// The rate of change every frame of the orthographic size (meters) in orthographic mode.
  16. /// </summary>
  17. [Tooltip("The rate of change of the orthographic size in orthographic mode.")]
  18. public float OrthoZoomSpeed = 0.5f;
  19. /// <summary>
  20. /// Reference to the active camera
  21. /// </summary>
  22. public Camera ActiveCamera;
  23. /// <summary>
  24. /// Min field of view
  25. /// </summary>
  26. [Tooltip("The minimum field of view when in perspective mode.")]
  27. public float MinFieldOfView = 30f;
  28. /// <summary>
  29. /// Max field of view
  30. /// </summary>
  31. [Tooltip("The maximum field of view when in perspective mode.")]
  32. public float MaxFieldOfView = 80;
  33. /// <summary>
  34. /// Max orthographic size (half height in meters).
  35. /// (Max zoom out)
  36. /// </summary>
  37. [Tooltip("The maximum size when in orthographic mode (Max zoom out).")]
  38. public float MaxOrthographicSize = 30f;
  39. /// <summary>
  40. /// Min orthographic size (half height in meters).
  41. /// (Max zoom in)
  42. /// </summary>
  43. [Tooltip("The minimum size when in orthographic mode (Max zoom in).")]
  44. public float MinOrthographicSize = 0.1f;
  45. #endregion
  46. /// <summary>
  47. /// Set the active camera to the main camera if it isn't already set.
  48. /// </summary>
  49. private void Start() {
  50. if (ActiveCamera == null) {
  51. ActiveCamera = Camera.main;
  52. }
  53. }
  54. /// <summary>
  55. /// Detects touches on screen. You need 2 touches to perform a zoom.
  56. /// Makes adjustments to camera orthographic size or camera field of view
  57. /// depending on camera settings.
  58. ///
  59. /// </summary>
  60. private void Update() {
  61. // If there are two touches on the device...
  62. if (Input.touchCount == 2) {
  63. // Store both touches.
  64. var touchZero = Input.GetTouch(0);
  65. var touchOne = Input.GetTouch(1);
  66. // Find the position in the previous frame of each touch.
  67. Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
  68. Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
  69. // Find the magnitude of the vector (the distance) between the touches in each frame.
  70. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
  71. float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
  72. // Find the difference in the distances between each frame.
  73. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
  74. // If the camera is orthographic...
  75. if (ActiveCamera.orthographic) {
  76. // ... change the orthographic size based on the change in distance between the touches.
  77. ActiveCamera.orthographicSize += deltaMagnitudeDiff * OrthoZoomSpeed;
  78. // Make sure the orthographic size is clamped.
  79. ActiveCamera.orthographicSize = Mathf.Clamp(ActiveCamera.orthographicSize,
  80. MinOrthographicSize, MaxOrthographicSize);
  81. } else {
  82. // Otherwise change the field of view based on the change in distance between the touches.
  83. ActiveCamera.fieldOfView += deltaMagnitudeDiff * PerspectiveZoomSpeed;
  84. // Clamp the field of view to make sure it's between min and max values.
  85. ActiveCamera.fieldOfView = Mathf.Clamp(ActiveCamera.fieldOfView,
  86. MinFieldOfView, MaxFieldOfView);
  87. }
  88. }
  89. }
  90. }
  91. }