Updated Scroll Snap Base to resolve #119

pull/413/head
Simon Jackson 2017-02-23 15:18:16 +00:00
parent 48e162ebde
commit cc7c66f9f5
1 changed files with 374 additions and 366 deletions

View File

@ -40,7 +40,7 @@ namespace UnityEngine.UI.Extensions
[Tooltip("The distance between two pages based on page height, by default pages are next to each other")] [Tooltip("The distance between two pages based on page height, by default pages are next to each other")]
[SerializeField] [SerializeField]
[Range(1, 8)] [Range(0, 8)]
public float PageStep = 1; public float PageStep = 1;
[Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")] [Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")]
@ -76,6 +76,7 @@ namespace UnityEngine.UI.Extensions
{ {
return _currentPage; return _currentPage;
} }
internal set internal set
{ {
if ((value != _currentPage && value >= 0 && value < _screensContainer.childCount) || (value == 0 && _screensContainer.childCount == 0)) if ((value != _currentPage && value >= 0 && value < _screensContainer.childCount) || (value == 0 && _screensContainer.childCount == 0))
@ -110,7 +111,6 @@ namespace UnityEngine.UI.Extensions
private SelectionChangeEndEvent m_OnSelectionChangeEndEvent = new SelectionChangeEndEvent(); private SelectionChangeEndEvent m_OnSelectionChangeEndEvent = new SelectionChangeEndEvent();
public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } }
// Use this for initialization // Use this for initialization
void Awake() void Awake()
{ {
@ -134,6 +134,7 @@ namespace UnityEngine.UI.Extensions
Debug.LogError("ScrollRect Content has children, this is not supported when using managed Child Objects\n Either remove the ScrollRect Content children or clear the ChildObjects array"); Debug.LogError("ScrollRect Content has children, this is not supported when using managed Child Objects\n Either remove the ScrollRect Content children or clear the ChildObjects array");
return; return;
} }
InitialiseChildObjectsFromArray(); InitialiseChildObjectsFromArray();
} }
else else
@ -178,6 +179,7 @@ namespace UnityEngine.UI.Extensions
childRect.localScale = _screensContainer.localScale; childRect.localScale = _screensContainer.localScale;
childRect.position = _screensContainer.position; childRect.position = _screensContainer.position;
} }
child.transform.SetParent(_screensContainer.transform); child.transform.SetParent(_screensContainer.transform);
ChildObjects[i] = child; ChildObjects[i] = child;
if (MaskArea && ChildObjects[i].activeSelf) if (MaskArea && ChildObjects[i].activeSelf)
@ -199,17 +201,19 @@ namespace UnityEngine.UI.Extensions
_halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2; _halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2;
_bottomItem = _topItem = 0; _bottomItem = _topItem = 0;
//work out how many items below the current page can be visible //work out how many items below the current page can be visible
for (int i = _halfNoVisibleItems + 1; i > 0 ; i--) for (int i = _halfNoVisibleItems + 1; i > 0; i--)
{ {
_bottomItem = _currentPage - i < 0 ? 0 : i; _bottomItem = _currentPage - i < 0 ? 0 : i;
if (_bottomItem > 0) break; if (_bottomItem > 0) break;
} }
//work out how many items above the current page can be visible //work out how many items above the current page can be visible
for (int i = _halfNoVisibleItems + 1; i > 0; i--) for (int i = _halfNoVisibleItems + 1; i > 0; i--)
{ {
_topItem = _screensContainer.childCount - _currentPage - i < 0 ? 0 : i; _topItem = _screensContainer.childCount - _currentPage - i < 0 ? 0 : i;
if (_topItem > 0) break; if (_topItem > 0) break;
} }
//Set the active items active //Set the active items active
for (int i = CurrentPage - _bottomItem; i < CurrentPage + _topItem; i++) for (int i = CurrentPage - _bottomItem; i < CurrentPage + _topItem; i++)
{ {
@ -229,7 +233,6 @@ namespace UnityEngine.UI.Extensions
if (_screensContainer.childCount - _currentPage > _topItem) ChildObjects[CurrentPage + _topItem].SetActive(false); if (_screensContainer.childCount - _currentPage > _topItem) ChildObjects[CurrentPage + _topItem].SetActive(false);
} }
//Function for switching screens with buttons //Function for switching screens with buttons
public void NextScreen() public void NextScreen()
{ {
@ -361,6 +364,7 @@ namespace UnityEngine.UI.Extensions
if (PrevButton) { if (PrevButton) {
PrevButton.GetComponent<Button>().interactable = targetScreen > 0; PrevButton.GetComponent<Button>().interactable = targetScreen > 0;
} }
if (NextButton) { if (NextButton) {
NextButton.GetComponent<Button>().interactable = targetScreen < _screensContainer.transform.childCount - 1; NextButton.GetComponent<Button>().interactable = targetScreen < _screensContainer.transform.childCount - 1;
} }
@ -376,19 +380,23 @@ namespace UnityEngine.UI.Extensions
{ {
StartingScreen = childCount - 1; StartingScreen = childCount - 1;
} }
if (StartingScreen < 0) if (StartingScreen < 0)
{ {
StartingScreen = 0; StartingScreen = 0;
} }
} }
if (MaskBuffer <= 0) if (MaskBuffer <= 0)
{ {
MaskBuffer = 1; MaskBuffer = 1;
} }
if (PageStep < 0) if (PageStep < 0)
{ {
PageStep = 0; PageStep = 0;
} }
if (PageStep > 8) if (PageStep > 8)
{ {
PageStep = 9; PageStep = 9;
@ -443,5 +451,5 @@ namespace UnityEngine.UI.Extensions
} }
#endregion #endregion
} }
} }