fixed adding component ScrollSnapScrollbarHelper multiple times

pull/413/head
i1 2018-10-04 00:19:56 +03:00
parent ee391cab7f
commit 16df45eea3
3 changed files with 28 additions and 2 deletions

View File

@ -136,12 +136,13 @@ namespace UnityEngine.UI.Extensions
} }
if (_scroll_rect.horizontalScrollbar != null && _scroll_rect.horizontal) if (_scroll_rect.horizontalScrollbar != null && _scroll_rect.horizontal)
{ {
var hscroll = _scroll_rect.horizontalScrollbar.gameObject.AddComponent<ScrollSnapScrollbarHelper>();
var hscroll = _scroll_rect.horizontalScrollbar.gameObject.GetOrAddComponent<ScrollSnapScrollbarHelper>();
hscroll.ss = this; hscroll.ss = this;
} }
if (_scroll_rect.verticalScrollbar != null && _scroll_rect.vertical) if (_scroll_rect.verticalScrollbar != null && _scroll_rect.vertical)
{ {
var vscroll = _scroll_rect.verticalScrollbar.gameObject.AddComponent<ScrollSnapScrollbarHelper>(); var vscroll = _scroll_rect.verticalScrollbar.gameObject.GetOrAddComponent<ScrollSnapScrollbarHelper>();
vscroll.ss = this; vscroll.ss = this;
} }
} }

View File

@ -0,0 +1,14 @@
using UnityEngine;
public static class ExtentionMethods
{
public static T GetOrAddComponent<T>(this GameObject child) where T : Component
{
T result = child.GetComponent<T>();
if (result == null)
{
result = child.AddComponent<T>();
}
return result;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ed85e09a894358f4782be8df50a1221a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: