VirtualFramework/Assets/Scripts/UI/UITextQuestion.cs
2024-12-14 18:27:59 +08:00

84 lines
2.7 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
using TMPro;
namespace QFramework.Example
{
public class UITextQuestionData : UIPanelData
{
public string title;
public List<string> options = new List<string>();
public List<string> answers = new List<string>();
public List<string> btns = new List<string>();
public float waitCloseTime = -1;
public bool showAnswer = false;
}
public partial class UITextQuestion : UIPanel
{
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UITextQuestionData ?? new UITextQuestionData();
// please add init code here
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UITextQuestionData ?? new UITextQuestionData();
Title.text = mData.title;
OptionContent.transform.RemoveAllChildren();
for (int i = 0; i < mData.options.Count; i++)
{
var item = mData.options[i];
GameObject obj = GameObject.Instantiate(OptionPrefab.gameObject, OptionContent.transform);
obj.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = item;
obj.name = (i + 1).ToString();
}
BtnContent.RemoveAllChildren();
foreach (var item in mData.btns)
{
GameObject obj = GameObject.Instantiate(BtnPrefab.gameObject, BtnContent);
var label = obj.transform.Find("Label").GetComponent<TextMeshProUGUI>();
label.text = item;
obj.GetComponent<Button>().onClick.AddListener(() =>
{
if (mData.showAnswer)
{
for (int i = 0; i < OptionContent.transform.childCount; i++)
{
Transform trans = OptionContent.transform.GetChild(i);
if (mData.answers.Contains(trans.name))
{
trans.Find("Label").GetComponent<TextMeshProUGUI>().color = Color.green;
}
}
}
if (mData.waitCloseTime != -1)
{
ActionKit.Delay(mData.waitCloseTime, () => Hide()).Start(this);
return;
}
Hide();
});
}
}
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
}
}
}