在Unity/C#中验证Text Mesh Pro表单字段时遇到问题。

huangapple go评论71阅读模式
英文:

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(&quot;SceneManager&quot;).GetComponent&lt;GameSceneManager&gt;();
        if (gameSceneManager == null)
        {
            Debug.LogError(&quot;GameSceneManager not found&quot;);
        }
    }


    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 &amp;&amp; suburbValid &amp;&amp; ageValid)
        {
            return true;
        }
        Debug.LogError(&quot;You must fill out all fields&quot;);
        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!

huangapple
  • 本文由 发表于 2023年5月15日 05:30:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定