英文:
Having problems validating Text Mesh Pro form fields in Unity/C#
问题
我正在尝试在Unity中验证TextMeshPro表单,但以编程方式访问字段似乎出现了某种原因导致它们失效。
以下是我在UI管理器脚本中的内容:
using UnityEngine;
using TMPro;
public class UserInputUI : MonoBehaviour
{
private GameSceneManager gameSceneManager;
[SerializeField]
private TMP_InputField nicknameField;
[SerializeField]
private TMP_Dropdown suburbField;
[SerializeField]
private TMP_Dropdown ageField;
private void Start()
{
gameSceneManager = GameObject.Find("SceneManager").GetComponent<GameSceneManager>();
if (gameSceneManager == null)
{
Debug.LogError("GameSceneManager not found");
}
}
private bool ValidateForm()
{
bool nicknameValid = false;
bool suburbValid = false;
bool ageValid = false;
if (string.IsNullOrEmpty(nicknameField.text) == false)
{
nicknameValid = true;
}
if (suburbField.value == 0)
{
suburbValid = true;
}
if (ageField.value == 0)
{
ageValid = true;
}
if (nicknameValid && suburbValid && ageValid)
{
return true;
}
Debug.LogError("You must fill out all fields");
return false;
}
public void SubmitForm()
{
if (ValidateForm())
{
gameSceneManager.ChangeScene(2);
}
}
}
我已将此脚本附加到Canvas。如果我在检查器中不填充任何序列化字段,那么该字段按预期工作。但是,一旦我拖入引用,该字段就停止工作 - 对鼠标点击没有响应,因此我既无法在文本字段中输入文字,也无法打开下拉菜单。
有人以前见过类似的情况吗?我已经搜索了一下,但找不到任何有用的信息。
英文:
I'm trying to validate a TextMeshPro form in Unity, but accessing the fields programmatically seems to be disabling them for some reason.
Here's what I have in my UI Manager script:
using UnityEngine;
using TMPro;
public class UserInputUI : MonoBehaviour
{
private GameSceneManager gameSceneManager;
[SerializeField]
private TMP_InputField nicknameField;
[SerializeField]
private TMP_Dropdown suburbField;
[SerializeField]
private TMP_Dropdown ageField;
private void Start()
{
gameSceneManager = GameObject.Find("SceneManager").GetComponent<GameSceneManager>();
if (gameSceneManager == null)
{
Debug.LogError("GameSceneManager not found");
}
}
private bool ValidateForm()
{
bool nicknameValid = false;
bool suburbValid = false;
bool ageValid = false;
if (string.IsNullOrEmpty(nicknameField.text) == false)
{
nicknameValid = true;
}
if (suburbField.value == 0)
{
nicknameValid = true;
}
if (ageField.value == 0)
{
nicknameValid = true;
}
if (nicknameValid && suburbValid && ageValid)
{
return true;
}
Debug.LogError("You must fill out all fields");
return false;
}
public void SubmitForm()
{
if (ValidateForm())
{
gameSceneManager.ChangeScene(2);
}
}
}
I've attached the script to the Canvas. If I leave any of the serialised fields unpopulated in the inspector, then that field works as expected. As soon as I drag the reference in, the field stops working - no response to mouse clicks so I can neither type in the text field or open the dropdown.
Has anyone ever seen anything like this before? I've searched around, but I can't find anything that helps.
答案1
得分: 0
I've found the problem. PEBKAC.
如果我尝试在表单未通过验证时提交它,那么它会抛出错误。然后Unity会帮忙暂停播放窗口。这意味着无论我点击多少次,都不会起作用。
这就是为什么孩子们不应该在疲惫时编写代码!
英文:
-sigh- I've found the problem. PEBKAC.
If I try and submit the form when it doesn't pass the validation, then it throws an error. Unity then helpfully pauses the play window. This means that nothing works no matter how many times I click it.
This is why you shouldn't code while tired, kids!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论