89 lines
2.6 KiB
C#
Raw Normal View History

2024-12-14 18:27:59 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System;
using System.Collections.Generic;
using TMPro;
2024-12-30 19:15:34 +08:00
using static OperationController;
2024-12-14 18:27:59 +08:00
namespace QFramework.Example
{
public class UITipWindowData : UIPanelData
{
public class ItemData
{
public string txt;
public Action OnClick;
}
public string txt;
2025-01-08 16:02:57 +08:00
public string audio;
2024-12-14 18:27:59 +08:00
public List<ItemData> btns = new List<ItemData>();
}
public partial class UITipWindow : UIPanel
{
2025-01-08 16:02:57 +08:00
ResLoader loader;
2024-12-14 18:27:59 +08:00
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UITipWindowData ?? new UITipWindowData();
// please add init code here
2025-01-09 09:43:15 +08:00
TypeEventSystem.Global.Register<OnModuleQuit>((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject);
2024-12-30 19:15:34 +08:00
}
private void OnStepChanged(StepStatusOnChange change)
{
Hide();
2024-12-14 18:27:59 +08:00
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UITipWindowData ?? new UITipWindowData();
2025-01-09 13:35:04 +08:00
TypeEventSystem.Global.Register<StepStatusOnChange>(OnStepChanged).UnRegisterWhenDisabled(gameObject);
2024-12-14 18:27:59 +08:00
Label.text = mData.txt;
BtnContent.RemoveAllChildren();
if (mData != null)
{
foreach (var item in mData.btns)
{
GameObject obj = GameObject.Instantiate(BtnPrefab.gameObject, BtnContent);
obj.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = item.txt;
2025-01-08 16:02:57 +08:00
obj.name = item.txt;
2024-12-14 18:27:59 +08:00
obj.GetComponent<Button>().onClick.AddListener(() =>
{
item.OnClick?.Invoke();
Hide();
});
}
}
2025-01-08 16:02:57 +08:00
if (string.IsNullOrEmpty(mData.audio) == false)
{
string path = Global.audioPath + mData.audio;
loader = ResLoader.Allocate();
loader.Add2Load(path.ToLocalAudioResName(), (success, res) =>
{
if (success)
{
AudioKit.PlayVoice(res.Asset.As<AudioClip>(), false);
}
});
loader.LoadAsync();
}
2024-12-14 18:27:59 +08:00
}
protected override void OnShow()
{
}
protected override void OnHide()
{
2025-01-08 16:02:57 +08:00
AudioKit.StopVoice();
2024-12-14 18:27:59 +08:00
}
protected override void OnClose()
{
2025-01-08 16:02:57 +08:00
loader.Recycle2Cache();
2024-12-14 18:27:59 +08:00
}
}
}