From a31766326821a8b0d78f7b7f49d1a7288003f49f Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Sat, 24 Dec 2022 12:44:23 +0000 Subject: [PATCH] Added error handling around setting Unity UI Components for Vertical/Horizontal scrolling Resolves: https://github.com/Unity-UI-Extensions/com.unity.uiextensions/issues/296 --- Runtime/Scripts/Layout/HorizontalScrollSnap.cs | 10 +++++++++- Runtime/Scripts/Layout/VerticalScrollSnap.cs | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Runtime/Scripts/Layout/HorizontalScrollSnap.cs b/Runtime/Scripts/Layout/HorizontalScrollSnap.cs index f6de775..365c1a9 100644 --- a/Runtime/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Runtime/Scripts/Layout/HorizontalScrollSnap.cs @@ -114,12 +114,20 @@ namespace UnityEngine.UI.Extensions /// Should the world position be updated to it's parent transform? public void AddChild(GameObject GO, bool WorldPositionStays) { - _scroll_rect.horizontalNormalizedPosition = 0; + try + { + // Rare instances of Unity bug cause error, adding try to manage it. + _scroll_rect.horizontalNormalizedPosition = 0; + } + catch { } + GO.transform.SetParent(_screensContainer, WorldPositionStays); InitialiseChildObjectsFromScene(); DistributePages(); if (MaskArea) + { UpdateVisible(); + } SetScrollContainerPosition(); } diff --git a/Runtime/Scripts/Layout/VerticalScrollSnap.cs b/Runtime/Scripts/Layout/VerticalScrollSnap.cs index 58bde93..4493d8b 100644 --- a/Runtime/Scripts/Layout/VerticalScrollSnap.cs +++ b/Runtime/Scripts/Layout/VerticalScrollSnap.cs @@ -114,7 +114,13 @@ namespace UnityEngine.UI.Extensions /// Should the world position be updated to it's parent transform? public void AddChild(GameObject GO, bool WorldPositionStays) { - _scroll_rect.verticalNormalizedPosition = 0; + try + { + // Rare instances of Unity bug cause error, adding try to manage it. + _scroll_rect.verticalNormalizedPosition = 0; + } + catch { } + GO.transform.SetParent(_screensContainer, WorldPositionStays); InitialiseChildObjectsFromScene(); DistributePages();