Added Scrollbar helper to ALL current scroll snaps

Resolves #120
release
Simon Jackson 2017-07-02 10:09:52 +01:00
parent 23deee414a
commit 49ecdc1b0e
8 changed files with 960 additions and 573 deletions

View File

@ -379,7 +379,7 @@ namespace UnityEditor.UI
ScrollSnap scrollSnap = scrollSnapRoot.AddComponent<ScrollSnap>();
scrollSnap.direction = direction;
scrollSnap.itemsVisibleAtOnce = itemVisible;
scrollSnap.ItemsVisibleAtOnce = itemVisible;
//Setup Content container
RectTransform rectTransformContent = itemList.GetComponent<RectTransform>();

View File

@ -290,6 +290,7 @@ RectTransform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 1826600021}
- {fileID: 472879517}
m_Father: {fileID: 448991457}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -392,7 +393,7 @@ MonoBehaviour:
m_DecelerationRate: 0.135
m_ScrollSensitivity: 1
m_Viewport: {fileID: 0}
m_HorizontalScrollbar: {fileID: 0}
m_HorizontalScrollbar: {fileID: 472879516}
m_VerticalScrollbar: {fileID: 0}
m_HorizontalScrollbarVisibility: 0
m_VerticalScrollbarVisibility: 0
@ -899,6 +900,122 @@ Canvas:
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
--- !u!1 &472879515
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 472879517}
- component: {fileID: 472879519}
- component: {fileID: 472879518}
- component: {fileID: 472879516}
m_Layer: 5
m_Name: Scrollbar
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &472879516
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 472879515}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: -2061169968, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 557949556}
m_HandleRect: {fileID: 557949554}
m_Direction: 0
m_Value: 0
m_Size: 1
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.Scrollbar+ScrollEvent, UnityEngine.UI, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
--- !u!224 &472879517
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 472879515}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 1513549040}
m_Father: {fileID: 67156822}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -2.6999998, y: -73.5}
m_SizeDelta: {x: 384.7, y: 20}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &472879518
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 472879515}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: -765806418, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
--- !u!222 &472879519
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 472879515}
--- !u!1 &475765631
GameObject:
m_ObjectHideFlags: 0
@ -1116,6 +1233,74 @@ CanvasRenderer:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 546976065}
--- !u!1 &557949553
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 557949554}
- component: {fileID: 557949555}
- component: {fileID: 557949556}
m_Layer: 5
m_Name: Handle
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &557949554
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 557949553}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1513549040}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 20, y: 20}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &557949555
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 557949553}
--- !u!114 &557949556
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 557949553}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: -765806418, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
--- !u!1 &571794653
GameObject:
m_ObjectHideFlags: 0
@ -1707,7 +1892,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: -122.5, y: -176.5}
m_AnchoredPosition: {x: -113.9, y: -183.2}
m_SizeDelta: {x: 150, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &803284191
@ -2525,7 +2710,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: -335.5, y: -176.5}
m_AnchoredPosition: {x: -326.9, y: -183.2}
m_SizeDelta: {x: 150, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1112321046
@ -3612,6 +3797,40 @@ CanvasRenderer:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1496412125}
--- !u!1 &1513549039
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1513549040}
m_Layer: 5
m_Name: Sliding Area
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1513549040
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1513549039}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 557949554}
m_Father: {fileID: 472879517}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: -20, y: -20}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!1 &1520368422
GameObject:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,14 @@
/// Credit SimonDarksideJ
/// Required for scrollbar support to work across ALL scroll snaps
namespace UnityEngine.UI.Extensions
{
internal interface IScrollSnap
{
void ChangePage(int page);
void SetLerp(bool value);
int CurrentPage();
void StartScreenChange();
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 08ba470d8c8dc384e9d5828136742452
timeCreated: 1498921617
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -17,7 +17,7 @@ namespace UnityEngine.UI.Extensions
[ExecuteInEditMode]
[RequireComponent(typeof(ScrollRect))]
[AddComponentMenu("UI/Extensions/Scroll Snap")]
public class ScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
public class ScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, IScrollSnap
{
// needed because of reversed behaviour of axis Y compared to X
// (positions of children lower in children list in horizontal directions grows when in vertical it gets smaller)
@ -27,118 +27,123 @@ namespace UnityEngine.UI.Extensions
Vertical
}
private ScrollRect _scroll_rect;
private RectTransform _scrollRectTransform;
private Transform _listContainerTransform;
private RectTransform _rectTransform;
private int _pages;
private int _startingPage = 0;
// anchor points to lerp to to see child on certain indexes
private Vector3[] _pageAnchorPositions;
private Vector3 _lerpTarget;
private bool _lerp;
// item list related
private float _listContainerMinPosition;
private float _listContainerMaxPosition;
private float _listContainerSize;
private RectTransform _listContainerRectTransform;
private Vector2 _listContainerCachedSize;
private float _itemSize;
private int _itemsCount = 0;
// drag related
private bool _startDrag = true;
private Vector3 _positionOnDragStart = new Vector3();
private int _pageOnDragStart;
private bool _fastSwipeTimer = false;
private int _fastSwipeCounter = 0;
private int _fastSwipeTarget = 10;
[Tooltip("Button to go to the next page. (optional)")]
public Button NextButton;
[Tooltip("Button to go to the previous page. (optional)")]
public Button PrevButton;
[Tooltip("Number of items visible in one page of scroll frame.")]
[RangeAttribute(1, 100)]
public int ItemsVisibleAtOnce = 1;
[Tooltip("Sets minimum width of list items to 1/itemsVisibleAtOnce.")]
public bool AutoLayoutItems = true;
[Tooltip("If you wish to update scrollbar numberOfSteps to number of active children on list.")]
public bool LinkScrolbarSteps = false;
[Tooltip("If you wish to update scrollrect sensitivity to size of list element.")]
public bool LinkScrolrectScrollSensitivity = false;
public Boolean UseFastSwipe = true;
public int FastSwipeThreshold = 100;
public delegate void PageSnapChange(int page);
public event PageSnapChange onPageChange;
public ScrollDirection direction = ScrollDirection.Horizontal;
protected ScrollRect scrollRect;
protected RectTransform scrollRectTransform;
protected Transform listContainerTransform;
protected RectTransform rectTransform;
int pages;
protected int startingPage = 0;
// anchor points to lerp to to see child on certain indexes
protected Vector3[] pageAnchorPositions;
protected Vector3 lerpTarget;
protected bool lerp;
// item list related
protected float listContainerMinPosition;
protected float listContainerMaxPosition;
protected float listContainerSize;
protected RectTransform listContainerRectTransform;
protected Vector2 listContainerCachedSize;
protected float itemSize;
protected int itemsCount = 0;
[Tooltip("Button to go to the next page. (optional)")]
public Button nextButton;
[Tooltip("Button to go to the previous page. (optional)")]
public Button prevButton;
[Tooltip("Number of items visible in one page of scroll frame.")]
[RangeAttribute(1, 100)]
public int itemsVisibleAtOnce = 1;
[Tooltip("Sets minimum width of list items to 1/itemsVisibleAtOnce.")]
public bool autoLayoutItems = true;
[Tooltip("If you wish to update scrollbar numberOfSteps to number of active children on list.")]
public bool linkScrolbarSteps = false;
[Tooltip("If you wish to update scrollrect sensitivity to size of list element.")]
public bool linkScrolrectScrollSensitivity = false;
public Boolean useFastSwipe = true;
public int fastSwipeThreshold = 100;
// drag related
protected bool startDrag = true;
protected Vector3 positionOnDragStart = new Vector3();
protected int pageOnDragStart;
protected bool fastSwipeTimer = false;
protected int fastSwipeCounter = 0;
protected int fastSwipeTarget = 10;
// Use this for initialization
void Awake()
void Start()
{
lerp = false;
_lerp = false;
scrollRect = gameObject.GetComponent<ScrollRect>();
scrollRectTransform = gameObject.GetComponent<RectTransform>();
listContainerTransform = scrollRect.content;
listContainerRectTransform = listContainerTransform.GetComponent<RectTransform>();
_scroll_rect = gameObject.GetComponent<ScrollRect>();
_scrollRectTransform = gameObject.GetComponent<RectTransform>();
_listContainerTransform = _scroll_rect.content;
_listContainerRectTransform = _listContainerTransform.GetComponent<RectTransform>();
rectTransform = listContainerTransform.gameObject.GetComponent<RectTransform>();
_rectTransform = _listContainerTransform.gameObject.GetComponent<RectTransform>();
UpdateListItemsSize();
UpdateListItemPositions();
PageChanged(CurrentPage());
if (nextButton)
if (NextButton)
{
nextButton.GetComponent<Button>().onClick.AddListener(() =>
NextButton.GetComponent<Button>().onClick.AddListener(() =>
{
NextScreen();
});
}
if (prevButton)
if (PrevButton)
{
prevButton.GetComponent<Button>().onClick.AddListener(() =>
PrevButton.GetComponent<Button>().onClick.AddListener(() =>
{
PreviousScreen();
});
}
}
void Start()
if (_scroll_rect.horizontalScrollbar != null && _scroll_rect.horizontal)
{
Awake();
var hscroll = _scroll_rect.horizontalScrollbar.gameObject.AddComponent<ScrollSnapScrollbarHelper>();
hscroll.ss = this;
}
if (_scroll_rect.verticalScrollbar != null && _scroll_rect.vertical)
{
var vscroll = _scroll_rect.verticalScrollbar.gameObject.AddComponent<ScrollSnapScrollbarHelper>();
vscroll.ss = this;
}
}
public void UpdateListItemsSize()
@ -147,27 +152,27 @@ namespace UnityEngine.UI.Extensions
float currentSize = 0;
if (direction == ScrollSnap.ScrollDirection.Horizontal)
{
size = scrollRectTransform.rect.width / itemsVisibleAtOnce;
currentSize = listContainerRectTransform.rect.width / itemsCount;
size = _scrollRectTransform.rect.width / ItemsVisibleAtOnce;
currentSize = _listContainerRectTransform.rect.width / _itemsCount;
}
else
{
size = scrollRectTransform.rect.height / itemsVisibleAtOnce;
currentSize = listContainerRectTransform.rect.height / itemsCount;
size = _scrollRectTransform.rect.height / ItemsVisibleAtOnce;
currentSize = _listContainerRectTransform.rect.height / _itemsCount;
}
itemSize = size;
_itemSize = size;
if (linkScrolrectScrollSensitivity)
if (LinkScrolrectScrollSensitivity)
{
scrollRect.scrollSensitivity = itemSize;
_scroll_rect.scrollSensitivity = _itemSize;
}
if (autoLayoutItems && currentSize != size && itemsCount > 0)
if (AutoLayoutItems && currentSize != size && _itemsCount > 0)
{
if (direction == ScrollSnap.ScrollDirection.Horizontal)
{
foreach (var tr in listContainerTransform)
foreach (var tr in _listContainerTransform)
{
GameObject child = ((Transform)tr).gameObject;
if (child.activeInHierarchy)
@ -179,13 +184,13 @@ namespace UnityEngine.UI.Extensions
childLayout = child.AddComponent<LayoutElement>();
}
childLayout.minWidth = itemSize;
childLayout.minWidth = _itemSize;
}
}
}
else
{
foreach (var tr in listContainerTransform)
foreach (var tr in _listContainerTransform)
{
GameObject child = ((Transform)tr).gameObject;
if (child.activeInHierarchy)
@ -197,7 +202,7 @@ namespace UnityEngine.UI.Extensions
childLayout = child.AddComponent<LayoutElement>();
}
childLayout.minHeight = itemSize;
childLayout.minHeight = _itemSize;
}
}
}
@ -206,12 +211,12 @@ namespace UnityEngine.UI.Extensions
public void UpdateListItemPositions()
{
if (!listContainerRectTransform.rect.size.Equals(listContainerCachedSize))
if (!_listContainerRectTransform.rect.size.Equals(_listContainerCachedSize))
{
// checking how many children of list are active
int activeCount = 0;
foreach (var tr in listContainerTransform)
foreach (var tr in _listContainerTransform)
{
if (((Transform)tr).gameObject.activeInHierarchy)
{
@ -220,29 +225,29 @@ namespace UnityEngine.UI.Extensions
}
// if anything changed since last check reinitialize anchors list
itemsCount = 0;
Array.Resize(ref pageAnchorPositions, activeCount);
_itemsCount = 0;
Array.Resize(ref _pageAnchorPositions, activeCount);
if (activeCount > 0)
{
pages = Mathf.Max(activeCount - itemsVisibleAtOnce + 1, 1);
_pages = Mathf.Max(activeCount - ItemsVisibleAtOnce + 1, 1);
if (direction == ScrollDirection.Horizontal)
{
// looking for list spanning range min/max
scrollRect.horizontalNormalizedPosition = 0;
listContainerMaxPosition = listContainerTransform.localPosition.x;
scrollRect.horizontalNormalizedPosition = 1;
listContainerMinPosition = listContainerTransform.localPosition.x;
_scroll_rect.horizontalNormalizedPosition = 0;
_listContainerMaxPosition = _listContainerTransform.localPosition.x;
_scroll_rect.horizontalNormalizedPosition = 1;
_listContainerMinPosition = _listContainerTransform.localPosition.x;
listContainerSize = listContainerMaxPosition - listContainerMinPosition;
_listContainerSize = _listContainerMaxPosition - _listContainerMinPosition;
for (var i = 0; i < pages; i++)
for (var i = 0; i < _pages; i++)
{
pageAnchorPositions[i] = new Vector3(
listContainerMaxPosition - itemSize * i,
listContainerTransform.localPosition.y,
listContainerTransform.localPosition.z
_pageAnchorPositions[i] = new Vector3(
_listContainerMaxPosition - _itemSize * i,
_listContainerTransform.localPosition.y,
_listContainerTransform.localPosition.z
);
}
}
@ -250,35 +255,35 @@ namespace UnityEngine.UI.Extensions
{
//Debug.Log ("-------------looking for list spanning range----------------");
// looking for list spanning range
scrollRect.verticalNormalizedPosition = 1;
listContainerMinPosition = listContainerTransform.localPosition.y;
scrollRect.verticalNormalizedPosition = 0;
listContainerMaxPosition = listContainerTransform.localPosition.y;
_scroll_rect.verticalNormalizedPosition = 1;
_listContainerMinPosition = _listContainerTransform.localPosition.y;
_scroll_rect.verticalNormalizedPosition = 0;
_listContainerMaxPosition = _listContainerTransform.localPosition.y;
listContainerSize = listContainerMaxPosition - listContainerMinPosition;
_listContainerSize = _listContainerMaxPosition - _listContainerMinPosition;
for (var i = 0; i < pages; i++)
for (var i = 0; i < _pages; i++)
{
pageAnchorPositions[i] = new Vector3(
listContainerTransform.localPosition.x,
listContainerMinPosition + itemSize * i,
listContainerTransform.localPosition.z
_pageAnchorPositions[i] = new Vector3(
_listContainerTransform.localPosition.x,
_listContainerMinPosition + _itemSize * i,
_listContainerTransform.localPosition.z
);
}
}
UpdateScrollbar(linkScrolbarSteps);
startingPage = Mathf.Min(startingPage, pages);
UpdateScrollbar(LinkScrolbarSteps);
_startingPage = Mathf.Min(_startingPage, _pages);
ResetPage();
}
if (itemsCount != activeCount)
if (_itemsCount != activeCount)
{
PageChanged(CurrentPage());
}
itemsCount = activeCount;
listContainerCachedSize.Set(listContainerRectTransform.rect.size.x, listContainerRectTransform.rect.size.y);
_itemsCount = activeCount;
_listContainerCachedSize.Set(_listContainerRectTransform.rect.size.x, _listContainerRectTransform.rect.size.y);
}
}
@ -287,30 +292,30 @@ namespace UnityEngine.UI.Extensions
{
if (direction == ScrollDirection.Horizontal)
{
scrollRect.horizontalNormalizedPosition = pages > 1 ? (float)startingPage / (float)(pages - 1) : 0;
_scroll_rect.horizontalNormalizedPosition = _pages > 1 ? (float)_startingPage / (float)(_pages - 1) : 0;
}
else
{
scrollRect.verticalNormalizedPosition = pages > 1 ? (float)(pages - startingPage - 1) / (float)(pages - 1) : 0;
_scroll_rect.verticalNormalizedPosition = _pages > 1 ? (float)(_pages - _startingPage - 1) / (float)(_pages - 1) : 0;
}
}
protected void UpdateScrollbar(bool linkSteps)
private void UpdateScrollbar(bool linkSteps)
{
if (linkSteps)
{
if (direction == ScrollDirection.Horizontal)
{
if (scrollRect.horizontalScrollbar != null)
if (_scroll_rect.horizontalScrollbar != null)
{
scrollRect.horizontalScrollbar.numberOfSteps = pages;
_scroll_rect.horizontalScrollbar.numberOfSteps = _pages;
}
}
else
{
if (scrollRect.verticalScrollbar != null)
if (_scroll_rect.verticalScrollbar != null)
{
scrollRect.verticalScrollbar.numberOfSteps = pages;
_scroll_rect.verticalScrollbar.numberOfSteps = _pages;
}
}
}
@ -318,16 +323,16 @@ namespace UnityEngine.UI.Extensions
{
if (direction == ScrollDirection.Horizontal)
{
if (scrollRect.horizontalScrollbar != null)
if (_scroll_rect.horizontalScrollbar != null)
{
scrollRect.horizontalScrollbar.numberOfSteps = 0;
_scroll_rect.horizontalScrollbar.numberOfSteps = 0;
}
}
else
{
if (scrollRect.verticalScrollbar != null)
if (_scroll_rect.verticalScrollbar != null)
{
scrollRect.verticalScrollbar.numberOfSteps = 0;
_scroll_rect.verticalScrollbar.numberOfSteps = 0;
}
}
}
@ -338,30 +343,30 @@ namespace UnityEngine.UI.Extensions
UpdateListItemsSize();
UpdateListItemPositions();
if (lerp)
if (_lerp)
{
UpdateScrollbar(false);
listContainerTransform.localPosition = Vector3.Lerp(listContainerTransform.localPosition, lerpTarget, 7.5f * Time.deltaTime);
_listContainerTransform.localPosition = Vector3.Lerp(_listContainerTransform.localPosition, _lerpTarget, 7.5f * Time.deltaTime);
if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 0.001f)
if (Vector3.Distance(_listContainerTransform.localPosition, _lerpTarget) < 0.001f)
{
listContainerTransform.localPosition = lerpTarget;
lerp = false;
_listContainerTransform.localPosition = _lerpTarget;
_lerp = false;
UpdateScrollbar(linkScrolbarSteps);
UpdateScrollbar(LinkScrolbarSteps);
}
//change the info bullets at the bottom of the screen. Just for visual effect
if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 10f)
if (Vector3.Distance(_listContainerTransform.localPosition, _lerpTarget) < 10f)
{
PageChanged(CurrentPage());
}
}
if (fastSwipeTimer)
if (_fastSwipeTimer)
{
fastSwipeCounter++;
_fastSwipeCounter++;
}
}
@ -373,10 +378,10 @@ namespace UnityEngine.UI.Extensions
{
UpdateListItemPositions();
if (CurrentPage() < pages - 1)
if (CurrentPage() < _pages - 1)
{
lerp = true;
lerpTarget = pageAnchorPositions[CurrentPage() + 1];
_lerp = true;
_lerpTarget = _pageAnchorPositions[CurrentPage() + 1];
PageChanged(CurrentPage() + 1);
}
@ -389,8 +394,8 @@ namespace UnityEngine.UI.Extensions
if (CurrentPage() > 0)
{
lerp = true;
lerpTarget = pageAnchorPositions[CurrentPage() - 1];
_lerp = true;
_lerpTarget = _pageAnchorPositions[CurrentPage() - 1];
PageChanged(CurrentPage() - 1);
}
@ -399,12 +404,12 @@ namespace UnityEngine.UI.Extensions
//Because the CurrentScreen function is not so reliable, these are the functions used for swipes
private void NextScreenCommand()
{
if (pageOnDragStart < pages - 1)
if (_pageOnDragStart < _pages - 1)
{
int targetPage = Mathf.Min(pages - 1, pageOnDragStart + itemsVisibleAtOnce);
lerp = true;
int targetPage = Mathf.Min(_pages - 1, _pageOnDragStart + ItemsVisibleAtOnce);
_lerp = true;
lerpTarget = pageAnchorPositions[targetPage];
_lerpTarget = _pageAnchorPositions[targetPage];
PageChanged(targetPage);
}
@ -413,12 +418,12 @@ namespace UnityEngine.UI.Extensions
//Because the CurrentScreen function is not so reliable, these are the functions used for swipes
private void PrevScreenCommand()
{
if (pageOnDragStart > 0)
if (_pageOnDragStart > 0)
{
int targetPage = Mathf.Max(0, pageOnDragStart - itemsVisibleAtOnce);
lerp = true;
int targetPage = Mathf.Max(0, _pageOnDragStart - ItemsVisibleAtOnce);
_lerp = true;
lerpTarget = pageAnchorPositions[targetPage];
_lerpTarget = _pageAnchorPositions[targetPage];
PageChanged(targetPage);
}
@ -432,27 +437,35 @@ namespace UnityEngine.UI.Extensions
if (direction == ScrollDirection.Horizontal)
{
pos = listContainerMaxPosition - listContainerTransform.localPosition.x;
pos = Mathf.Clamp(pos, 0, listContainerSize);
pos = _listContainerMaxPosition - _listContainerTransform.localPosition.x;
pos = Mathf.Clamp(pos, 0, _listContainerSize);
}
else
{
pos = listContainerTransform.localPosition.y - listContainerMinPosition;
pos = Mathf.Clamp(pos, 0, listContainerSize);
pos = _listContainerTransform.localPosition.y - _listContainerMinPosition;
pos = Mathf.Clamp(pos, 0, _listContainerSize);
}
float page = pos / itemSize;
float page = pos / _itemSize;
return Mathf.Clamp(Mathf.RoundToInt(page), 0, pages);
return Mathf.Clamp(Mathf.RoundToInt(page), 0, _pages);
}
/// <summary>
/// Added to provide a uniform interface for the ScrollBarHelper
/// </summary>
public void SetLerp(bool value)
{
_lerp = value;
}
public void ChangePage(int page)
{
if (0 <= page && page < pages)
if (0 <= page && page < _pages)
{
lerp = true;
_lerp = true;
lerpTarget = pageAnchorPositions[page];
_lerpTarget = _pageAnchorPositions[page];
PageChanged(page);
}
@ -461,16 +474,16 @@ namespace UnityEngine.UI.Extensions
//changes the bullets on the bottom of the page - pagination
private void PageChanged(int currentPage)
{
startingPage = currentPage;
_startingPage = currentPage;
if (nextButton)
if (NextButton)
{
nextButton.interactable = currentPage < pages - 1;
NextButton.interactable = currentPage < _pages - 1;
}
if (prevButton)
if (PrevButton)
{
prevButton.interactable = currentPage > 0;
PrevButton.interactable = currentPage > 0;
}
if (onPageChange != null)
@ -484,35 +497,35 @@ namespace UnityEngine.UI.Extensions
{
UpdateScrollbar(false);
fastSwipeCounter = 0;
fastSwipeTimer = true;
_fastSwipeCounter = 0;
_fastSwipeTimer = true;
positionOnDragStart = eventData.position;
pageOnDragStart = CurrentPage();
_positionOnDragStart = eventData.position;
_pageOnDragStart = CurrentPage();
}
public void OnEndDrag(PointerEventData eventData)
{
startDrag = true;
_startDrag = true;
float change = 0;
if (direction == ScrollDirection.Horizontal)
{
change = positionOnDragStart.x - eventData.position.x;
change = _positionOnDragStart.x - eventData.position.x;
}
else
{
change = -positionOnDragStart.y + eventData.position.y;
change = -_positionOnDragStart.y + eventData.position.y;
}
if (useFastSwipe)
if (UseFastSwipe)
{
fastSwipe = false;
fastSwipeTimer = false;
_fastSwipeTimer = false;
if (fastSwipeCounter <= fastSwipeTarget)
if (_fastSwipeCounter <= _fastSwipeTarget)
{
if (Math.Abs(change) > fastSwipeThreshold)
if (Math.Abs(change) > FastSwipeThreshold)
{
fastSwipe = true;
}
@ -530,27 +543,29 @@ namespace UnityEngine.UI.Extensions
}
else
{
lerp = true;
lerpTarget = pageAnchorPositions[CurrentPage()];
_lerp = true;
_lerpTarget = _pageAnchorPositions[CurrentPage()];
}
}
else
{
lerp = true;
lerpTarget = pageAnchorPositions[CurrentPage()];
_lerp = true;
_lerpTarget = _pageAnchorPositions[CurrentPage()];
}
}
public void OnDrag(PointerEventData eventData)
{
lerp = false;
_lerp = false;
if (startDrag)
if (_startDrag)
{
OnBeginDrag(eventData);
startDrag = false;
_startDrag = false;
}
}
public void StartScreenChange() { }
#endregion
}
}

View File

@ -1,10 +1,14 @@
using System;
/// Credit BinaryX
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1945602
/// Updated by ddreaper - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect.
using System;
using UnityEngine.Events;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler
public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IScrollSnap
{
internal RectTransform _screensContainer;
internal bool _isVertical;
@ -26,6 +30,7 @@ namespace UnityEngine.UI.Extensions
internal int _previousPage;
internal int _halfNoVisibleItems;
private int _bottomItem, _topItem;
private bool _moveStarted;
[Serializable]
public class SelectionChangeStartEvent : UnityEvent { }
@ -62,7 +67,7 @@ namespace UnityEngine.UI.Extensions
public int FastSwipeThreshold = 100;
[Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")]
public int SwipeVelocityThreshold = 200;
public int SwipeVelocityThreshold = 100;
[Tooltip("The visible bounds area, controls which items are visible/enabled. *Note Should use a RectMask. (optional)")]
public RectTransform MaskArea;
@ -83,8 +88,8 @@ namespace UnityEngine.UI.Extensions
{
_previousPage = _currentPage;
_currentPage = value;
if(MaskArea) UpdateVisible();
if(!_lerp) ScreenChange();
if (MaskArea) UpdateVisible();
if (!_lerp) ScreenChange();
OnCurrentScreenChange(_currentPage);
}
}
@ -120,11 +125,19 @@ namespace UnityEngine.UI.Extensions
// Use this for initialization
void Awake()
{
_scroll_rect = gameObject.GetComponent<ScrollRect>();
if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar)
if (_scroll_rect == null)
{
Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results");
_scroll_rect = gameObject.GetComponent<ScrollRect>();
}
if (_scroll_rect.horizontalScrollbar && _scroll_rect.horizontal)
{
var hscroll = _scroll_rect.horizontalScrollbar.gameObject.AddComponent<ScrollSnapScrollbarHelper>();
hscroll.ss = this;
}
if (_scroll_rect.verticalScrollbar && _scroll_rect.vertical)
{
var vscroll = _scroll_rect.verticalScrollbar.gameObject.AddComponent<ScrollSnapScrollbarHelper>();
vscroll.ss = this;
}
if (StartingScreen < 0)
@ -372,18 +385,34 @@ namespace UnityEngine.UI.Extensions
/// disables the page navigation buttons when at the first or last screen
/// </summary>
/// <param name="targetScreen"></param>
private void ToggleNavigationButtons(int targetScreen) {
if (PrevButton) {
private void ToggleNavigationButtons(int targetScreen)
{
if (PrevButton)
{
PrevButton.GetComponent<Button>().interactable = targetScreen > 0;
}
if (NextButton) {
if (NextButton)
{
NextButton.GetComponent<Button>().interactable = targetScreen < _screensContainer.transform.childCount - 1;
}
}
private void OnValidate()
{
if (_scroll_rect == null)
{
_scroll_rect = GetComponent<ScrollRect>();
}
if (!_scroll_rect.horizontal && !_scroll_rect.vertical)
{
Debug.LogError("ScrollRect has to have a direction, please select either Horizontal OR Vertical with the appropriate control.");
}
if (_scroll_rect.horizontal && _scroll_rect.vertical)
{
Debug.LogError("ScrollRect has to be unidirectional, only use either Horizontal or Vertical on the ScrollRect, NOT both.");
}
var children = gameObject.GetComponent<ScrollRect>().content.childCount;
if (children != 0 || ChildObjects != null)
{
@ -418,10 +447,14 @@ namespace UnityEngine.UI.Extensions
/// <summary>
/// Event fires when the user starts to change the page, either via swipe or button.
/// </summary>
internal void StartScreenChange()
public void StartScreenChange()
{
if (!_moveStarted)
{
_moveStarted = true;
OnSelectionChangeStartEvent.Invoke();
}
}
/// <summary>
/// Event fires when the currently viewed page changes, also updates while the scroll is moving
@ -438,6 +471,7 @@ namespace UnityEngine.UI.Extensions
{
OnSelectionChangeEndEvent.Invoke(_currentPage);
_settled = true;
_moveStarted = false;
}
#region Interfaces
@ -463,5 +497,32 @@ namespace UnityEngine.UI.Extensions
}
#endregion
}
#region IScrollSnap Interface
/// <summary>
/// Added to provide a uniform interface for the ScrollBarHelper
/// </summary>
int IScrollSnap.CurrentPage()
{
return CurrentPage = GetPageforPosition(_screensContainer.localPosition);
}
/// <summary>
/// Added to provide a uniform interface for the ScrollBarHelper
/// </summary>
public void SetLerp(bool value)
{
_lerp = value;
}
/// <summary>
/// Added to provide a uniform interface for the ScrollBarHelper
/// </summary>
public void ChangePage(int page)
{
GoToScreen(page);
}
#endregion
}
}

View File

@ -0,0 +1,54 @@
/// Credit Anonymous donation
/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/120/horizontal-scroll-snap-scroll-bar-fix
/// Updated by ddreaper - Made extension support all types of scroll snap
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
public class ScrollSnapScrollbarHelper : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
internal IScrollSnap ss;
public void OnBeginDrag(PointerEventData eventData)
{
OnScrollBarDown();
}
public void OnDrag(PointerEventData eventData)
{
ss.CurrentPage();
}
public void OnEndDrag(PointerEventData eventData)
{
OnScrollBarUp();
}
public void OnPointerDown(PointerEventData eventData)
{
OnScrollBarDown();
}
public void OnPointerUp(PointerEventData eventData)
{
OnScrollBarUp();
}
void OnScrollBarDown()
{
if (ss != null)
{
ss.SetLerp(false);
ss.StartScreenChange();
}
}
void OnScrollBarUp()
{
ss.SetLerp(true);
ss.ChangePage(ss.CurrentPage());
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2ec759101f5786b4b89aae8a1b47ca4b
timeCreated: 1498919960
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: