游戏中常见的弹窗逻辑,按顺序依次弹出,避免同时乱序弹出。。。
 
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 | /// <summary>
/// 弹窗对象基类
/// </summary>
public abstract class PopupItem
{
    public bool IsPopupIng { get; set; }
    
    public int Priority { get; set; }
    
    public abstract void DoPopup();
}
 | 
 
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
 | /// <summary>
/// 弹窗对象的显示优先级
/// </summary>
public enum PopupPriority
{
    RoleLevelUp,
    FunctionUnlock,
    Guide,
    PopWindow,
}
 | 
 
弹出对象的存放
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 | public partial class Model_Home
{
    private List<PopupItem> popupItemList = new();
    private PopupItem curPopupItem;
    
    /// <summary>
    /// 添加一个弹窗到列表中
    /// </summary>
    public void AddPopupItem(PopupItem item)
    {
        popupItemList.Add(item);
        popupItemList.Sort(PopupItemCompare);
    }
    public void UpdatePopupItems()
    {
        if (curPopupItem == null && popupItemList.Count > 0)
        {
            curPopupItem = popupItemList[0];
            curPopupItem.IsPopupIng = true;
            curPopupItem.DoPopup();
        }
    }
    /// <summary>
    /// 当一个弹窗关闭时,主动调用,从而触发下一个弹窗
    /// </summary>
    public void ApplyNextPopup()
    {
        popupItemList.Remove(curPopupItem);
        curPopupItem = null;
    }
    private int PopupItemCompare(PopupItem x, PopupItem y)
    {
        if (x.IsPopupIng)
        {
            return -1;
        }
        if (y.IsPopupIng)
        {
            return -1;
        }
        return x.Priority.CompareTo(y.Priority);
    }
}
 | 
 
把弹出对象加到队列中,需要在主界面中进行更新
| 1
2
3
4
5
6
7
8
9
 | public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
    base.OnUpdate(elapseSeconds, realElapseSeconds);
    //Popup弹窗更新
    if (!isCovered && !HotfixGameUtility.UI.HasUIForm(FairyGUIFormID.PopWindowForm))
    {
        _homeModel.UpdatePopupItems();
    }
}
 | 
 
以功能解锁弹窗为例
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 | /// <summary>
/// 功能解锁弹窗界面
/// </summary>
public class FunctionUnlockPopup : PopupItem
{
    private int functionId;
    
    public static void Create(int functionId)
    {
        var popup = new FunctionUnlockPopup(functionId);
        var model = MVC.Ins.FindModel<Model_Home>();
        model.AddPopupItem(popup);
    }
    public FunctionUnlockPopup(int _functionId)
    {
        functionId = _functionId;
        Priority = (int)PopupPriority.FunctionUnlock;
    }
    
    public override void DoPopup()
    {
        FunctionUnlockForm.OpenForm(functionId);
    }
}
 | 
 
功能解锁弹窗界面如下,注意的是,弹窗界面关闭时要调用 ApplyNextPopup 从而触发下一个弹窗
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 | public class FunctionUnlockForm : FairyGUIFormBase
{
    private UI_FunctionUnlockForm view;
    
    public FunctionUnlockForm(object formLogic) : base(formLogic)
    {
        view = new UI_FunctionUnlockForm(FormRoot);
    }
    
    public static void OpenForm(int functionId)
    {
        HotfixGameUtility.UI.FireOpenFormEvent(null, FairyGUIFormID.FunctionUnlockForm, functionId.ToString());
    }
    
    private void CloseSelf()
    {
        HotfixGameUtility.UI.FireCloseFormEvent(this, FairyGUIFormID.FunctionUnlockForm, null);
    }
    public override void OnInit(object userData)
    {
        base.OnInit(userData);
        
        view.m_closeBtn.onClick.Set(CloseSelf);
    }
    public override void OnOpen(object userData)
    {
        base.OnOpen(userData);
        var functionIdStr = (userData as UserFairyGUIData)?.UserData as string;
        int functionIdInt = int.Parse(functionIdStr);
        var config = DataCenter.Ins.FunctionUnlock.GetConfigByID(functionIdInt);
        view.m_function.SetSelectedIndex(config.UIState);
    }
    public override void OnClose(object userData)
    {
        base.OnClose(userData);
        
        MVC.Ins.FindModel<Model_Home>().ApplyNextPopup();
    }
}
 |