fix:代码提交
This commit is contained in:
158
Assets/Scripts/Ui/PnlMain.cs
Normal file
158
Assets/Scripts/Ui/PnlMain.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
using DG.Tweening;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class PnlMain : ActionBase
|
||||
{
|
||||
private const int ButtonCount = 5;
|
||||
private List<Button> buttons = new List<Button>();
|
||||
private List<Action> clickActions = new List<Action>();
|
||||
|
||||
// 定义一个结构体来存储动态参数
|
||||
private struct PageActionParams
|
||||
{
|
||||
public List<PageType> closePageTypes; // 存储需要关闭的所有页面类型
|
||||
public List<PageType> openPageType; // 存储需要打开的所有页面类型
|
||||
public PageType actionPageType;
|
||||
public float openDelay;
|
||||
public string buttonName; // 存储按钮名称,避免复杂的条件判断
|
||||
}
|
||||
|
||||
private List<PageActionParams> pageActionParamsList = new List<PageActionParams>
|
||||
{
|
||||
new PageActionParams
|
||||
{
|
||||
closePageTypes = new List<PageType> { PageType.Icon01,PageType.Icon02, PageType.Icon03, PageType.Icon04 },
|
||||
openPageType = new List<PageType> { PageType.Icon01,PageType.Icon02, PageType.Icon03, PageType.Icon04 },
|
||||
actionPageType = PageType.Camera1,
|
||||
openDelay = 1f,
|
||||
buttonName = "all"
|
||||
},
|
||||
new PageActionParams
|
||||
{
|
||||
closePageTypes = new List<PageType> { PageType.Icon01,PageType.Icon02, PageType.Icon03, PageType.Icon04 },
|
||||
openPageType = new List<PageType> { PageType.Icon01 },
|
||||
actionPageType = PageType.Camera1,
|
||||
openDelay = 1f,
|
||||
buttonName = "gnss"
|
||||
},
|
||||
new PageActionParams
|
||||
{
|
||||
closePageTypes = new List<PageType> { PageType.Icon01,PageType.Icon02, PageType.Icon03, PageType.Icon04 },
|
||||
openPageType = new List<PageType> { PageType.Icon02 },
|
||||
actionPageType = PageType.Camera2,
|
||||
openDelay = 1f,
|
||||
buttonName = "水位"
|
||||
},
|
||||
new PageActionParams
|
||||
{
|
||||
closePageTypes = new List<PageType> { PageType.Icon01, PageType.Icon02,PageType.Icon03, PageType.Icon04 },
|
||||
openPageType = new List<PageType> { PageType.Icon03 },
|
||||
actionPageType = PageType.Camera1,
|
||||
openDelay = 1f,
|
||||
buttonName = "雨量监测"
|
||||
},
|
||||
new PageActionParams
|
||||
{
|
||||
closePageTypes = new List<PageType> { PageType.Icon01, PageType.Icon02, PageType.Icon03,PageType.Icon04 },
|
||||
openPageType = new List<PageType> { PageType.Icon04 },
|
||||
actionPageType = PageType.Camera1,
|
||||
openDelay = 1f,
|
||||
buttonName = "渗压计监测"
|
||||
}
|
||||
};
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
for (int i = 1; i <= ButtonCount; i++)
|
||||
{
|
||||
string buttonName = $"btnPage{i}";
|
||||
Button button = transform.Find(buttonName).GetComponent<Button>();
|
||||
// Debug.Log($"找到按钮{buttonName}");
|
||||
buttons.Add(button);
|
||||
|
||||
int index = i - 1;
|
||||
button.onClick.AddListener(() => OnButtonClick(index));
|
||||
}
|
||||
|
||||
for (int i = 0; i < ButtonCount; i++)
|
||||
{
|
||||
int index = i;
|
||||
clickActions.Add(() =>
|
||||
{
|
||||
// Debug.Log($"我点击了按钮{pageActionParamsList[index].buttonName}");
|
||||
OnButtonClickCommon(index);
|
||||
});
|
||||
}
|
||||
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
void OnButtonClick(int index)
|
||||
{
|
||||
for (int i = 0; i < buttons.Count; i++)
|
||||
{
|
||||
buttons[i].interactable = i != index;
|
||||
}
|
||||
|
||||
if (index < clickActions.Count)
|
||||
{
|
||||
clickActions[index].Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
void OnButtonClickCommon(int index)
|
||||
{
|
||||
if (index < pageActionParamsList.Count)
|
||||
{
|
||||
var paramsData = pageActionParamsList[index];
|
||||
|
||||
// 关闭所有需要关闭的页面
|
||||
if (paramsData.closePageTypes != null && paramsData.closePageTypes.Count > 0)
|
||||
{
|
||||
foreach (var pageType in paramsData.closePageTypes)
|
||||
{
|
||||
AppCache.ClosePage(pageType);
|
||||
}
|
||||
}
|
||||
|
||||
// 打开所有需要打开的页面
|
||||
if (paramsData.openPageType != null && paramsData.openPageType.Count > 0)
|
||||
{
|
||||
foreach (var pageType in paramsData.openPageType)
|
||||
{
|
||||
AppCache.OpenPageDelay(pageType, paramsData.openDelay);
|
||||
}
|
||||
}
|
||||
|
||||
if (paramsData.actionPageType != default(PageType))
|
||||
{
|
||||
ActionCenter.Instance.DoAction(GameEvent.OpenPage, paramsData.actionPageType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void TriggerButtonClick(string buttonName)
|
||||
{
|
||||
for (int i = 0; i < pageActionParamsList.Count; i++)
|
||||
{
|
||||
if (pageActionParamsList[i].buttonName == buttonName)
|
||||
{
|
||||
OnButtonClick(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void RegisterAction()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void RemoveAction()
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user