From 75d60d03aa8bb55e09b6e2d2e571e709d3f34f70 Mon Sep 17 00:00:00 2001 From: LokoSolo Games Date: Thu, 15 Sep 2022 16:45:25 +0800 Subject: [PATCH] Fix an incorrect zooming behaviour on ScreenSpace-Camera & WorldSpace render mode --- CHANGELOG.md | 4 ++++ Runtime/PinchableScrollRect.cs | 15 ++++++++++++++- package.json | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d922ef..52e7028 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.0.4] - 2022-09-15 +### Fixed +- Fix an incorrect zooming behaviour on ScreenSpace-Camera & WorldSpace render mode. + ## [1.0.3] - 2022-08-20 ### Fixed - Fix an incorrect zooming behaviour with Canvas Scalar. diff --git a/Runtime/PinchableScrollRect.cs b/Runtime/PinchableScrollRect.cs index 93a855b..bde5b41 100644 --- a/Runtime/PinchableScrollRect.cs +++ b/Runtime/PinchableScrollRect.cs @@ -146,11 +146,24 @@ protected virtual void HandleZoom(float zoomValue) { Mathf.Clamp(newScale.z, lowerScale.z, upperScale.z)); this.SetContentLocalScale(newScale); // The world position should remain the same - content.position = _worldPos + (Vector3)pixelDelta * _canvas.scaleFactor; // compensate for value due to pivot change + content.position = _worldPos + (Vector3)pixelDelta * getScaleFactor(); // compensate for value due to pivot change // Reset delta since zooming deceleration take place at the same pivot zoomPosDelta = Vector2.zero; } + float getScaleFactor() { + switch (_canvas.renderMode) { + case RenderMode.ScreenSpaceOverlay: + return _canvas.scaleFactor; + case RenderMode.ScreenSpaceCamera: + return _canvas.scaleFactor / _canvas.referencePixelsPerUnit; + case RenderMode.WorldSpace: + return _canvas.transform.localScale.x; + default: + throw new ArgumentOutOfRangeException(); + } + } + protected virtual void SetContentPivotPosition(Vector2 pivot) { Vector2 _pivot = content.pivot; if (!this.horizontal) pivot.x = _pivot.x; diff --git a/package.json b/package.json index 14385af..f18b75f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.lokosolo.pinchable-scrollrect", - "version": "1.0.3", + "version": "1.0.4", "displayName": "Pinchable ScrollRect", "description": "Pinchable ScrollRect allows users to zoom in and out on the ScrollRect with both touches pinching input or mouse scroll input.", "unity": "2018.4",