【发布时间】: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