2025-01-09 09:43:15 +08:00

72 lines
2.1 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
using TMPro;
using System.Text.RegularExpressions;
namespace QFramework.Example
{
public class UITextTipData : UIPanelData
{
public string text;
public string audio;
public List<string> btns;
}
public partial class UITextTip : UIPanel
{
ResLoader loader;
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UITextTipData ?? new UITextTipData();
TypeEventSystem.Global.Register<OnModuleQuit>((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject);
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UITextTipData ?? new UITextTipData();
Des.text = Regex.Replace(mData.text, @"\\n", "\n");
BtnContent.RemoveAllChildren();
foreach (var item in mData.btns)
{
GameObject btn = GameObject.Instantiate(Btn.gameObject, BtnContent);
btn.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = item;
btn.name = item;
btn.GetComponent<Button>().onClick.AddListener(() =>
{
Hide();
});
}
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();
}
}
protected override void OnShow()
{
}
protected override void OnHide()
{
AudioKit.StopVoice();
}
protected override void OnClose()
{
loader.Recycle2Cache();
}
}
}