介绍
unity ui上,同一个canvas中,上下排列panel(first、second、third)。两个面板都有两个按钮(进入、退出),第三个面板只有“返回”按钮。点击first的“进入”,first面板会隐身,它会进入second面板。second面板如果点击“进入”,那么second面板会隐身并进入third面板。second面板如果点击“退出”,那么second面板会隐身,并返回first面板。
方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class aab : MonoBehaviour
{
public GameObject firstPanel;
public GameObject secondPanel;
public GameObject thirdPanel;
public Button firstout;
public Button firstToSecondBtn;
public Button secondToFirstBtn;
public Button secondToThirdBtn;
public Button thirdToSecondBtn;
private Stack<GameObject> panelStack;
void Start()
{
panelStack = new Stack<GameObject>();
panelStack.Push(firstPanel);
firstPanel.SetActive(true);
// 添加按钮点击事件
firstout.onClick.AddListener(Firstout);
firstToSecondBtn.onClick.AddListener(FirstToSecond);
secondToFirstBtn.onClick.AddListener(SecondToFirst);
secondToThirdBtn.onClick.AddListener(SecondToThird);
thirdToSecondBtn.onClick.AddListener(ThirdToSecond);
}
// 关闭第一个
void Firstout()
{
firstPanel.SetActive(false);
}
// 跳转到第二个面板
void FirstToSecond()
{
firstPanel.SetActive(false);
secondPanel.SetActive(true);
panelStack.Push(secondPanel);
}
// 返回第一个面板
void SecondToFirst()
{
GameObject topPanel = panelStack.Pop();
topPanel.SetActive(false);
firstPanel.SetActive(true);
}
// 跳转到第三个面板
void SecondToThird()
{
secondPanel.SetActive(false);
thirdPanel.SetActive(true);
panelStack.Push(thirdPanel);
}
// 返回第二个面板
void ThirdToSecond()
{
GameObject topPanel = panelStack.Pop();
topPanel.SetActive(false);
secondPanel.SetActive(true);
}
}