// 面板栈栈元素信息
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);
}
}
面板关闭时出栈,非全屏面板强制出栈