help me please

Updated on January 8, 2019 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on January 8, 2019

hello everyone,

 

i maked a scrollview with images in the content and i used this script to pinch zoom the images, when i zoom-in it’s okey but when zoom-out the image don’t have limite.

i want to make the scrollview lik this

 

help

 

  • Liked by
Reply
1 on January 8, 2019

Can we see what you have so far?

 

on January 8, 2019
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class PinchableScrollRect : ScrollRect
  6. {
  7. [SerializeField] float _minZoom = .1f;
  8. [SerializeField] float _maxZoom = 10;
  9. [SerializeField] float _zoomLerpSpeed = 10f;
  10. float _currentZoom = 1;
  11. bool _isPinching = false;
  12. float _startPinchDist;
  13. float _startPinchZoom;
  14. Vector2 _startPinchCenterPosition;
  15. Vector2 _startPinchScreenPosition;
  16. float _mouseWheelSensitivity = 1;
  17. bool blockPan = false;
  18. protected override void Awake()
  19. {
  20. Input.multiTouchEnabled = true;
  21. }
  22. private void Update()
  23. {
  24. if (Input.touchCount == 2)
  25. {
  26. if (!_isPinching)
  27. {
  28. _isPinching = true;
  29. OnPinchStart();
  30. }
  31. OnPinch();
  32. }
  33. else
  34. {
  35. _isPinching = false;
  36. if (Input.touchCount == 0)
  37. {
  38. blockPan = false;
  39. }
  40. }
  41. //pc input
  42. float scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");
  43. if (Mathf.Abs(scrollWheelInput) > float.Epsilon)
  44. {
  45. _currentZoom *= 1 + scrollWheelInput * _mouseWheelSensitivity;
  46. _currentZoom = Mathf.Clamp(_currentZoom, _minZoom, _maxZoom);
  47. _startPinchScreenPosition = (Vector2)Input.mousePosition;
  48. RectTransformUtility.ScreenPointToLocalPointInRectangle(content, _startPinchScreenPosition, null, out _startPinchCenterPosition);
  49. Vector2 pivotPosition = new Vector3(content.pivot.x * content.rect.size.x, content.pivot.y * content.rect.size.y);
  50. Vector2 posFromBottomLeft = pivotPosition + _startPinchCenterPosition;
  51. SetPivot(content, new Vector2(posFromBottomLeft.x / content.rect.width, posFromBottomLeft.y / content.rect.height));
  52. }
  53. //pc input end
  54. if (Mathf.Abs(content.localScale.x - _currentZoom) > 0.001f)
  55. content.localScale = Vector3.Lerp(content.localScale, Vector3.one * _currentZoom, _zoomLerpSpeed * Time.deltaTime);
  56. }
  57. protected override void SetContentAnchoredPosition(Vector2 position)
  58. {
  59. if (_isPinching || blockPan) return;
  60. base.SetContentAnchoredPosition(position);
  61. }
  62. void OnPinchStart()
  63. {
  64. Vector2 pos1 = Input.touches[0].position;
  65. Vector2 pos2 = Input.touches[1].position;
  66. _startPinchDist = Distance(pos1, pos2) * content.localScale.x;
  67. _startPinchZoom = _currentZoom;
  68. _startPinchScreenPosition = (pos1 + pos2) / 2;
  69. RectTransformUtility.ScreenPointToLocalPointInRectangle(content, _startPinchScreenPosition, null, out _startPinchCenterPosition);
  70. Vector2 pivotPosition = new Vector3(content.pivot.x * content.rect.size.x, content.pivot.y * content.rect.size.y);
  71. Vector2 posFromBottomLeft = pivotPosition + _startPinchCenterPosition;
  72. SetPivot(content, new Vector2(posFromBottomLeft.x / content.rect.width, posFromBottomLeft.y / content.rect.height));
  73. blockPan = true;
  74. }
  75. void OnPinch()
  76. {
  77. float currentPinchDist = Distance(Input.touches[0].position, Input.touches[1].position) * content.localScale.x;
  78. _currentZoom = (currentPinchDist / _startPinchDist) * _startPinchZoom;
  79. _currentZoom = Mathf.Clamp(_currentZoom, _minZoom, _maxZoom);
  80. }
  81. float Distance(Vector2 pos1, Vector2 pos2)
  82. {
  83. RectTransformUtility.ScreenPointToLocalPointInRectangle(content, pos1, null, out pos1);
  84. RectTransformUtility.ScreenPointToLocalPointInRectangle(content, pos2, null, out pos2);
  85. return Vector2.Distance(pos1, pos2);
  86. }
  87. static void SetPivot(RectTransform rectTransform, Vector2 pivot)
  88. {
  89. if (rectTransform == null) return;
  90. Vector2 size = rectTransform.rect.size;
  91. Vector2 deltaPivot = rectTransform.pivot - pivot;
  92. Vector3 deltaPosition = new Vector3(deltaPivot.x * size.x, deltaPivot.y * size.y) * rectTransform.localScale.x;
  93. rectTransform.pivot = pivot;
  94. rectTransform.localPosition -= deltaPosition;
  95. }
  96. }
Show more replies
  • Liked by
Reply
Cancel