From fc6ea6089d4c494b1c1c62334b51db75f0519410 Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Sat, 23 Apr 2022 21:01:37 +0100 Subject: [PATCH] Fixes issue #398 where the Next / Previous buttons filed to work if the ScrollSnap was previously scrolling. Also renamed the Extension Methods scripts and added a new function. Resolves: #398 --- Runtime/Scripts/Layout/ScrollSnapBase.cs | 14 +++++------- ...xtentionMethods.cs => ExtensionMethods.cs} | 22 +++++++++++++++++++ ...thods.cs.meta => ExtensionMethods.cs.meta} | 0 3 files changed, 28 insertions(+), 8 deletions(-) rename Runtime/Scripts/Utilities/{ExtentionMethods.cs => ExtensionMethods.cs} (57%) rename Runtime/Scripts/Utilities/{ExtentionMethods.cs.meta => ExtensionMethods.cs.meta} (100%) diff --git a/Runtime/Scripts/Layout/ScrollSnapBase.cs b/Runtime/Scripts/Layout/ScrollSnapBase.cs index abee12c..3d570c7 100644 --- a/Runtime/Scripts/Layout/ScrollSnapBase.cs +++ b/Runtime/Scripts/Layout/ScrollSnapBase.cs @@ -5,6 +5,7 @@ using System; using UnityEngine.Events; using UnityEngine.EventSystems; +using UnityEngine.UI.Extensions; namespace UnityEngine.UI.Extensions { @@ -314,6 +315,8 @@ namespace UnityEngine.UI.Extensions //Function for switching screens with buttons public void NextScreen() { + _scroll_rect.velocity = Vector2.zero; + if (_currentPage < _screens - 1 || _isInfinite) { if (!_lerp) StartScreenChange(); @@ -336,6 +339,8 @@ namespace UnityEngine.UI.Extensions //Function for switching screens with buttons public void PreviousScreen() { + _scroll_rect.velocity = Vector2.zero; + if (_currentPage > 0 || _isInfinite) { if (!_lerp) StartScreenChange(); @@ -515,15 +520,8 @@ namespace UnityEngine.UI.Extensions MaskBuffer = 1; } - if (PageStep < 0) - { - PageStep = 0; - } + PageStep.Clamp(0, 9); - if (PageStep > 8) - { - PageStep = 9; - } var infiniteScroll = GetComponent(); if (ChildObjects != null && ChildObjects.Length > 0 && infiniteScroll != null && !infiniteScroll.InitByUser) { diff --git a/Runtime/Scripts/Utilities/ExtentionMethods.cs b/Runtime/Scripts/Utilities/ExtensionMethods.cs similarity index 57% rename from Runtime/Scripts/Utilities/ExtentionMethods.cs rename to Runtime/Scripts/Utilities/ExtensionMethods.cs index 07da566..4193c3c 100644 --- a/Runtime/Scripts/Utilities/ExtentionMethods.cs +++ b/Runtime/Scripts/Utilities/ExtensionMethods.cs @@ -29,5 +29,27 @@ namespace UnityEngine.UI.Extensions !gameObject.hideFlags.HasFlag(HideFlags.HideInHierarchy); // I don't care about GameObjects *inside* prefabs, just the overall prefab. } + + /// + /// Generic clamp method to limt a value between a range of values + /// + /// data type + /// Value to clamp + /// Minimum value + /// Maximum value + /// + public static T Clamp(this T value, T min, T max) where T : IComparable + { + if (value.CompareTo(min) < 0) + { + value = min; + } + if (value.CompareTo(max) > 0) + { + value = max; + } + + return value; + } } } diff --git a/Runtime/Scripts/Utilities/ExtentionMethods.cs.meta b/Runtime/Scripts/Utilities/ExtensionMethods.cs.meta similarity index 100% rename from Runtime/Scripts/Utilities/ExtentionMethods.cs.meta rename to Runtime/Scripts/Utilities/ExtensionMethods.cs.meta