From 16df45eea30cd597de8fccbbb2092c74aa2423bd Mon Sep 17 00:00:00 2001 From: i1 Date: Thu, 4 Oct 2018 00:19:56 +0300 Subject: [PATCH 1/2] 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: From 57cb33cca7153294936efddc2bf5cdae7251a778 Mon Sep 17 00:00:00 2001 From: i1 Date: Thu, 4 Oct 2018 00:25:09 +0300 Subject: [PATCH 2/2] namespace --- Scripts/Utilities/ExtentionMethods.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Scripts/Utilities/ExtentionMethods.cs b/Scripts/Utilities/ExtentionMethods.cs index 68220c4..cb39962 100644 --- a/Scripts/Utilities/ExtentionMethods.cs +++ b/Scripts/Utilities/ExtentionMethods.cs @@ -1,14 +1,15 @@ -using UnityEngine; - -public static class ExtentionMethods +namespace UnityEngine.UI.Extensions { - public static T GetOrAddComponent(this GameObject child) where T : Component + public static class ExtentionMethods { - T result = child.GetComponent(); - if (result == null) + public static T GetOrAddComponent(this GameObject child) where T : Component { - result = child.AddComponent(); + T result = child.GetComponent(); + if (result == null) + { + result = child.AddComponent(); + } + return result; } - return result; } }