Unity面板栈管理

    // 面板栈栈元素信息
    public struct PanelStackInfo
    {
        public int panelType;        // 面板类型
        public CustomParam param;    // 面板开启传参
    }

定义一个栈元素结构体,存储面板信息

    List<PanelStackInfo> _panelStack = new List<PanelStackInfo>();

定义栈(列表)

    /// <summary>
    /// 返回面板栈栈顶信息
    /// </summary>
    public PanelStackInfo GetTopPanelStackInfo()
    {
        var count = _panelStack.Count;
        return count == 0 ? default : _panelStack[count - 1];
    }

    /// <summary>
    /// 返回面板栈栈元素数量
    /// </summary>
    public int PanelStackCount { get { return _panelStack.Count; } }

    /// <summary>
    /// 返回面板栈信息序号
    /// </summary>
    /// <param name="panelType"></param>
    int GetPanelStackInfoIndex(int panelType)
    {
        var maxIndex = _panelStack.Count - 1;
        for (var i = maxIndex; i >= 0; --i)
        {
            if (_panelStack[i].panelType == panelType)
            {
                return i;
            }
        }

        return -1;
    }

    /// <summary>
    /// 清除面板栈
    /// </summary>
    void ClearPanelStack()
    {
        _panelStack.Clear();
    }

    void PushPanelStack(int panelType, CustomParam param)
    {
        var newStackInfo = new PanelStackInfo() { panelType = panelType, param = param };
        _panelStack.Add(newStackInfo);
    }

    void PopPanelStack(int panelType)
    {
        var index = GetPanelStackInfoIndex(panelType);
        if (index != -1)
        {
            _panelStack.RemoveAt(index);
        }
    }

栈列表的基本操作

    // 是否保留面板栈信息
    bool _isRetainPanelStack;

定义bool变量标记是否保留面板栈信息,在关闭指定面板组时先置为true,全部关闭时置为false

    public void OnPanelStartOpen(int panelType, CustomParam param)
    {
        // 面板开启时压栈
        if (_panelStackPanelList.Contains(panelType))
        {
            // 新开启面板置顶
            PopPanelStack(panelType);
            PushPanelStack(panelType, param);
        }
    }

面板打开时将面板压栈

    public void OnPanelStartClose(int panelType)
    {
        var isExclusive = IsPanelExclusive(panelType);    // 读配置

        // 面板关闭时出栈,非独占(全屏)面板强制出栈
        if ((!_isRetainPanelStack || !isExclusive) &&                     
              _panelStackPanelList.Contains(panelType))
        {
            PopPanelStack(panelType);
        }
    }

面板关闭时出栈,非全屏面板强制出栈

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值