Updated Vertical and Horrizontal Scroll Snaps controls to include:

* Tranisition speed
* Starting page
* Page Snap size (to control distribution of pages)

Resolves requested issues / fixes

--HG--
branch : develop_5.3
pull/413/head
Simon (darkside) Jackson 2016-05-17 22:37:01 +01:00
parent 3b5bdaaa5a
commit 3d39d1da47
5 changed files with 105 additions and 79 deletions

View File

@ -7,6 +7,7 @@ using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[RequireComponent(typeof(ScrollRect))] [RequireComponent(typeof(ScrollRect))]
[AddComponentMenu("Layout/Extensions/Horizontal Scroll Snap")] [AddComponentMenu("Layout/Extensions/Horizontal Scroll Snap")]
public class HorizontalScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler public class HorizontalScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
@ -14,7 +15,6 @@ namespace UnityEngine.UI.Extensions
private Transform _screensContainer; private Transform _screensContainer;
private int _screens = 1; private int _screens = 1;
private int _startingScreen = 2;
private bool _fastSwipeTimer = false; private bool _fastSwipeTimer = false;
private int _fastSwipeCounter = 0; private int _fastSwipeCounter = 0;
@ -28,31 +28,6 @@ namespace UnityEngine.UI.Extensions
private int _containerSize; private int _containerSize;
public int StartingScreen
{
get
{
return _startingScreen;
}
set
{
if (_startingScreen == value)
return;
if (value < _screens)
{
_startingScreen = 1;
}
else if (value > _screens)
{
_startingScreen = transform.childCount;
}
else
{
_startingScreen = value;
}
}
}
[Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")] [Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")]
public GameObject Pagination; public GameObject Pagination;
@ -70,11 +45,20 @@ namespace UnityEngine.UI.Extensions
private Vector3 _startPosition = new Vector3(); private Vector3 _startPosition = new Vector3();
private int _currentScreen; private int _currentScreen;
public int StartingScreen = 1;
public int PageStep = 0;
// Use this for initialization // Use this for initialization
void Start() void Start()
{ {
_scroll_rect = gameObject.GetComponent<ScrollRect>(); _scroll_rect = gameObject.GetComponent<ScrollRect>();
_screensContainer = _scroll_rect.content; _screensContainer = _scroll_rect.content;
if (PageStep == 0)
{
PageStep = (int)_scroll_rect.GetComponent<RectTransform>().rect.width * 3;
}
DistributePages(); DistributePages();
_screens = _screensContainer.childCount; _screens = _screensContainer.childCount;
@ -85,18 +69,18 @@ namespace UnityEngine.UI.Extensions
if (_screens > 0) if (_screens > 0)
{ {
for (int i = 0; i < _screens; ++i) for (float i = 0; i < _screens; ++i)
{ {
_scroll_rect.horizontalNormalizedPosition = (float)i / (float)(_screens - 1); _scroll_rect.horizontalNormalizedPosition = i / (_screens - 1);
_positions.Add(_screensContainer.localPosition); _positions.Add(_screensContainer.localPosition);
} }
} }
_scroll_rect.horizontalNormalizedPosition = (float)(_startingScreen - 1) / (_screens - 1); _scroll_rect.horizontalNormalizedPosition = (float)(StartingScreen - 1) / (_screens - 1);
_containerSize = (int)_screensContainer.gameObject.GetComponent<RectTransform>().offsetMax.x; _containerSize = (int)_screensContainer.gameObject.GetComponent<RectTransform>().offsetMax.x;
ChangeBulletsInfo(CurrentScreen()); ChangeBulletsInfo(_currentScreen);
if (NextButton) if (NextButton)
NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); }); NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); });
@ -126,7 +110,6 @@ namespace UnityEngine.UI.Extensions
{ {
_fastSwipeCounter++; _fastSwipeCounter++;
} }
} }
private bool fastSwipe = false; //to determine if a fast swipe was performed private bool fastSwipe = false; //to determine if a fast swipe was performed
@ -135,24 +118,26 @@ namespace UnityEngine.UI.Extensions
//Function for switching screens with buttons //Function for switching screens with buttons
public void NextScreen() public void NextScreen()
{ {
if (CurrentScreen() < _screens - 1) if (_currentScreen < _screens - 1)
{ {
_currentScreen++;
_lerp = true; _lerp = true;
_lerp_target = _positions[CurrentScreen() + 1]; _lerp_target = _positions[_currentScreen];
ChangeBulletsInfo(CurrentScreen() + 1); ChangeBulletsInfo(_currentScreen);
} }
} }
//Function for switching screens with buttons //Function for switching screens with buttons
public void PreviousScreen() public void PreviousScreen()
{ {
if (CurrentScreen() > 0) if (_currentScreen > 0)
{ {
_currentScreen--;
_lerp = true; _lerp = true;
_lerp_target = _positions[CurrentScreen() - 1]; _lerp_target = _positions[_currentScreen];
ChangeBulletsInfo(CurrentScreen() - 1); ChangeBulletsInfo(_currentScreen);
} }
} }
@ -203,13 +188,8 @@ namespace UnityEngine.UI.Extensions
//returns the current screen that the is seeing //returns the current screen that the is seeing
public int CurrentScreen() public int CurrentScreen()
{ {
float absPoz = Math.Abs(_screensContainer.gameObject.GetComponent<RectTransform>().offsetMin.x); var pos = FindClosestFrom(_screensContainer.localPosition, _positions);
return _currentScreen = GetPageforPosition(pos);
absPoz = Mathf.Clamp(absPoz, 1, _containerSize - 1);
float calc = (absPoz / _containerSize) * _screens;
return (int)calc;
} }
//changes the bullets on the bottom of the page - pagination //changes the bullets on the bottom of the page - pagination
@ -228,21 +208,16 @@ namespace UnityEngine.UI.Extensions
private void DistributePages() private void DistributePages()
{ {
int _offset = 0; int _offset = 0;
int _step = Screen.width;
//Options to fix - stepping causes issues with overlap
//float _step = GameObject.FindObjectOfType<Canvas> ().GetComponent<RectTransform>().rect.width;
//int _step = (int)(transform.root.GetComponent<RectTransform>().sizeDelta.x);
int _dimension = 0; int _dimension = 0;
Vector2 panelDimensions = gameObject.GetComponent<RectTransform>().sizeDelta;
int currentXPosition = 0; int currentXPosition = 0;
for (int i = 0; i < _screensContainer.transform.childCount; i++) for (int i = 0; i < _screensContainer.transform.childCount; i++)
{ {
RectTransform child = _screensContainer.transform.GetChild(i).gameObject.GetComponent<RectTransform>(); RectTransform child = _screensContainer.transform.GetChild(i).gameObject.GetComponent<RectTransform>();
_step = (int)child.rect.width * 3; currentXPosition = _offset + i * PageStep;
currentXPosition = _offset + i * _step; child.sizeDelta = new Vector2(panelDimensions.x, panelDimensions.y);
child.anchoredPosition = new Vector2(currentXPosition, 0f); child.anchoredPosition = new Vector2(currentXPosition, 0f);
child.sizeDelta = new Vector2(gameObject.GetComponent<RectTransform>().rect.width, gameObject.GetComponent<RectTransform>().rect.height);
} }
_dimension = currentXPosition + _offset * -1; _dimension = currentXPosition + _offset * -1;
@ -250,6 +225,30 @@ namespace UnityEngine.UI.Extensions
_screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(_dimension, 0f); _screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(_dimension, 0f);
} }
int GetPageforPosition(Vector3 pos)
{
for (int i = 0; i < _positions.Count; i++)
{
if (_positions[i] == pos)
{
return i;
}
}
return 0;
}
void OnValidate()
{
var childCount = gameObject.GetComponent<ScrollRect>().content.childCount;
if (StartingScreen > childCount)
{
StartingScreen = childCount;
}
if (StartingScreen < 1)
{
StartingScreen = 1;
}
}
#region Interfaces #region Interfaces
public void OnBeginDrag(PointerEventData eventData) public void OnBeginDrag(PointerEventData eventData)
{ {
@ -290,12 +289,14 @@ namespace UnityEngine.UI.Extensions
{ {
_lerp = true; _lerp = true;
_lerp_target = FindClosestFrom(_screensContainer.localPosition, _positions); _lerp_target = FindClosestFrom(_screensContainer.localPosition, _positions);
_currentScreen = GetPageforPosition(_lerp_target);
} }
} }
else else
{ {
_lerp = true; _lerp = true;
_lerp_target = FindClosestFrom(_screensContainer.localPosition, _positions); _lerp_target = FindClosestFrom(_screensContainer.localPosition, _positions);
_currentScreen = GetPageforPosition(_lerp_target);
} }
} }
} }
@ -310,5 +311,7 @@ namespace UnityEngine.UI.Extensions
} }
} }
#endregion #endregion
} }
} }

View File

@ -31,7 +31,7 @@ namespace UnityEngine.UI.Extensions
//private int elementsDistance; //private int elementsDistance;
private int minElementsNum; private int minElementsNum;
private int elementLength; private int elementLength;
private int elementHalfLength; //private int elementHalfLength;
private float deltaY; private float deltaY;
private string result; private string result;

View File

@ -14,7 +14,6 @@ namespace UnityEngine.UI.Extensions
private Transform _screensContainer; private Transform _screensContainer;
private int _screens = 1; private int _screens = 1;
private int _startingScreen = 1;
private bool _fastSwipeTimer = false; private bool _fastSwipeTimer = false;
private int _fastSwipeCounter = 0; private int _fastSwipeCounter = 0;
@ -45,11 +44,18 @@ namespace UnityEngine.UI.Extensions
private Vector3 _startPosition = new Vector3(); private Vector3 _startPosition = new Vector3();
private int _currentScreen; private int _currentScreen;
public int StartingScreen = 1;
public int PageStep = 0;
// Use this for initialization // Use this for initialization
void Start() void Start()
{ {
_scroll_rect = gameObject.GetComponent<ScrollRect>(); _scroll_rect = gameObject.GetComponent<ScrollRect>();
_screensContainer = _scroll_rect.content; _screensContainer = _scroll_rect.content;
if (PageStep == 0)
{
PageStep = (int)_scroll_rect.GetComponent<RectTransform>().rect.height * 3;
}
DistributePages(); DistributePages();
_screens = _screensContainer.childCount; _screens = _screensContainer.childCount;
@ -67,11 +73,11 @@ namespace UnityEngine.UI.Extensions
} }
} }
_scroll_rect.verticalNormalizedPosition = (float)(_startingScreen - 1) / (float)(_screens - 1); _scroll_rect.verticalNormalizedPosition = (float)(StartingScreen - 1) / (float)(_screens - 1);
_containerSize = (int)_screensContainer.gameObject.GetComponent<RectTransform>().offsetMax.y; _containerSize = (int)_screensContainer.gameObject.GetComponent<RectTransform>().offsetMax.y;
ChangeBulletsInfo(CurrentScreen()); ChangeBulletsInfo(_currentScreen);
if (NextButton) if (NextButton)
NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); }); NextButton.GetComponent<Button>().onClick.AddListener(() => { NextScreen(); });
@ -110,24 +116,26 @@ namespace UnityEngine.UI.Extensions
//Function for switching screens with buttons //Function for switching screens with buttons
public void NextScreen() public void NextScreen()
{ {
if (CurrentScreen() < _screens - 1) if (_currentScreen < _screens - 1)
{ {
_currentScreen++;
_lerp = true; _lerp = true;
_lerp_target = _positions[CurrentScreen() + 1]; _lerp_target = _positions[_currentScreen];
ChangeBulletsInfo(CurrentScreen() + 1); ChangeBulletsInfo(_currentScreen);
} }
} }
//Function for switching screens with buttons //Function for switching screens with buttons
public void PreviousScreen() public void PreviousScreen()
{ {
if (CurrentScreen() > 0) if (_currentScreen > 0)
{ {
_currentScreen--;
_lerp = true; _lerp = true;
_lerp_target = _positions[CurrentScreen() - 1]; _lerp_target = _positions[_currentScreen];
ChangeBulletsInfo(CurrentScreen() - 1); ChangeBulletsInfo(_currentScreen);
} }
} }
@ -178,13 +186,8 @@ namespace UnityEngine.UI.Extensions
//returns the current screen that the is seeing //returns the current screen that the is seeing
public int CurrentScreen() public int CurrentScreen()
{ {
float absPoz = Math.Abs(_screensContainer.gameObject.GetComponent<RectTransform>().offsetMin.y); var pos = FindClosestFrom(_screensContainer.localPosition, _positions);
return _currentScreen = GetPageforPosition(pos);
absPoz = Mathf.Clamp(absPoz, 1, _containerSize - 1);
float calc = (absPoz / _containerSize) * _screens;
return (int)calc;
} }
//changes the bullets on the bottom of the page - pagination //changes the bullets on the bottom of the page - pagination
@ -203,20 +206,14 @@ namespace UnityEngine.UI.Extensions
private void DistributePages() private void DistributePages()
{ {
float _offset = 0; float _offset = 0;
float _step = Screen.height;
//Options to fix - stepping causes issues with overlap
//float _step = GameObject.FindObjectOfType<Canvas> ().GetComponent<RectTransform>().rect.height;
//int _step = (int)(transform.root.GetComponent<RectTransform>().sizeDelta.y);
float _dimension = 0; float _dimension = 0;
Vector2 panelDimensions = gameObject.GetComponent<RectTransform>().sizeDelta; Vector2 panelDimensions = gameObject.GetComponent<RectTransform>().sizeDelta;
float currentYPosition = 0; float currentYPosition = 0;
for (int i = 0; i < _screensContainer.transform.childCount; i++) for (int i = 0; i < _screensContainer.transform.childCount; i++)
{ {
RectTransform child = _screensContainer.transform.GetChild(i).gameObject.GetComponent<RectTransform>(); RectTransform child = _screensContainer.transform.GetChild(i).gameObject.GetComponent<RectTransform>();
currentYPosition = _offset + i * _step; currentYPosition = _offset + i * PageStep;
child.sizeDelta = new Vector2(panelDimensions.x, panelDimensions.y); child.sizeDelta = new Vector2(panelDimensions.x, panelDimensions.y);
child.anchoredPosition = new Vector2(0f - panelDimensions.x / 2, currentYPosition + panelDimensions.y / 2); child.anchoredPosition = new Vector2(0f - panelDimensions.x / 2, currentYPosition + panelDimensions.y / 2);
} }
@ -226,6 +223,30 @@ namespace UnityEngine.UI.Extensions
_screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(0f,_dimension); _screensContainer.GetComponent<RectTransform>().offsetMax = new Vector2(0f,_dimension);
} }
int GetPageforPosition(Vector3 pos)
{
for (int i = 0; i < _positions.Count; i++)
{
if (_positions[i] == pos)
{
return i;
}
}
return 0;
}
void OnValidate()
{
var childCount = gameObject.GetComponent<ScrollRect>().content.childCount;
if (StartingScreen > childCount)
{
StartingScreen = childCount;
}
if (StartingScreen < 1)
{
StartingScreen = 1;
}
}
#region Interfaces #region Interfaces
public void OnBeginDrag(PointerEventData eventData) public void OnBeginDrag(PointerEventData eventData)
{ {
@ -266,12 +287,14 @@ namespace UnityEngine.UI.Extensions
{ {
_lerp = true; _lerp = true;
_lerp_target = FindClosestFrom(_screensContainer.localPosition, _positions); _lerp_target = FindClosestFrom(_screensContainer.localPosition, _positions);
_currentScreen = GetPageforPosition(_lerp_target);
} }
} }
else else
{ {
_lerp = true; _lerp = true;
_lerp_target = FindClosestFrom(_screensContainer.localPosition, _positions); _lerp_target = FindClosestFrom(_screensContainer.localPosition, _positions);
_currentScreen = GetPageforPosition(_lerp_target);
} }
} }
} }

View File

@ -30,7 +30,7 @@ namespace UnityEngine.UI.Extensions
// public float height; // public float height;
//detect canvas mode so to apply different behaviors to different canvas modes, currently only RenderMode.ScreenSpaceCamera implemented //detect canvas mode so to apply different behaviors to different canvas modes, currently only RenderMode.ScreenSpaceCamera implemented
int canvasMode; //int canvasMode;
RenderMode GUIMode; RenderMode GUIMode;
//the scene GUI camera //the scene GUI camera

View File

@ -34,7 +34,7 @@ namespace UnityEngine.UI.Extensions
// private bool _xShifted, _yShifted = false; // private bool _xShifted, _yShifted = false;
private float width, height, canvasWidth, canvasHeight; private float width, height;//, canvasWidth, canvasHeight;
// private int screenWidth, screenHeight; // private int screenWidth, screenHeight;