Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by tamilfetcher · Sep 23, 2021 at 07:21 AM · unity 2dscrollviewzoompanpinch

zoom and pan

Can someone help me with this code here found from this forum **https://answers.unity.com/questions/1280592/pinch-and-zoom-functionality-on-canvas-ui-images.html#** the zoom in and pinch and Pan works absolutely fine but when zooming out it zooms out too much without a limit, how to limit the zooming out to the size of the original image(to zoom out max to the original scene size)

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  public class PinchableScrollRect : ScrollRect
  {
      [SerializeField] float _minZoom = .1f;
      [SerializeField] float _maxZoom = 10;
      [SerializeField] float _zoomLerpSpeed = 10f;
      float _currentZoom = 1;
      bool _isPinching = false;
      float _startPinchDist;
      float _startPinchZoom;
      Vector2 _startPinchCenterPosition;
      Vector2 _startPinchScreenPosition;
      float _mouseWheelSensitivity = 1;
      bool blockPan = false;
  
      protected override void Awake()
      {
          Input.multiTouchEnabled = true;
      }
  
      private void Update()
      {
          if (Input.touchCount == 2)
          {
              if (!_isPinching)
              {
                  _isPinching = true;
                  OnPinchStart();
              }
              OnPinch();
          }
          else
          {
              _isPinching = false;
              if (Input.touchCount == 0)
              {
                  blockPan = false;
              }
          }
          //pc input
          float scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");
          if (Mathf.Abs(scrollWheelInput) > float.Epsilon)
          {
              _currentZoom *= 1 + scrollWheelInput * _mouseWheelSensitivity;
              _currentZoom = Mathf.Clamp(_currentZoom, _minZoom, _maxZoom);
              _startPinchScreenPosition = (Vector2)Input.mousePosition;
              RectTransformUtility.ScreenPointToLocalPointInRectangle(content, _startPinchScreenPosition, null, out _startPinchCenterPosition);
              Vector2 pivotPosition = new Vector3(content.pivot.x * content.rect.size.x, content.pivot.y * content.rect.size.y);
              Vector2 posFromBottomLeft = pivotPosition + _startPinchCenterPosition;
              SetPivot(content, new Vector2(posFromBottomLeft.x / content.rect.width, posFromBottomLeft.y / content.rect.height));
          }
          //pc input end
  
          if (Mathf.Abs(content.localScale.x - _currentZoom) > 0.001f)
              content.localScale = Vector3.Lerp(content.localScale, Vector3.one * _currentZoom, _zoomLerpSpeed * Time.deltaTime);
      }
  
      protected override void SetContentAnchoredPosition(Vector2 position)
      {
          if (_isPinching || blockPan) return;
          base.SetContentAnchoredPosition(position);
      }
  
      void OnPinchStart()
      {
          Vector2 pos1 = Input.touches[0].position;
          Vector2 pos2 = Input.touches[1].position;
  
          _startPinchDist = Distance(pos1, pos2) * content.localScale.x;
          _startPinchZoom = _currentZoom;
          _startPinchScreenPosition = (pos1 + pos2) / 2;
          RectTransformUtility.ScreenPointToLocalPointInRectangle(content, _startPinchScreenPosition, null, out _startPinchCenterPosition);
  
          Vector2 pivotPosition = new Vector3(content.pivot.x * content.rect.size.x, content.pivot.y * content.rect.size.y);
          Vector2 posFromBottomLeft = pivotPosition + _startPinchCenterPosition;
  
          SetPivot(content, new Vector2(posFromBottomLeft.x / content.rect.width, posFromBottomLeft.y / content.rect.height));
          blockPan = true;
      }
  
      void OnPinch()
      {
          float currentPinchDist = Distance(Input.touches[0].position, Input.touches[1].position) * content.localScale.x;
          _currentZoom = (currentPinchDist / _startPinchDist) * _startPinchZoom;
          _currentZoom = Mathf.Clamp(_currentZoom, _minZoom, _maxZoom);
      }
  
      float Distance(Vector2 pos1, Vector2 pos2)
      {
          RectTransformUtility.ScreenPointToLocalPointInRectangle(content, pos1, null, out pos1);
          RectTransformUtility.ScreenPointToLocalPointInRectangle(content, pos2, null, out pos2);
          return Vector2.Distance(pos1, pos2);
      }
  
      static void SetPivot(RectTransform rectTransform, Vector2 pivot)
      {
          if (rectTransform == null) return;
  
          Vector2 size = rectTransform.rect.size;
          Vector2 deltaPivot = rectTransform.pivot - pivot;
          Vector3 deltaPosition = new Vector3(deltaPivot.x * size.x, deltaPivot.y * size.y) * rectTransform.localScale.x;
          rectTransform.pivot = pivot;
          rectTransform.localPosition -= deltaPosition;
      }
  }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trackpad won't let me pan or zoom 0 Answers

Unity2D: ScrollRect not working on android 0 Answers

How to add a Minimum and Maximum to pinch zoom touch script 2 Answers

Camera zoom out relative to player's speed 0 Answers

Scroll Rect which changes size of the number of buttons 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges