unity,利用堆栈实现ui面板进退

该文章描述了一个在UnityUI中使用Canvas管理多个Panel的场景。通过按钮事件,实现了在first、second、third三个Panel之间的动态切换。点击‘进入’按钮,Panel会隐藏并切换到下一个,而‘退出’按钮则返回上一个Panel。文章提供了一个具体的C#脚本示例,用于管理Panel的堆栈和状态。
摘要由CSDN通过智能技术生成

介绍

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);
	}
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忽然602

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值