我在我的游戏中有开发人员控制台,当你按下向上箭头时,它会加载你之前使用的命令,以输入字段。但是,当我尝试更改脚本中的文本时,我会将前面的命令写入输入字段,但输入字段在您按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()
看起来输入字段改变了它的值,但是它看不到它本身有一些您没有直接输入的文本。
编辑:
为了更好地理解,这里有更多的代码。我从update循环中调用这段代码。
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;
}
}
发布于 2019-08-10 10:55:25
根据您的错误日志,似乎问题出在生成插入符号上:
IndexOutOfRangeException:索引超出了数组的界限。TMPro.TMP_InputField.GenerateCaret(UnityEng...
可能无法在字符串结束处紧跟其后添加插入符号,请尝试:
inputField.caretPosition = inputField.text.Length -1;
而不是。
如果您真的想在字符串结束后立即创建一个插入符号,请使用相同的代码,但在输入字段的末尾留一个空白。
它给玩家一种错觉,即插入符号在字符串的末尾(不过,它只是在一个空白处)。
此外,您的typedCommands
可能没有任何内容,而您可能仍在访问它,因此您可能希望这样做:
if(backCount != 0 && typedCommands.Count != 0)
发布于 2019-08-10 02:09:18
typedCommands.Count返回typedCommands的长度。只要backCount等于0,代码实际读取的就是typedCommandstypedCommands.Count。这是行不通的,因为数组从0开始,typedCommands.Count将从1开始计数,并返回一个超出数组界限的值。
您应该始终从计数中减去1,以保持在数组的边界内,如下所示:
inputField.text = typedCommands[typedCommands.Count - 1 - backCount];
https://stackoverflow.com/questions/57433730
复制相似问题