You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
5.4 KiB
176 lines
5.4 KiB
using AX.NetworkSystem; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
|
|
public class PagesController : MonoBehaviour |
|
{ |
|
public static PagesController Instance; |
|
public Button pre; |
|
public Button next; |
|
public GameObject prefab; |
|
public ToggleGroup togGroup; |
|
private int pageCount = 1; //一共的页数 |
|
private int pageIndex = 1; //当前显示页数 |
|
private QueryOptions queryOptions = new QueryOptions(); |
|
|
|
private void Awake() |
|
{ |
|
Instance = this; |
|
//pre.onClick.AddListener(PrePage);//单机版注释 |
|
//next.onClick.AddListener(NextPage);//单机版注释 |
|
pre.gameObject.SetActive(false);//非单机版解开注释 |
|
next.gameObject.SetActive(false);//非单机版解开注释 |
|
} |
|
// Use this for initialization |
|
void Start() |
|
{ |
|
ApplyForInitPage(); |
|
} |
|
public void ApplyForInitPage() |
|
{ |
|
pageIndex = 1; |
|
//1、向服务端申请获取一共的页数,返回调用GetPageCount() |
|
queryOptions.PageNumber = 1; //申请第一页数据 |
|
queryOptions.PageSize = 5; //每页显示5条 |
|
//NetworkManager.Default.SendAsync("GET_DISASTER_INFOS_REQUEST", queryOptions); //联机版 |
|
DisasterList.Instance.ShowIndexDisasters(DisasterInfosManager.Instance.GetDisasterInfos()); //单机版,获取本地灾情列表 |
|
} |
|
public void ApplyForPage(int number) |
|
{ |
|
pageIndex = number; |
|
queryOptions.PageNumber = number; //申请第一页数据 |
|
queryOptions.PageSize = 5; //每页显示5条 |
|
//NetworkManager.Default.SendAsync("GET_DISASTER_INFOS_REQUEST", queryOptions);//单机版注释 |
|
DisasterList.Instance.ShowIndexDisasters(DisasterInfosManager.Instance.GetDisasterInfos()); //单机版,获取本地灾情列表 |
|
} |
|
public void GetPageCount(int count) |
|
{ |
|
pageCount = count; |
|
for (int j = 0; j < transform.childCount; j++) |
|
{ |
|
Destroy(transform.GetChild(j).gameObject); |
|
} |
|
//生成页码 |
|
InstantiatePageIndex(); |
|
CheckPreNextButton(); |
|
} |
|
/// <summary> |
|
/// 生成页码 |
|
/// </summary> |
|
/// <param name="count"></param> |
|
private void InstantiatePageIndex() |
|
{ |
|
if (pageCount < 10) |
|
{ |
|
for (int i = 0; i < pageCount; i++) |
|
{ |
|
GameObject indexItem = Instantiate(prefab, transform); |
|
indexItem.GetComponent<PageIndex>().Index = i + 1; |
|
if (i + 1 == pageIndex) |
|
{ |
|
indexItem.GetComponent<PageIndex>().OnThisPage(); |
|
} |
|
} |
|
} |
|
else if (pageIndex - 4 > 1 && pageIndex + 4 < pageCount) |
|
{ |
|
for (int j = pageIndex - 4; j <= pageIndex + 4; j++) |
|
{ |
|
GameObject indexItem = Instantiate(prefab, transform); |
|
indexItem.GetComponent<PageIndex>().Index = j; |
|
if (j == pageIndex) |
|
{ |
|
indexItem.GetComponent<PageIndex>().OnThisPage(); |
|
} |
|
} |
|
} |
|
else if (pageIndex - 4 > 1 && pageIndex + 4 >= pageCount) |
|
{ |
|
for (int k = pageCount - 8; k <= pageCount; k++) |
|
{ |
|
GameObject indexItem = Instantiate(prefab, transform); |
|
indexItem.GetComponent<PageIndex>().Index = k; |
|
if (k == pageIndex) |
|
{ |
|
indexItem.GetComponent<PageIndex>().OnThisPage(); |
|
} |
|
} |
|
} |
|
else //if (pageIndex < 6 && pageCount>9) |
|
{ |
|
for (int l = 1; l < 10; l++) |
|
{ |
|
GameObject indexItem = Instantiate(prefab, transform); |
|
indexItem.GetComponent<PageIndex>().Index = l; |
|
if (l == pageIndex) |
|
{ |
|
indexItem.GetComponent<PageIndex>().OnThisPage(); |
|
} |
|
} |
|
} |
|
} |
|
private void CheckPreNextButton() |
|
{ |
|
if (pageIndex == 1) |
|
{ |
|
pre.interactable = false; |
|
if (pageCount == 1) |
|
{ |
|
next.interactable = false; |
|
} |
|
else |
|
{ |
|
next.interactable = true; |
|
} |
|
} |
|
else if (pageIndex == pageCount) |
|
{ |
|
next.interactable = false; |
|
if (pageCount > 1) |
|
{ |
|
pre.interactable = true; |
|
} |
|
else |
|
{ |
|
pre.interactable = false; |
|
} |
|
} |
|
else |
|
{ |
|
pre.interactable = true; |
|
next.interactable = true; |
|
} |
|
} |
|
private void PrePage() |
|
{ |
|
pageIndex -= 1; |
|
//如果页码是第一页,禁用“上一页”按钮 |
|
if (pageIndex == 1) |
|
{ |
|
pre.interactable = false; |
|
} |
|
//如果页码是倒数第二页,恢复“下一页”按钮 |
|
else if (pageIndex == pageCount - 1) |
|
{ |
|
next.interactable = true; |
|
} |
|
//向服务端申请第pageIndex页数据 |
|
ApplyForPage(pageIndex); |
|
} |
|
private void NextPage() |
|
{ |
|
pageIndex += 1; |
|
if (pageIndex == pageCount) |
|
{ |
|
next.interactable = false; |
|
} |
|
else if (pageIndex == 2) |
|
{ |
|
pre.interactable = true; |
|
} |
|
//向服务端申请第pageIndex页数据 |
|
ApplyForPage(pageIndex); |
|
} |
|
}
|
|
|