正在递减数组中的最后一个字符串元素,而不在C#中删除它



我目前正在Unity上做一个演示,将整个字母表放在一个数组中。对于backspace函数,我想减少数组中的最后一个字符串元素,但它不起作用。

[SerializeField] private GameObject keyboard;
[SerializeField] private Text keyboardInput;
private KeyCode[] MyKeys = {KeyCode.A, KeyCode.B,KeyCode.C,KeyCode.D, KeyCode.E,KeyCode.F,KeyCode.G,KeyCode.H,KeyCode.I,KeyCode.J,KeyCode.K,KeyCode.L,KeyCode.M,KeyCode.N,KeyCode.O,KeyCode.P,KeyCode.Q,KeyCode.R,KeyCode.S,KeyCode.T, KeyCode.U ,KeyCode.V,KeyCode.W,KeyCode.X,KeyCode.Y,KeyCode.Z, KeyCode.Space, KeyCode.Backspace };
private string[] currText;
void Start()
{
keyboardInput.text = currText.ToString();
}
private void typeKeys()
{
for (var i = 0; i < MyKeys.Length; i++)
{
if (Input.GetKeyDown(MyKeys[i]))
{
if (i == 0)
{
keyboardInput.text += "a";
}
else if (i == 1)
{
keyboardInput.text += "b";
}
else if (i == 2)
{
keyboardInput.text += "c";
}
else if (i == 3)
{
keyboardInput.text += "d";
}
else if (i == 4)
{
keyboardInput.text += "e";
}
else if (i == 5)
{
keyboardInput.text += "f";
}
else if (i == 6)
{
keyboardInput.text += "g";
}
else if (i == 7)
{
keyboardInput.text += "h";
}
else if (i == 8)
{
keyboardInput.text += "i";
}
else if (i == 9)
{
keyboardInput.text += "j";
}
else if (i == 10)
{
keyboardInput.text += "k";
}
else if (i == 11)
{
keyboardInput.text += "l";
}
else if (i == 12)
{
keyboardInput.text += "m";
}
else if (i == 13)
{
keyboardInput.text += "n";
}
else if (i == 14)
{
keyboardInput.text += "o";
}
else if (i == 15)
{
keyboardInput.text += "p";
}
else if (i == 16)
{
keyboardInput.text += "q";
}
else if (i == 17)
{
keyboardInput.text += "r";
}
else if (i == 18)
{
keyboardInput.text += "s";
}
else if (i == 19)
{
keyboardInput.text += "t";
}
else if (i == 20)
{
keyboardInput.text += "u";
}
else if (i == 21)
{
keyboardInput.text += "v";
}
else if (i == 22)
{
keyboardInput.text += "w";
}
else if (i == 23)
{
keyboardInput.text += "x";
}
else if (i == 24)
{
keyboardInput.text += "y";
}
else if (i == 25)
{
keyboardInput.text += "z";
}
else if (i == 26)
{
keyboardInput.text += " ";
}
else if (i == 27)
{
currText = currText.Take(currText.Length - 1).ToArray();
}
}
};
}
// Update is called once per frame
void Update()
{
typeKeys();
}

这是我试图开始工作的部分

else if (i == 27)
{
currText = currText.Take(currText.Length - 1).ToArray();
}

请参阅视频了解

我的意思是,它不起作用是因为我无法从数组中删除最后一个字母,因此在我的虚拟设置中,会产生在句子中使用退格的效果。这就是整个剧本。

让我们从减少代码量开始。然后,在那之后,我们还可以利用两个事实;Unity有一个KeyCode枚举,字母表按从"a"到"a"+25的值排序。我会用一点代码来澄清这一点:

private Text keyboardInput;
List<char> charList = new List<char> ( );
private void typeKeys( )
{
var length = charList.Count;
if ( Input.GetKeyDown ( KeyCode.Space ) )
charList.Add ( ' ' );
if ( Input.GetKeyDown ( KeyCode.Backspace ) && length  > 0 )
charList.RemoveAt ( length - 1 );
for ( var i = 0; i < 26; i++ )
if ( Input.GetKeyDown ( KeyCode.A + i ) ) charList.Add ( ( char ) ( 'a' + i ) );
if ( length != charList.Count ) keyboardInput.text =  string.Concat ( charList );
}

我在这里要做的是,首先检查Space键,然后检查Backspace键。之后,我遍历字母表字符,将i值添加到KeyCode.A。请记住,默认情况下枚举只是int。如果匹配,我们将相应的i值添加到"a"字符中,并将其添加到List<char>列表中,记住还要先将生成的值强制转换回char

最后,如果您的字符数组的长度发生了变化,那么,也只有到那时,我们才会更新您的keyboardInput.text。这有助于节省每一帧产生的字符串垃圾。我们可以将Concat作为char的IEnumerable作为string

使用此方法可以删除以private KeyCode[] MyKeys开头的整行。您不需要将所有的KeyCode值存储在一个数组中进行检查。

这不是你的问题的答案,为了得到正确的答案,你需要发布完整的代码和对问题的解释,但我想我会发布这篇文章,因为它可能对你有用。

我重构了typeKeys()方法。

private void typeKeys()
{
foreach (var key in MyKeys.Where(k => k != KeyCode.Space).Where(k => k != KeyCode.Backspace))
{
if (Input.GetKeyDown(key))
{
keyboardInput.text += key.ToString().ToLower();
}
}
if (Input.GetKeyDown(KeyCode.Space))
{
keyboardInput.text += " ";
}
if (Input.GetKeyDown(KeyCode.Backspace))
{
currText = currText.Take(currText.Length - 1).ToArray();
}
}

就是这样。

currText = currText.Take(currText.Length - 1).ToArray();应该通过一个元素截断正确定义的数组,或者简单地将其保持在零个元素。

最新更新