From 16df45eea30cd597de8fccbbb2092c74aa2423bd Mon Sep 17 00:00:00 2001 From: i1 Date: Thu, 4 Oct 2018 00:19:56 +0300 Subject: [PATCH] fixed adding component ScrollSnapScrollbarHelper multiple times --- Scripts/Layout/ScrollSnap.cs | 5 +++-- Scripts/Utilities/ExtentionMethods.cs | 14 ++++++++++++++ Scripts/Utilities/ExtentionMethods.cs.meta | 11 +++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 Scripts/Utilities/ExtentionMethods.cs create mode 100644 Scripts/Utilities/ExtentionMethods.cs.meta diff --git a/Scripts/Layout/ScrollSnap.cs b/Scripts/Layout/ScrollSnap.cs index 0d93a28..b7f8b68 100644 --- a/Scripts/Layout/ScrollSnap.cs +++ b/Scripts/Layout/ScrollSnap.cs @@ -136,12 +136,13 @@ namespace UnityEngine.UI.Extensions } if (_scroll_rect.horizontalScrollbar != null && _scroll_rect.horizontal) { - var hscroll = _scroll_rect.horizontalScrollbar.gameObject.AddComponent(); + + var hscroll = _scroll_rect.horizontalScrollbar.gameObject.GetOrAddComponent(); hscroll.ss = this; } if (_scroll_rect.verticalScrollbar != null && _scroll_rect.vertical) { - var vscroll = _scroll_rect.verticalScrollbar.gameObject.AddComponent(); + var vscroll = _scroll_rect.verticalScrollbar.gameObject.GetOrAddComponent(); vscroll.ss = this; } } diff --git a/Scripts/Utilities/ExtentionMethods.cs b/Scripts/Utilities/ExtentionMethods.cs new file mode 100644 index 0000000..68220c4 --- /dev/null +++ b/Scripts/Utilities/ExtentionMethods.cs @@ -0,0 +1,14 @@ +using UnityEngine; + +public static class ExtentionMethods +{ + public static T GetOrAddComponent(this GameObject child) where T : Component + { + T result = child.GetComponent(); + if (result == null) + { + result = child.AddComponent(); + } + return result; + } +} diff --git a/Scripts/Utilities/ExtentionMethods.cs.meta b/Scripts/Utilities/ExtentionMethods.cs.meta new file mode 100644 index 0000000..cfa1894 --- /dev/null +++ b/Scripts/Utilities/ExtentionMethods.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ed85e09a894358f4782be8df50a1221a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: