Added Pagination Manager

Resolves #158

Thanks @BroganKing
release
Simon Jackson 2017-08-09 19:38:29 +01:00
parent b4a2e81aae
commit d6cfb41bf5
4 changed files with 4470 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a457ff2aa045adc4896083aaa023a67b
timeCreated: 1502303044
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,130 @@
using System.Collections.Generic;
/// Credit Brogan King (@BroganKing)
/// Original Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/158/pagination-script
using System.Linq;
namespace UnityEngine.UI.Extensions
{
public class PaginationManager : ToggleGroup
{
private List<Toggle> m_PaginationChildren;
[SerializeField]
private ScrollSnapBase scrollSnap;
private bool isAClick;
public int CurrentPage
{
get { return scrollSnap.CurrentPage; }
}
protected PaginationManager()
{ }
// Use this for initialization
protected override void Start()
{
base.Start();
if (scrollSnap == null)
{
Debug.LogError("A ScrollSnap script must be attached");
return;
}
// dont want the scroll snap pagination
if (scrollSnap.Pagination)
scrollSnap.Pagination = null;
// set scroll snap listeners
scrollSnap.OnSelectionPageChangedEvent.AddListener(SetToggleGraphics);
scrollSnap.OnSelectionChangeEndEvent.AddListener(OnPageChangeEnd);
// add selectables to list
m_PaginationChildren = GetComponentsInChildren<Toggle>().ToList<Toggle>();
for (int i = 0; i < m_PaginationChildren.Count; i++)
{
m_PaginationChildren[i].onValueChanged.AddListener(ToggleClick);
m_PaginationChildren[i].group = this;
m_PaginationChildren[i].isOn = false;
}
// set toggles on start
SetToggleGraphics(CurrentPage);
// warn user that they have uneven amount of pagination toggles to page count
if (m_PaginationChildren.Count != scrollSnap._scroll_rect.content.childCount)
Debug.LogWarning("Uneven pagination icon to page count");
}
/// <summary>
/// Calling from other scripts if you need to change screens progamatically
/// </summary>
/// <param name="pageNo"></param>
public void GoToScreen(int pageNo)
{
scrollSnap.GoToScreen(pageNo);
}
/// <summary>
/// Calls GoToScreen() based on the index of toggle that was pressed
/// </summary>
/// <param name="target"></param>
private void ToggleClick(Toggle target)
{
if (!target.isOn)
{
isAClick = true;
GoToScreen(m_PaginationChildren.IndexOf(target));
}
}
private void ToggleClick(bool toggle)
{
if (toggle)
{
for (int i = 0; i < m_PaginationChildren.Count; i++)
{
if (m_PaginationChildren[i].isOn)
{
GoToScreen(i);
break;
}
}
}
}
/// <summary>
/// Calls GoToScreen() based on the index of toggle that was pressed
/// </summary>
/// <param name="target"></param>
private void ToggleClick(int target)
{
isAClick = true;
GoToScreen(target);
}
private void SetToggleGraphics(int pageNo)
{
if (!isAClick)
{
m_PaginationChildren[pageNo].isOn = true;
//for (int i = 0; i < m_PaginationChildren.Count; i++)
//{
// m_PaginationChildren[i].isOn = pageNo == i ? true : false;
//}
}
}
private void OnPageChangeEnd(int pageNo)
{
isAClick = false;
}
}
}

View File

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