Checkin with FlowLayoutGroup, HorrizontalScrollSnap fixes
Added the WIP for the new ScollSnap with fixes --HG-- branch : develop_4.6release
parent
c20194ad92
commit
e1e48e7514
|
@ -1024,5 +1024,268 @@ namespace UnityEditor.UI
|
|||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region New ScrollSnapCode
|
||||
static public void FixedScrollSnapBase(MenuCommand menuCommand, string name, ScrollSnap.ScrollDirection direction, int itemVisible, int itemCount, Vector2 itemSize)
|
||||
|
||||
{
|
||||
|
||||
GameObject scrollSnapRoot = CreateUIElementRoot(name, menuCommand, s_ThickGUIElementSize);
|
||||
|
||||
|
||||
|
||||
GameObject itemList = CreateUIObject("List", scrollSnapRoot);
|
||||
|
||||
|
||||
|
||||
// Set RectTransform to stretch
|
||||
|
||||
RectTransform rectTransformScrollSnapRoot = scrollSnapRoot.GetComponent<RectTransform>();
|
||||
|
||||
rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f);
|
||||
|
||||
rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f);
|
||||
|
||||
rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero;
|
||||
|
||||
|
||||
|
||||
if (direction == ScrollSnap.ScrollDirection.Horizontal)
|
||||
|
||||
{
|
||||
|
||||
rectTransformScrollSnapRoot.sizeDelta = new Vector2(itemVisible * itemSize.x, itemSize.y);
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
|
||||
{
|
||||
|
||||
rectTransformScrollSnapRoot.sizeDelta = new Vector2(itemSize.x, itemVisible * itemSize.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Image image = scrollSnapRoot.AddComponent<Image>();
|
||||
|
||||
image.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(kBackgroundSpriteResourcePath);
|
||||
|
||||
image.type = Image.Type.Sliced;
|
||||
|
||||
image.color = new Color(1f, 1f, 1f, 1f);
|
||||
|
||||
|
||||
|
||||
Mask listMask = scrollSnapRoot.AddComponent<Mask>();
|
||||
|
||||
listMask.showMaskGraphic = false;
|
||||
|
||||
|
||||
|
||||
ScrollRect scrollRect = scrollSnapRoot.AddComponent<ScrollRect>();
|
||||
|
||||
scrollRect.vertical = direction == ScrollSnap.ScrollDirection.Vertical;
|
||||
|
||||
scrollRect.horizontal = direction == ScrollSnap.ScrollDirection.Horizontal;
|
||||
|
||||
|
||||
|
||||
ScrollSnap scrollSnap = scrollSnapRoot.AddComponent<ScrollSnap>();
|
||||
|
||||
scrollSnap.direction = direction;
|
||||
|
||||
scrollSnap.itemsVisibleAtOnce = itemVisible;
|
||||
|
||||
|
||||
|
||||
//Setup Content container
|
||||
|
||||
RectTransform rectTransformContent = itemList.GetComponent<RectTransform>();
|
||||
|
||||
rectTransformContent.anchorMin = Vector2.zero;
|
||||
|
||||
rectTransformContent.anchorMax = new Vector2(1f, 1f);
|
||||
|
||||
//rectTransformContent.anchoredPosition = Vector2.zero;
|
||||
|
||||
rectTransformContent.sizeDelta = Vector2.zero;
|
||||
|
||||
scrollRect.content = rectTransformContent;
|
||||
|
||||
|
||||
|
||||
//Setup Item list container
|
||||
|
||||
if (direction == ScrollSnap.ScrollDirection.Horizontal)
|
||||
|
||||
{
|
||||
|
||||
itemList.AddComponent<HorizontalLayoutGroup> ();
|
||||
|
||||
ContentSizeFitter sizeFitter = itemList.AddComponent<ContentSizeFitter>();
|
||||
|
||||
sizeFitter.horizontalFit = ContentSizeFitter.FitMode.MinSize;
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
|
||||
{
|
||||
|
||||
itemList.AddComponent<VerticalLayoutGroup> ();
|
||||
|
||||
ContentSizeFitter sizeFitter = itemList.AddComponent<ContentSizeFitter>();
|
||||
|
||||
sizeFitter.verticalFit = ContentSizeFitter.FitMode.MinSize;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Setup children
|
||||
|
||||
|
||||
|
||||
for (var i = 0; i < itemCount; i ++)
|
||||
|
||||
{
|
||||
|
||||
GameObject item = CreateUIObject (string.Format("Item_{0:00}", i), itemList);
|
||||
|
||||
|
||||
|
||||
GameObject childText = CreateUIObject ("Text", item);
|
||||
|
||||
|
||||
|
||||
Image pageImage = item.AddComponent<Image> ();
|
||||
|
||||
pageImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite> (kStandardSpritePath);
|
||||
|
||||
pageImage.type = Image.Type.Sliced;
|
||||
|
||||
pageImage.color = s_DefaultSelectableColor;
|
||||
|
||||
|
||||
|
||||
LayoutElement elementLayout = item.AddComponent<LayoutElement> ();
|
||||
|
||||
if (direction == ScrollSnap.ScrollDirection.Horizontal)
|
||||
|
||||
{
|
||||
|
||||
elementLayout.minWidth = itemSize.x;
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
|
||||
{
|
||||
|
||||
elementLayout.minHeight = itemSize.y;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
RectTransform rectTransformPage01 = item.GetComponent<RectTransform> ();
|
||||
|
||||
rectTransformPage01.anchorMin = new Vector2 (0f, 0.5f);
|
||||
|
||||
rectTransformPage01.anchorMax = new Vector2 (0f, 0.5f);
|
||||
|
||||
//rectTransformPage01.anchoredPosition = Vector2.zero;
|
||||
|
||||
//rectTransformPage01.sizeDelta = Vector2.zero;
|
||||
|
||||
rectTransformPage01.pivot = new Vector2 (0f, 0.5f);
|
||||
|
||||
|
||||
|
||||
//Setup Text on Page01
|
||||
|
||||
Text text = childText.AddComponent<Text> ();
|
||||
|
||||
text.text = item.name;
|
||||
|
||||
text.alignment = TextAnchor.MiddleCenter;
|
||||
|
||||
text.color = new Color (0.196f, 0.196f, 0.196f);
|
||||
|
||||
|
||||
|
||||
//Setup Text 1st Child
|
||||
|
||||
RectTransform rectTransformPage01Text = childText.GetComponent<RectTransform> ();
|
||||
|
||||
rectTransformPage01Text.anchorMin = new Vector2 (0.5f, 0.5f);
|
||||
|
||||
rectTransformPage01Text.anchorMax = new Vector2 (0.5f, 0.5f);
|
||||
|
||||
//rectTransformPage01Text.anchoredPosition = Vector2.zero;
|
||||
|
||||
//rectTransformPage01Text.sizeDelta = Vector2.zero;
|
||||
|
||||
rectTransformPage01Text.pivot = new Vector2 (0.5f, 0.5f);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Selection.activeGameObject = scrollSnapRoot;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
[MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Horizontal Single Item", false)]
|
||||
|
||||
static public void AddFixedItemScrollSnapHorizontalSingle(MenuCommand menuCommand)
|
||||
|
||||
{
|
||||
|
||||
FixedScrollSnapBase (menuCommand, "Scroll Snap Horizontal Single", ScrollSnap.ScrollDirection.Horizontal, 1, 3, new Vector2(100, 100));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
[MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Horizontal Multiple Items", false)]
|
||||
|
||||
static public void AddFixedItemScrollSnapHorizontalMultiple(MenuCommand menuCommand)
|
||||
|
||||
{
|
||||
|
||||
FixedScrollSnapBase (menuCommand, "Scroll Snap Horizontal Multiple", ScrollSnap.ScrollDirection.Horizontal, 3, 15, new Vector2(100, 100));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
[MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Vertical Single Item", false)]
|
||||
|
||||
static public void AddFixedItemScrollSnapVerticalSingle(MenuCommand menuCommand)
|
||||
|
||||
{
|
||||
|
||||
FixedScrollSnapBase (menuCommand, "Scroll Snap Vertical Multiple", ScrollSnap.ScrollDirection.Vertical, 1, 3, new Vector2(100, 100));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
[MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Vertical Multiple Items", false)]
|
||||
|
||||
static public void AddFixedItemScrollSnapVerticalMultiple(MenuCommand menuCommand)
|
||||
|
||||
{
|
||||
|
||||
FixedScrollSnapBase (menuCommand, "Scroll Snap Vertical Multiple", ScrollSnap.ScrollDirection.Vertical, 3, 15, new Vector2(100, 100));
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -165,7 +165,7 @@ namespace UnityEngine.UI.Extensions
|
|||
var h = CalculateRowVerticalOffset(groupHeight, yOffset, currentRowHeight);
|
||||
currentRowWidth -= SpacingX;
|
||||
// Layout the final row
|
||||
LayoutRow(_rowList, currentRowWidth, currentRowHeight, workingWidth, padding.left, h, axis);
|
||||
LayoutRow(_rowList, currentRowWidth, currentRowHeight, workingWidth - (_rowList.Count > 1 ? SpacingX : 0), padding.left, h, axis);
|
||||
}
|
||||
|
||||
_rowList.Clear();
|
||||
|
|
|
@ -211,7 +211,7 @@ namespace UnityEngine.UI.Extensions
|
|||
RectTransform child = _screensContainer.transform.GetChild(i).gameObject.GetComponent<RectTransform>();
|
||||
currentXPosition = _offset + i * _step;
|
||||
child.anchoredPosition = new Vector2(currentXPosition, 0f);
|
||||
child.sizeDelta = new Vector2(gameObject.GetComponent<RectTransform>().sizeDelta.x, gameObject.GetComponent<RectTransform>().sizeDelta.y);
|
||||
child.sizeDelta = new Vector2(gameObject.GetComponent<RectTransform>().rect.width, gameObject.GetComponent<RectTransform>().rect.height);
|
||||
}
|
||||
|
||||
_dimension = currentXPosition + _offset * -1;
|
||||
|
|
|
@ -0,0 +1,495 @@
|
|||
/// 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.
|
||||
/// Update by xesenix - rewrited almost entire code
|
||||
/// - configuration for direction move instead of 2 concurrent class (easiear to change direction in editor)
|
||||
/// - supports list layouted with horizontal or vertical layout need to match direction with type of layout used
|
||||
/// - dynamicly checks if scrolled list size changes and recalculates anchor positions
|
||||
/// and item size based on itemsVisibleAtOnce and size of root container
|
||||
/// if you dont wish to use this auto resize turn of autoLayoutItems
|
||||
/// - fixed current page made it independant from pivot
|
||||
/// - replaced pagination with delegate function
|
||||
|
||||
using System;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(ScrollRect))]
|
||||
[AddComponentMenu("UI/Extensions/Scroll Snap")]
|
||||
public class ScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
|
||||
{
|
||||
// needed becouse of reversed behavior of axis Y compared to X
|
||||
// (positions of children lower in children list in horizontal directions grows when in vertical it gets smaller)
|
||||
public enum ScrollDirection {
|
||||
Horizontal,
|
||||
Vertical
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
protected int items = 0;
|
||||
|
||||
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;
|
||||
|
||||
[Tooltip("Button to go to the next page. (optional)")]
|
||||
public GameObject nextButton;
|
||||
[Tooltip("Button to go to the previous page. (optional)")]
|
||||
public GameObject 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;
|
||||
|
||||
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 Start()
|
||||
{
|
||||
lerp = false;
|
||||
|
||||
scrollRect = gameObject.GetComponent<ScrollRect> ();
|
||||
scrollRectTransform = gameObject.GetComponent<RectTransform> ();
|
||||
listContainerTransform = scrollRect.content;
|
||||
listContainerRectTransform = listContainerTransform.GetComponent<RectTransform> ();
|
||||
|
||||
rectTransform = listContainerTransform.gameObject.GetComponent<RectTransform> ();
|
||||
UpdateListItemsSize();
|
||||
UpdateListItemPositions();
|
||||
|
||||
ChangePage (CurrentPage ());
|
||||
|
||||
if (nextButton)
|
||||
{
|
||||
nextButton.GetComponent<Button> ().onClick.AddListener (() => { NextScreen (); });
|
||||
}
|
||||
|
||||
if (prevButton)
|
||||
{
|
||||
prevButton.GetComponent<Button> ().onClick.AddListener (() => { PreviousScreen (); });
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateListItemsSize()
|
||||
{
|
||||
float size = 0;
|
||||
if (direction == ScrollSnap.ScrollDirection.Horizontal)
|
||||
{
|
||||
size = scrollRectTransform.rect.width / itemsVisibleAtOnce;
|
||||
}
|
||||
else
|
||||
{
|
||||
size = scrollRectTransform.rect.height / itemsVisibleAtOnce;
|
||||
}
|
||||
|
||||
itemSize = size;
|
||||
|
||||
if (autoLayoutItems && size != itemSize)
|
||||
{
|
||||
if (direction == ScrollSnap.ScrollDirection.Horizontal)
|
||||
{
|
||||
foreach (var tr in listContainerTransform)
|
||||
{
|
||||
GameObject child = ((Transform)tr).gameObject;
|
||||
if (child.activeInHierarchy)
|
||||
{
|
||||
var childLayout = child.GetComponent<LayoutElement> ();
|
||||
|
||||
if (childLayout == null)
|
||||
{
|
||||
childLayout = child.AddComponent<LayoutElement> ();
|
||||
}
|
||||
|
||||
childLayout.minWidth = itemSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var tr in listContainerTransform)
|
||||
{
|
||||
GameObject child = ((Transform)tr).gameObject;
|
||||
if (child.activeInHierarchy)
|
||||
{
|
||||
var childLayout = child.GetComponent<LayoutElement> ();
|
||||
|
||||
if (childLayout == null)
|
||||
{
|
||||
childLayout = child.AddComponent<LayoutElement> ();
|
||||
}
|
||||
|
||||
childLayout.minHeight = itemSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateListItemPositions()
|
||||
{
|
||||
if (!listContainerRectTransform.rect.size.Equals(listContainerCachedSize))
|
||||
{
|
||||
// checking how many children of list are active
|
||||
int activeCount = 0;
|
||||
|
||||
foreach (var tr in listContainerTransform) {
|
||||
if (((Transform)tr).gameObject.activeInHierarchy) {
|
||||
activeCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// if anything changed since last check reinitialize anchors list
|
||||
items = 0;
|
||||
Array.Resize(ref pageAnchorPositions, activeCount);
|
||||
|
||||
if (activeCount > 0)
|
||||
{
|
||||
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;
|
||||
|
||||
listContainerSize = listContainerMaxPosition - listContainerMinPosition;
|
||||
|
||||
for (var i = 0; i < pages; i ++)
|
||||
{
|
||||
pageAnchorPositions[i] = new Vector3(
|
||||
listContainerMaxPosition - itemSize * i,
|
||||
listContainerTransform.localPosition.y,
|
||||
listContainerTransform.localPosition.z
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// looking for list spanning range
|
||||
scrollRect.verticalNormalizedPosition = 1;
|
||||
listContainerMinPosition = listContainerTransform.localPosition.y;
|
||||
scrollRect.verticalNormalizedPosition = 0;
|
||||
listContainerMaxPosition = listContainerTransform.localPosition.y;
|
||||
|
||||
listContainerSize = listContainerMaxPosition - listContainerMinPosition;
|
||||
|
||||
for (var i = 0; i < pages; i ++)
|
||||
{
|
||||
pageAnchorPositions[i] = new Vector3(
|
||||
listContainerTransform.localPosition.x,
|
||||
listContainerMinPosition + itemSize * i,
|
||||
listContainerTransform.localPosition.z
|
||||
);
|
||||
}
|
||||
|
||||
foreach (var tr in listContainerTransform) {
|
||||
if (((Transform)tr).gameObject.activeInHierarchy) {
|
||||
activeCount++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
UpdateScrollbar(linkScrolbarSteps);
|
||||
startingPage = Mathf.Min(startingPage, pages);
|
||||
ResetPage();
|
||||
}
|
||||
items = activeCount;
|
||||
listContainerCachedSize.Set (listContainerRectTransform.rect.size.x, listContainerRectTransform.rect.size.y);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void ResetPage()
|
||||
{
|
||||
if (direction == ScrollDirection.Horizontal)
|
||||
{
|
||||
scrollRect.horizontalNormalizedPosition = pages > 1 ? (float)startingPage / (float)(pages - 1) : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
scrollRect.verticalNormalizedPosition = pages > 1 ? (float)(pages - startingPage - 1) / (float)(pages - 1) : 0;
|
||||
}
|
||||
}
|
||||
|
||||
protected void UpdateScrollbar(bool linkSteps)
|
||||
{
|
||||
if (linkSteps)
|
||||
{
|
||||
if (direction == ScrollDirection.Horizontal)
|
||||
{
|
||||
if (scrollRect.horizontalScrollbar != null)
|
||||
{
|
||||
scrollRect.horizontalScrollbar.numberOfSteps = pages;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (scrollRect.verticalScrollbar != null)
|
||||
{
|
||||
scrollRect.verticalScrollbar.numberOfSteps = pages;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (direction == ScrollDirection.Horizontal)
|
||||
{
|
||||
if (scrollRect.horizontalScrollbar != null)
|
||||
{
|
||||
scrollRect.horizontalScrollbar.numberOfSteps = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (scrollRect.verticalScrollbar != null)
|
||||
{
|
||||
scrollRect.verticalScrollbar.numberOfSteps = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateListItemsSize();
|
||||
UpdateListItemPositions();
|
||||
|
||||
if (lerp)
|
||||
{
|
||||
UpdateScrollbar(false);
|
||||
|
||||
listContainerTransform.localPosition = Vector3.Lerp(listContainerTransform.localPosition, lerpTarget, 7.5f * Time.deltaTime);
|
||||
|
||||
if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 0.001f)
|
||||
{
|
||||
listContainerTransform.localPosition = lerpTarget;
|
||||
lerp = false;
|
||||
|
||||
UpdateScrollbar(linkScrolbarSteps);
|
||||
}
|
||||
|
||||
//change the info bullets at the bottom of the screen. Just for visual effect
|
||||
if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 10f)
|
||||
{
|
||||
ChangePage(CurrentPage());
|
||||
}
|
||||
}
|
||||
|
||||
if (fastSwipeTimer)
|
||||
{
|
||||
fastSwipeCounter++;
|
||||
}
|
||||
}
|
||||
|
||||
private bool fastSwipe = false; //to determine if a fast swipe was performed
|
||||
|
||||
|
||||
//Function for switching screens with buttons
|
||||
public void NextScreen()
|
||||
{
|
||||
UpdateListItemPositions ();
|
||||
|
||||
if (CurrentPage() < pages - 1)
|
||||
{
|
||||
lerp = true;
|
||||
lerpTarget = pageAnchorPositions[CurrentPage() + 1];
|
||||
|
||||
ChangePage(CurrentPage() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
//Function for switching screens with buttons
|
||||
public void PreviousScreen()
|
||||
{
|
||||
UpdateListItemPositions ();
|
||||
|
||||
if (CurrentPage() > 0)
|
||||
{
|
||||
lerp = true;
|
||||
lerpTarget = pageAnchorPositions[CurrentPage() - 1];
|
||||
|
||||
ChangePage(CurrentPage() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
//Because the CurrentScreen function is not so reliable, these are the functions used for swipes
|
||||
private void NextScreenCommand()
|
||||
{
|
||||
if (pageOnDragStart < pages - 1)
|
||||
{
|
||||
int targetPage = Mathf.Min(pages - 1, pageOnDragStart + itemsVisibleAtOnce);
|
||||
lerp = true;
|
||||
|
||||
lerpTarget = pageAnchorPositions[targetPage];
|
||||
|
||||
ChangePage(targetPage);
|
||||
}
|
||||
}
|
||||
|
||||
//Because the CurrentScreen function is not so reliable, these are the functions used for swipes
|
||||
private void PrevScreenCommand()
|
||||
{
|
||||
if (pageOnDragStart > 0)
|
||||
{
|
||||
int targetPage = Mathf.Max(0, pageOnDragStart - itemsVisibleAtOnce);
|
||||
lerp = true;
|
||||
|
||||
lerpTarget = pageAnchorPositions[targetPage];
|
||||
|
||||
ChangePage(targetPage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//returns the current screen that the is seeing
|
||||
public int CurrentPage()
|
||||
{
|
||||
float pos;
|
||||
|
||||
if (direction == ScrollDirection.Horizontal)
|
||||
{
|
||||
pos = listContainerMaxPosition - listContainerTransform.localPosition.x;
|
||||
pos = Mathf.Clamp(pos, 0, listContainerSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
pos = listContainerTransform.localPosition.y - listContainerMinPosition;
|
||||
pos = Mathf.Clamp(pos, 0, listContainerSize);
|
||||
}
|
||||
|
||||
float page = pos / itemSize;
|
||||
|
||||
return Mathf.Clamp(Mathf.RoundToInt(page), 0, pages);
|
||||
}
|
||||
|
||||
//changes the bullets on the bottom of the page - pagination
|
||||
private void ChangePage(int currentPage)
|
||||
{
|
||||
startingPage = currentPage;
|
||||
|
||||
if (onPageChange != null)
|
||||
{
|
||||
onPageChange(currentPage);
|
||||
}
|
||||
}
|
||||
|
||||
#region Interfaces
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
UpdateScrollbar(false);
|
||||
|
||||
fastSwipeCounter = 0;
|
||||
fastSwipeTimer = true;
|
||||
|
||||
positionOnDragStart = eventData.position;
|
||||
pageOnDragStart = CurrentPage();
|
||||
}
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
startDrag = true;
|
||||
float change = 0;
|
||||
|
||||
if (direction == ScrollDirection.Horizontal)
|
||||
{
|
||||
change = positionOnDragStart.x - eventData.position.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
change = -positionOnDragStart.y + eventData.position.y;
|
||||
}
|
||||
|
||||
if (useFastSwipe)
|
||||
{
|
||||
fastSwipe = false;
|
||||
fastSwipeTimer = false;
|
||||
|
||||
if (fastSwipeCounter <= fastSwipeTarget)
|
||||
{
|
||||
if (Math.Abs(change) > fastSwipeThreshold)
|
||||
{
|
||||
fastSwipe = true;
|
||||
}
|
||||
}
|
||||
if (fastSwipe)
|
||||
{
|
||||
if (change > 0)
|
||||
{
|
||||
NextScreenCommand();
|
||||
}
|
||||
else
|
||||
{
|
||||
PrevScreenCommand();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lerp = true;
|
||||
lerpTarget = pageAnchorPositions[CurrentPage()];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lerp = true;
|
||||
lerpTarget = pageAnchorPositions[CurrentPage()];
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
lerp = false;
|
||||
|
||||
if (startDrag)
|
||||
{
|
||||
OnBeginDrag(eventData);
|
||||
startDrag = false;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c598b387777d96643991be3f0b6c98c2
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
|
@ -0,0 +1,127 @@
|
|||
/// Credit zero3growlithe
|
||||
/// sourced from: http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-2011648
|
||||
/// Update by xesenix - based on UIScrollToSelection centers on selected element in scrollrect which can move in XY
|
||||
/// you can restrict movement by locking axis on ScrollRect component
|
||||
|
||||
/*USAGE:
|
||||
Simply place the script on the ScrollRect that contains the selectable children we'll be scroling to
|
||||
and drag'n'drop the RectTransform of the options "container" that we'll be scrolling.*/
|
||||
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
[RequireComponent(typeof(ScrollRect))]
|
||||
public class UIScrollToSelectionXY : MonoBehaviour
|
||||
{
|
||||
|
||||
#region Variables
|
||||
|
||||
// settings
|
||||
public float scrollSpeed = 10f;
|
||||
|
||||
[SerializeField]
|
||||
private RectTransform layoutListGroup;
|
||||
|
||||
// temporary variables
|
||||
private RectTransform targetScrollObject;
|
||||
private bool scrollToSelection = true;
|
||||
|
||||
// references
|
||||
private RectTransform scrollWindow;
|
||||
private RectTransform currentCanvas;
|
||||
private ScrollRect targetScrollRect;
|
||||
#endregion
|
||||
|
||||
// Use this for initialization
|
||||
private void Start()
|
||||
{
|
||||
targetScrollRect = GetComponent<ScrollRect>();
|
||||
scrollWindow = targetScrollRect.GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
ScrollRectToLevelSelection();
|
||||
}
|
||||
|
||||
private void ScrollRectToLevelSelection()
|
||||
{
|
||||
// FIX: if you dont do that here events can have null value
|
||||
var events = EventSystem.current;
|
||||
|
||||
// check main references
|
||||
bool referencesAreIncorrect =
|
||||
(targetScrollRect == null || layoutListGroup == null || scrollWindow == null);
|
||||
if (referencesAreIncorrect == true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// get calculation references
|
||||
RectTransform selection = events.currentSelectedGameObject != null ?
|
||||
events.currentSelectedGameObject.GetComponent<RectTransform>() :
|
||||
null;
|
||||
|
||||
if (selection != targetScrollObject)
|
||||
{
|
||||
scrollToSelection = true;
|
||||
}
|
||||
|
||||
// check if scrolling is possible
|
||||
bool isScrollDirectionUnknown = (selection == null || scrollToSelection == false);
|
||||
|
||||
if (isScrollDirectionUnknown == true || selection.transform.parent != layoutListGroup.transform)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool finishedX = false, finishedY = false;
|
||||
|
||||
if (targetScrollRect.vertical)
|
||||
{
|
||||
// move the current scroll rect to correct position
|
||||
float selectionPos = -selection.anchoredPosition.y;
|
||||
|
||||
//float elementHeight = layoutListGroup.sizeDelta.y / layoutListGroup.transform.childCount;
|
||||
//float maskHeight = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y;
|
||||
float listPixelAnchor = layoutListGroup.anchoredPosition.y;
|
||||
|
||||
// get the element offset value depending on the cursor move direction
|
||||
float offlimitsValue = 0;
|
||||
|
||||
offlimitsValue = listPixelAnchor - selectionPos;
|
||||
// move the target scroll rect
|
||||
targetScrollRect.verticalNormalizedPosition += (offlimitsValue / layoutListGroup.sizeDelta.y) * Time.deltaTime * scrollSpeed;
|
||||
|
||||
finishedY = Mathf.Abs(offlimitsValue) < 2f;
|
||||
}
|
||||
|
||||
if (targetScrollRect.horizontal)
|
||||
{
|
||||
// move the current scroll rect to correct position
|
||||
float selectionPos = -selection.anchoredPosition.x;
|
||||
|
||||
//float elementWidth = layoutListGroup.sizeDelta.x / layoutListGroup.transform.childCount;
|
||||
//float maskWidth = currentCanvas.sizeDelta.y + scrollWindow.sizeDelta.y;
|
||||
float listPixelAnchor = layoutListGroup.anchoredPosition.x;
|
||||
|
||||
// get the element offset value depending on the cursor move direction
|
||||
float offlimitsValue = 0;
|
||||
|
||||
offlimitsValue = listPixelAnchor - selectionPos;
|
||||
// move the target scroll rect
|
||||
targetScrollRect.horizontalNormalizedPosition += (offlimitsValue / layoutListGroup.sizeDelta.x) * Time.deltaTime * scrollSpeed;
|
||||
|
||||
finishedX = Mathf.Abs(offlimitsValue) < 2f;
|
||||
}
|
||||
// check if we reached our destination
|
||||
if (finishedX && finishedY) {
|
||||
scrollToSelection = false;
|
||||
}
|
||||
// save last object we were "heading to" to prevent blocking
|
||||
targetScrollObject = selection;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6580683838fcc12479150dd5e76e6b2e
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
Loading…
Reference in New Issue