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: