VirtualFramework/Assets/Scripts/UI/UIInput_AnHuiBaoShi.cs

354 lines
10 KiB
C#
Raw Normal View History

2025-02-06 17:23:24 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
2025-02-07 18:03:05 +08:00
using System.ComponentModel;
using System.Linq;
2025-02-06 17:23:24 +08:00
namespace QFramework.Example
{
2025-04-24 10:21:49 +08:00
public class UIInput_AnHuiBaoShiData : UIPanelData
{
}
public partial class UIInput_AnHuiBaoShi : UIPanel
2025-02-07 18:03:05 +08:00
{
2025-06-03 09:19:44 +08:00
public int CurrentInputIndex;//当前索引次数
[Header("所有的图片")]
2025-02-07 18:03:05 +08:00
public Image[] images;
2025-06-03 09:19:44 +08:00
[Header("设置最大目标值")]
public string[] targetMax; // 设置最大目标值
[Header("设置最小目标值")]
public string[] targetMin; // 设置最小目标值
2025-02-07 18:03:05 +08:00
2025-06-03 09:19:44 +08:00
[Header("最大目标值错误次数")]
public int errowIntMax = 0;//最大目标值错误次数
[Header("最小目标值错误次数")]
public int errowIntMin = 0;//最小目标值错误次数
[Header("错误弹窗")]
public GameObject errowTip;//错误弹窗
public Button errowTipClickBtn;//错误弹窗,确认按钮
2025-02-07 18:03:05 +08:00
2025-04-24 10:21:49 +08:00
private string tMP_InputField1Value = "1.770";
private string tMP_InputField2Value = "1.762";
2025-02-07 18:03:05 +08:00
2025-03-26 10:16:17 +08:00
private string tMP_InputField3Value = "0.008";
2025-02-07 18:03:05 +08:00
public int tMP_InputField1ErrowIndex = 0;
public int tMP_InputField2ErrowIndex = 0;
public int tMP_InputField3ErrowIndex = 0;
2025-06-03 09:19:44 +08:00
public GameObject ZheSheInput;//输入三个数值的输入框
2025-02-07 18:03:05 +08:00
2025-04-24 10:21:49 +08:00
public void OnReSet()
{
InputFieldmax.gameObject.SetActive(true);
InputFieldmin.gameObject.SetActive(true);
images[0].gameObject.SetActive(true);
images[1].gameObject.SetActive(false);
images[2].gameObject.SetActive(false);
images[3].gameObject.SetActive(false);
images[4].gameObject.SetActive(false);
tMP_InputField1.gameObject.SetActive(false);
tMP_InputField2.gameObject.SetActive(false);
tMP_InputField3.gameObject.SetActive(false);
errowTip.gameObject.SetActive(false);
CurrentInputIndex = 0;
errowIntMax = 0;
errowIntMin = 0;
}
2025-02-07 18:03:05 +08:00
protected override void OnInit(IUIData uiData = null)
2025-04-24 10:21:49 +08:00
{
mData = uiData as UIInput_AnHuiBaoShiData ?? new UIInput_AnHuiBaoShiData();
OnReSet();
OnOpen();
2025-06-03 09:19:44 +08:00
TypeEventSystem.Global.Register<OnModuleQuit>(OnModuleQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
2025-04-24 10:21:49 +08:00
}
2025-06-03 09:19:44 +08:00
public void OnModuleQuit(OnModuleQuit arg)
{
Hide();
}
2025-04-24 10:21:49 +08:00
protected override void OnOpen(IUIData uiData = null)
2025-02-06 17:23:24 +08:00
{
2025-02-07 18:03:05 +08:00
transform.SetAsLastSibling();
2025-02-07 18:03:05 +08:00
ZheSheInput.gameObject.SetActive(false);
CurrentInputIndex = 0;
errowIntMax = 0;
errowIntMin = 0;
2025-04-24 10:21:49 +08:00
2025-02-07 18:03:05 +08:00
InputFieldmax.onEndEdit.RemoveAllListeners();
InputFieldmin.onEndEdit.RemoveAllListeners();
2025-04-24 10:21:49 +08:00
2025-02-07 18:03:05 +08:00
InputFieldmax.onEndEdit.AddListener(CheckInputMax);
InputFieldmin.onEndEdit.AddListener(CheckInputMin);
tMP_InputField1.onEndEdit.RemoveAllListeners();
tMP_InputField2.onEndEdit.RemoveAllListeners();
tMP_InputField3.onEndEdit.RemoveAllListeners();
tMP_InputField1.onEndEdit.AddListener(CheacktMP_InputField1String);
tMP_InputField2.onEndEdit.AddListener(CheacktMP_InputField2String);
tMP_InputField3.onEndEdit.AddListener(CheacktMP_InputField3String);
2025-05-19 11:57:37 +08:00
tMP_InputField1.gameObject.SetActive(true);
tMP_InputField2.gameObject.SetActive(true);
tMP_InputField3.gameObject.SetActive(true);
2025-02-07 18:03:05 +08:00
}
public void CheackAllInputField()
{
2025-04-24 10:21:49 +08:00
if (tMP_InputField1.text == tMP_InputField1Value && tMP_InputField2.text == tMP_InputField2Value &&
tMP_InputField3.text == tMP_InputField3Value)
2025-02-07 18:03:05 +08:00
{
2025-04-24 10:21:49 +08:00
2025-06-03 09:19:44 +08:00
StringEventSystem.Global.Send("InputUI使用结束");
2025-02-07 18:03:05 +08:00
}
2025-02-06 17:23:24 +08:00
}
2025-02-07 18:03:05 +08:00
private void CheacktMP_InputField1String(string userInput)
{
2025-04-24 10:21:49 +08:00
if (userInput == tMP_InputField1Value)
2025-02-07 18:03:05 +08:00
{
tMP_InputField1.text = tMP_InputField1Value;
tMP_InputField1.DeactivateInputField();
tMP_InputField1.readOnly = true;
CheackAllInputField();
}
else
{
tMP_InputField1ErrowIndex++;
2025-04-24 10:21:49 +08:00
if (tMP_InputField1ErrowIndex >= 3)
2025-02-07 18:03:05 +08:00
{
tMP_InputField1.text = tMP_InputField1Value;
tMP_InputField1.DeactivateInputField();
tMP_InputField1.readOnly = true;
CheackAllInputField();
return;
}
else
{
2025-04-24 10:21:49 +08:00
2025-02-07 18:03:05 +08:00
errowTip.gameObject.SetActive(true);
tMP_InputField1.text = string.Empty;
}
}
}
private void CheacktMP_InputField2String(string userInput)
{
if (userInput == tMP_InputField2Value)
{
tMP_InputField2.text = tMP_InputField2Value;
tMP_InputField2.DeactivateInputField();
tMP_InputField2.readOnly = true; CheackAllInputField();
}
else
{
tMP_InputField2ErrowIndex++;
if (tMP_InputField2ErrowIndex >= 3)
{
tMP_InputField2.text = tMP_InputField2Value;
tMP_InputField2.DeactivateInputField();
tMP_InputField2.readOnly = true;
CheackAllInputField();
return;
}
else
{
errowTip.gameObject.SetActive(true);
tMP_InputField2.text = string.Empty;
}
}
}
private void CheacktMP_InputField3String(string userInput)
{
if (userInput == tMP_InputField3Value)
{
tMP_InputField3.text = tMP_InputField3Value;
tMP_InputField3.DeactivateInputField();
tMP_InputField3.readOnly = true; CheackAllInputField();
}
else
{
tMP_InputField3ErrowIndex++;
if (tMP_InputField3ErrowIndex >= 3)
{
tMP_InputField3.text = tMP_InputField3Value;
tMP_InputField3.DeactivateInputField();
tMP_InputField3.readOnly = true;
CheackAllInputField();
return;
}
else
{
errowTip.gameObject.SetActive(true);
tMP_InputField3.text = string.Empty;
}
}
}
/// <summary>
2025-06-03 09:19:44 +08:00
/// 输入最大值
/// </summary>
/// <param name="userInput"></param>
2025-02-07 18:03:05 +08:00
private void CheckInputMax(string userInput)
2025-04-24 10:21:49 +08:00
{
var v = targetMax[CurrentInputIndex].ToString();
2025-02-07 18:03:05 +08:00
if (v != userInput)
2025-04-24 10:21:49 +08:00
{
2025-06-03 09:19:44 +08:00
//第3次填入正确答案
2025-03-26 09:52:44 +08:00
if (errowIntMax >= 3)
2025-02-07 18:03:05 +08:00
{
InputFieldmax.text = targetMax[CurrentInputIndex].ToString();
InputFieldmax.DeactivateInputField();
InputFieldmax.readOnly = true;
2025-03-07 16:14:04 +08:00
Invoke("SetNextInput", 2f);
2025-02-07 18:03:05 +08:00
return;
}
errowIntMax++;
errowTip.gameObject.SetActive(true);
InputFieldmax.text = string.Empty;
2025-04-24 10:21:49 +08:00
2025-02-07 18:03:05 +08:00
}
2025-06-03 09:19:44 +08:00
//正确++
2025-02-07 18:03:05 +08:00
else
{
2025-06-03 09:19:44 +08:00
Debug.Log("填写数值正确");
2025-04-24 10:21:49 +08:00
userInput = targetMax.ToString();
2025-02-07 18:03:05 +08:00
SetNextInput();
}
}
/// <summary>
2025-06-03 09:19:44 +08:00
/// 输入最小值
/// </summary>
/// <param name="userInput"></param>
2025-02-07 18:03:05 +08:00
private void CheckInputMin(string userInput)
{
2025-04-24 10:21:49 +08:00
2025-02-07 18:03:05 +08:00
var v = targetMin[CurrentInputIndex].ToString();
if (v != userInput)
{
2025-06-03 09:19:44 +08:00
//第四次,填入正确答案
2025-03-26 09:52:44 +08:00
if (errowIntMin >= 3)
2025-02-07 18:03:05 +08:00
{
InputFieldmin.text = targetMin[CurrentInputIndex].ToString();
InputFieldmin.DeactivateInputField();
InputFieldmin.readOnly = true;
2025-04-24 10:21:49 +08:00
Invoke("SetNextInput", 2f);
2025-02-07 18:03:05 +08:00
return;
}
errowIntMin++;
errowTip.gameObject.SetActive(true);
InputFieldmin.text = string.Empty;
}
2025-06-03 09:19:44 +08:00
//正确++
2025-02-07 18:03:05 +08:00
else
{
2025-06-03 09:19:44 +08:00
Debug.Log("填写数值正确");
2025-02-07 18:03:05 +08:00
userInput = targetMax.ToString();
SetNextInput();
}
}
protected override void OnShow()
2025-04-24 10:21:49 +08:00
{
}
protected override void OnHide()
{
2025-06-03 09:19:44 +08:00
StringEventSystem.Global.Send("InputUI使用结束");
2025-04-24 10:21:49 +08:00
}
2025-06-03 09:19:44 +08:00
2025-04-24 10:21:49 +08:00
protected override void OnClose()
{
}
2025-02-07 18:03:05 +08:00
private void SetNextInput()
{
2025-04-24 10:21:49 +08:00
if (InputFieldmax.text == targetMax[CurrentInputIndex].ToString() && InputFieldmin.text == targetMin[CurrentInputIndex].ToString())
2025-02-07 18:03:05 +08:00
{
//InputFieldmax.ActivateInputField();
//InputFieldmin.ActivateInputField();
2025-06-03 09:19:44 +08:00
//重置
2025-02-07 18:03:05 +08:00
errowTip.gameObject.SetActive(false);
errowIntMax = 0;
errowIntMin = 0;
InputFieldmax.readOnly = false;
InputFieldmin.readOnly = false;
2025-04-24 10:21:49 +08:00
2025-02-07 18:03:05 +08:00
InputFieldmax.text = string.Empty;
InputFieldmin.text = string.Empty;
CurrentInputIndex++;
if (CurrentInputIndex < images.Length)
{
2025-04-24 10:21:49 +08:00
2025-06-03 09:19:44 +08:00
// 先递增索引
2025-04-24 10:21:49 +08:00
2025-02-07 18:03:05 +08:00
foreach (var item in images)
{
item.gameObject.SetActive(false);
}
images[CurrentInputIndex].gameObject.SetActive(true);
}
else
{
foreach (var item in images)
{
item.gameObject.SetActive(false);
}
ZheSheInput.gameObject.SetActive(true);
}
2025-04-24 10:21:49 +08:00
2025-02-07 18:03:05 +08:00
}
}
}
2025-02-06 17:23:24 +08:00
}