【问题标题】:How to correctly set text of input field from script in Unity Engine?如何从 Unity Engine 中的脚本正确设置输入字段的文本?
【发布时间】:2019-08-09 16:05:36
【问题描述】:

我的游戏中有开发者控制台,当您按下向上箭头时,它会加载您之前使用的命令到输入字段。但是,当我尝试从脚本中更改文本时,我会将上一个命令写入输入字段,但在您按下 esc 键之前,输入字段不可编辑。

我正在使用新的 TMPro.TMP_InputField。

inputField.text = typedCommands[(typedCommands.Count) - backCount];
inputField.caretPosition = inputField.text.Length;

在第一行我设置输入字段的文本变量,在第二行我将光标设置在输入字段的最后一个字符后面。

当我在游戏运行时尝试从编辑器的输入字段中删除所有文本时,我收到此错误:

IndexOutOfRangeException: Index was outside the bounds of the array.
TMPro.TMP_InputField.GenerateCaret (UnityEngine.UI.VertexHelper vbo, UnityEngine.Vector2 roundingOffset) (at Library/PackageCache/com.unity.textmeshpro@2.0.0/Scripts/Runtime/TMP_InputField.cs:3304)
TMPro.TMP_InputField.OnFillVBO (UnityEngine.Mesh vbo) (at Library/PackageCache/com.unity.textmeshpro@2.0.0/Scripts/Runtime/TMP_InputField.cs:3271)
TMPro.TMP_InputField.UpdateGeometry () (at Library/PackageCache/com.unity.textmeshpro@2.0.0/Scripts/Runtime/TMP_InputField.cs:3209)
TMPro.TMP_InputField.Rebuild (UnityEngine.UI.CanvasUpdate update) (at Library/PackageCache/com.unity.textmeshpro@2.0.0/Scripts/Runtime/TMP_InputField.cs:3184)
UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs:198)
UnityEngine.Canvas:SendWillRenderCanvases()

似乎输入字段改变了它的值,但它没有看到它本身有一些你没有直接输入的文本。

编辑:

这里有更多代码以便更好地理解。我从更新循环中调用这段代码。

    private void typedCommandFunc()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow) && backCount != (typedCommands.Count))
            backCount++;

        if (Input.GetKeyDown(KeyCode.DownArrow) && backCount > 0)
            backCount--;

        if(backCount != 0)
        {
            inputField.text = typedCommands[(typedCommands.Count) - backCount];
            inputField.caretPosition = inputField.text.Length;
        } 
    }

【问题讨论】:

  • When I try to delete all text from the input filed from editor... 是变量typedCommands 链接到您提到的input field
  • 对不起@Kaynn 我没有发布足够的代码。您可以检查我的问题的第一次编辑。

标签: unity3d


【解决方案1】:

根据您的错误日志,问题似乎在于生成插入符号:

IndexOutOfRangeException:索引超出了数组的范围。 TMPro.TMP_InputField.GenerateCaret(UnityEng...

可能无法在字符串结束后立即添加插入符号,请尝试:

inputField.caretPosition = inputField.text.Length -1;

改为。

如果您真的想在字符串结束后立即插入插入符号,请使用相同的代码,但在输入字段的末尾有一个空白空格。
它给玩家一种错觉,其中插入符号在字符串的末尾(虽然它只是在一个空白处)。

此外,您的 typedCommands 可能一无所有,而您可能仍在访问它,因此您可能想要这样做:

if(backCount != 0 && typedCommands.Count != 0)

【讨论】:

  • 行为与上一版本相同。但是,如果您至少有两个 previos 命令并且您按两次向上箭头然后按三次向下箭头,您将获得与按两次向下箭头相同的命令,但这次您可以正常编辑您获得的命令。
【解决方案2】:

typedCommands.Count 返回 typedCommands 的长度。每当 backCount 等于 0 时,代码实际上读取 typedCommands[typedCommands.Count]。这不起作用,因为数组从 0 开始,而 typedCommands.Count 将从 1 开始计数,并将返回一个超出数组边界的值。

您应该始终从计数中减去 1 以保持在数组的边界内,如下所示:

inputField.text = typedCommands[typedCommands.Count - 1 - backCount];

【讨论】:

  • 感谢重播@ThowV 我知道我做错了,但我删除了减一,因为当您在列表中有一项(计数为一)并且 backCount 为一(您想输入上一个命令)它是 1-1-1,即 -1。我需要重新考虑这部分。
猜你喜欢
  • 2022-10-12
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2012-10-05
  • 2012-12-12
相关资源
最近更新 更多