实现一个如上图的功能(动图绿的冒泡),登陆游戏之后,依次弹出一系列活动窗口。
这里我定义了两个类
重新定义了一个队列类:
public class UQueue: Queue<UIItem>
{
public void Show()
{
if (this.Count > 0)
{
Dequeue().Load();
}
}
}
定义一个UI的成员类,每一个窗口即为一个UIItem对象:
public class UIItem
{
private string name;
private GameObject uiItem;
private UQueue uQueue;
public UIItem(string name, UQueue uQueue)
{
this.name = name;
this.uQueue = uQueue;
}
/// <summary>
/// ui的加载方法
/// </summary>
public void Load()
{
//这里自己定义加载方法,绑定DestroyMe为关闭窗口方法
//这里仅为示例
//实例化活动界面
GameObject obj = Resources.Load<GameObject>(name);
uiItem= Object.Instantiate(obj, GameObject.Find("Canvas").transform);
Debug.Log("在canvas上实例化了一个" + name);
//绑定关闭方法
uiItem.transform.Find("Button").GetComponent<Button>().onClick.AddListener(DestroyMe);
// obj.transform.Find("Button").GetComponent<Button>().onClick.AddListener(()=> { Object.Destroy(uiItem); uQueue.Show(); });
Debug.Log("在" + name + "上绑定了一个关闭方法DestroyMe()");
}
public void DestroyMe()
{
UnityEngine.Object.Destroy(uiItem);
Debug.Log("销毁了"+uiItem.name);
//在销毁方法中调用队列的show方法,进行继续展示下一界面
uQueue.Show();
}
}
展示窗口时只需要将需要展示的对象存入重定义的队列,调用show方法即可:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestDemo : MonoBehaviour {
// Use this for initialization
private UQueue uIItems;
void Start ()
{
uIItems = new UQueue();
for (int i = 1; i < 4; i++)
{
UIItem uIItem = new UIItem("Panel"+i,uIItems);
uIItems.Enqueue(uIItem);
}
uIItems.Show();
}
}
效果展示: