152 lines
5.2 KiB
C#
152 lines
5.2 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
using QFramework;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using TMPro;
|
||
|
|
|
||
|
|
namespace QFramework.Example
|
||
|
|
{
|
||
|
|
public class UIInputData : UIPanelData
|
||
|
|
{
|
||
|
|
public class InputData
|
||
|
|
{
|
||
|
|
public string name;
|
||
|
|
public string var;
|
||
|
|
public string answer;
|
||
|
|
}
|
||
|
|
public class BtnData
|
||
|
|
{
|
||
|
|
public string name;
|
||
|
|
public string clickEvent;
|
||
|
|
public bool checkAnswer;
|
||
|
|
public string wrongLabel;
|
||
|
|
public string wrongEvent;
|
||
|
|
public int wrongCount;
|
||
|
|
public string scoreStepName;
|
||
|
|
public float rightScore = 0;
|
||
|
|
public float wrongScore = 0;
|
||
|
|
public float autoHide = -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
public string title;
|
||
|
|
public List<InputData> inputs = new List<InputData>();
|
||
|
|
public List<BtnData> btns = new List<BtnData>();
|
||
|
|
public string finishedEvent;
|
||
|
|
|
||
|
|
}
|
||
|
|
public partial class UIInput : UIPanel
|
||
|
|
{
|
||
|
|
int curCount = 0;
|
||
|
|
protected override void OnInit(IUIData uiData = null)
|
||
|
|
{
|
||
|
|
// please add init code here
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void OnOpen(IUIData uiData = null)
|
||
|
|
{
|
||
|
|
mData = uiData as UIInputData ?? new UIInputData();
|
||
|
|
curCount = 0;
|
||
|
|
Title.text = mData.title;
|
||
|
|
InputContent.RemoveAllChildren();
|
||
|
|
BtnContent.RemoveAllChildren();
|
||
|
|
foreach (var item in mData.inputs)
|
||
|
|
{
|
||
|
|
GameObject input = GameObject.Instantiate(InputItem.gameObject, InputContent);
|
||
|
|
input.transform.Find("Name").GetComponent<TextMeshProUGUI>().text = item.name;
|
||
|
|
var inputField = input.transform.Find("Input").GetComponent<TMP_InputField>();
|
||
|
|
inputField.onEndEdit.AddListener(value =>
|
||
|
|
{
|
||
|
|
float v = 0;
|
||
|
|
float.TryParse(value, out v);
|
||
|
|
VarController.Instance.Set(item.var, v);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach (var item in mData.btns)
|
||
|
|
{
|
||
|
|
GameObject btn = GameObject.Instantiate(BtnItem.gameObject, BtnContent);
|
||
|
|
btn.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = item.name;
|
||
|
|
btn.name = item.name;
|
||
|
|
btn.GetComponent<Button>().onClick.AddListener(() =>
|
||
|
|
{
|
||
|
|
if (item.checkAnswer)
|
||
|
|
{
|
||
|
|
if (Check())
|
||
|
|
{
|
||
|
|
if (string.IsNullOrEmpty(item.scoreStepName) == false)
|
||
|
|
{
|
||
|
|
ScoreController.Instance.Add(item.scoreStepName, item.rightScore);
|
||
|
|
}
|
||
|
|
Hide();
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
if (curCount >= item.wrongCount)
|
||
|
|
{
|
||
|
|
Hide();
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
if (string.IsNullOrEmpty(item.scoreStepName) == false)
|
||
|
|
{
|
||
|
|
ScoreController.Instance.Add(item.scoreStepName, item.wrongScore);
|
||
|
|
}
|
||
|
|
curCount++;
|
||
|
|
var data = new UIResultTipData();
|
||
|
|
data.label = item.wrongLabel;
|
||
|
|
data.isRight = false;
|
||
|
|
data.callback = () =>
|
||
|
|
{
|
||
|
|
if (string.IsNullOrEmpty(item.wrongEvent) == false)
|
||
|
|
{
|
||
|
|
StringEventSystem.Global.Send(item.wrongEvent);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
data.autoHideTime = item.autoHide;
|
||
|
|
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().Start(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Hide();
|
||
|
|
if (string.IsNullOrEmpty(item.clickEvent) == false)
|
||
|
|
{
|
||
|
|
StringEventSystem.Global.Send(item.clickEvent);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
public bool Check()
|
||
|
|
{
|
||
|
|
foreach (var item in mData.inputs)
|
||
|
|
{
|
||
|
|
if (VarController.Instance.Get(item.var).ToString() != item.answer)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void OnShow()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void OnHide()
|
||
|
|
{
|
||
|
|
if (string.IsNullOrEmpty(mData.finishedEvent) == false)
|
||
|
|
{
|
||
|
|
StringEventSystem.Global.Send(mData.finishedEvent);
|
||
|
|
}
|
||
|
|
mData = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void OnClose()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|