一个什么都没考虑只实现了翻页的翻页教程
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ScrollPageTool : MonoBehaviour
{
public Button lastButton;
public Button nextButton;
public Text pageNumText;
public int pageNum;
public int pageSize;
public GameObject contentPrefab;
int page = 0;
private void Start()
{
InitPage();
pageNumText.text = (page + 1).ToString();
lastButton.onClick.AddListener(delegate { PageMove(-1); });
nextButton.onClick.AddListener(delegate { PageMove(1); });
}
void Update()
{
}
void InitPage()
{
for (int i = 0; i < pageNum; i++)
{
GameObject pageContent = Instantiate(contentPrefab);
pageContent.transform.position = Vector3.zero;
pageContent.transform.parent = transform;
if (i == 0)
pageContent.SetActive(true);
else
pageContent.SetActive(false);
}
}
void PageMove(int num)
{
if (page >= 0 && page < pageNum)
{
if (num == 1)
{
if (page < pageNum - 1)
{
transform.GetChild(page).gameObject.SetActive(false);
transform.GetChild(page + 1).gameObject.SetActive(true);
page++;
pageNumText.text = (page + 1).ToString();
}
}
else if (num == -1)
{
if (page > 0)
{
transform.GetChild(page).gameObject.SetActive(false);
transform.GetChild(page - 1).gameObject.SetActive(true);
page--;
pageNumText.text = (page + 1).ToString();
}
}
}
}
}
|