YanCheng_Metrology/Assets/Scripts/Project/UI/UI_Panel/UI_SelectDevicePanel.cs

83 lines
3.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class UI_SelectDevicePanel : BasePanel
{
private List<TB_Scheme> allSchemeName;
private Dictionary<int, TB_DeviceType> allDeviceTypes;
public void Init()
{
allSchemeName = GameManager.DataMgr.GetAllSchemeName();
allDeviceTypes = GameManager.DataMgr.GetAllDeviceTypes();
CreatTitleAndBtnItem();
}
void CreatTitleAndBtnItem()
{
var onlyDeviceHashSet = new HashSet<int>();
foreach (var scheme in allSchemeName)
{
if (!onlyDeviceHashSet.Contains(scheme.deviceTypeId)) //没有这个设备类型ID就添加唯一标识
{
onlyDeviceHashSet.Add(scheme.deviceTypeId);
var titleItem =
GameManager.ResourcesMgr.Load<GameObject>(Const.UI_Item + "UI_SelectDevicePanel/titleItem");
titleItem.transform.parent = GetControl<ScrollRect>("DeviceScrollView").content;
if (allDeviceTypes.TryGetValue(scheme.deviceTypeId, out TB_DeviceType deviceNameItem))
{
titleItem.GetComponentInChildren<TextMeshProUGUI>().text = deviceNameItem.deviceTypeName;
var currentIdList = allSchemeName.FindAll(s => s.deviceTypeId == scheme.deviceTypeId);
//遍历每个当前设备类型,并生成按钮
currentIdList.ForEach(x =>
{
var deviceItemBtn =
GameManager.ResourcesMgr.Load<GameObject>(Const.UI_Item +
"UI_SelectDevicePanel/deviceItemBtn");
//按钮的父节点是标题的父节点content
deviceItemBtn.transform.parent = titleItem.transform.parent;
deviceItemBtn.GetComponentInChildren<TextMeshProUGUI>().text = x.schemeName;
var deviceBtnAComponent= deviceItemBtn.GetComponent<Button>();
AddEventForDeviceBtn(deviceBtnAComponent,x.deviceMap);
});
}
}
}
}
private void AddEventForDeviceBtn(UIBehaviour deviceBtnAComponent,string mapName)
{
GameManager.UIMgr.AddEventTriggerListener(deviceBtnAComponent,EventTriggerType.PointerEnter,
_ =>
{
Debug.LogError(deviceBtnAComponent.GetComponentInChildren<TextMeshProUGUI>().text);
deviceBtnAComponent.transform.DOScale(1.5f, 0.2f);
this.GetControl<Image>("DeviceShowImg").sprite =
GameManager.ResourcesMgr.Load<Sprite>(Const.UI_SelectDevicePanel + mapName);
this.GetControl<Image>("DeviceShowImg").color = new Color(1, 1, 1, 1);
});
GameManager.UIMgr.AddEventTriggerListener(deviceBtnAComponent,EventTriggerType.PointerExit,
_ =>
{
deviceBtnAComponent.transform.DOScale(1f, 0.2f);
this.GetControl<Image>("DeviceShowImg").color = new Color(1, 1, 1, 0);
});
}
[Button]
public void Test1()
{
foreach (var VARIABLE in allSchemeName)
{
Debug.LogError(VARIABLE.deviceMap+VARIABLE.schemeName);
}
Debug.LogError(allDeviceTypes.Count);
}
}