使用变量而不是键码 Unity

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

Use variable instead of keycode unity

问题

在您提供的代码中,您似乎正在尝试将一个随机选择的单词转换为字符数组,然后跟踪用户是否键入了这些字符。问题出在您尝试使用字符作为键码(Keycode)来检查输入时。这可能不是一种有效的方法,因为Keycode主要用于处理键盘上的物理键,而不是字符。

如果您想检查用户是否输入了单词中的字符,可以尝试以下方法:

string currentWord = wordArray[Random.Range(0, typingWords.Length)];

// 将单词分割为字符
char[] wordAsArray = currentWord.ToCharArray();

// 遍历字符数组并检查用户输入
foreach (char character in wordAsArray)
{
    if (Input.GetKey(character.ToString()))
    {
        Debug.Log("Test");
    }
}

这将遍历单词中的每个字符,并检查用户是否按下与字符匹配的键。但请注意,这种方法仍然有局限性,因为它假设用户按下与字符完全匹配的键。

如果您的目标是更复杂的文本输入或游戏内文字处理,您可能需要考虑使用其他方法,例如检查用户输入的字符串是否包含特定单词,而不是单个字符的匹配。

英文:

Okay so I'm trying to make a system that picks a random word then it turns that word into a char array. Then it will track if you type the characters. But the method that I'm trying to do hasn't been working. mainly because it won't let me use a variable name as a keycode. Is this a worthwhile problem, or should I abort mission and try something else.

string currentWord = wordArray[Random.Range(0, typingWords.Length)];
char[] wordAsArray = currentWord.ToCharArray();
Keycode currentLetter = wordAsArray[0];
if (Input.GetKey(currentLetter))
{
    Debug.Log("Test");
}

most of this works fine but what doesn't work the problem is the if (Input.GetKey(currentLetter))

is there something that can turn the word into a KeycodeArray or something like that or turn the specific character into keycode.

Does anybody know if this problem is solvable or will I have to use another method.

答案1

得分: 0

没有内置的函数可以直接将单词转换为 KeyCode 数组或类似的东西,或者将特定字符转换为 keycode。

GetNextKeyCode 方法通过将单词中的下一个字母转换为大写,然后使用System.Enum.Parse来查找相应的 KeyCode 值。

GetKeyPressed 方法检查哪个键被按下,并通过循环所有可能的 KeyCode 值来确定哪个键被按下,使用 Input.GetKeyDown。

英文:

is there something that can turn the word into a KeycodeArray or something like that or turn the specific character into keycode.

There is not a built-in function for that. You can create a keycode array with each keycodes in it and use that to determine current letter. Like:

KeyCode[] letterCodes = new KeyCode[] {
    KeyCode.A, KeyCode.B, KeyCode.C, ... KeyCode.Z
};
KeyCode currentLetterCode = letterCodes[wordAsArray[0] - 'A'];

'A' is subtracted from it to get the index of the letter in the letterCodes array. This assumes that the word only contains uppercase letters.

Instead of that method i would use a different approach. I will write down a complete example of how i would do it with comments.

public class TypingGame : MonoBehaviour
{
    public Text wordText;

    private string currentWord;
    private int currentIndex;

    private void Start()
    {
        // Pick a random word to type
        currentWord = GetRandomWord();
        // Display the word on screen
        wordText.text = currentWord;
    }

    private void Update()
    {
        // Check if the current letter has been typed
        if (Input.anyKeyDown)
        {
            KeyCode keyPressed = GetKeyPressed();
            if (keyPressed != KeyCode.None && keyPressed == GetNextKeyCode())
            {
                currentIndex++;
                if (currentIndex >= currentWord.Length)
                {
                    // The word has been completely typed
                    currentWord = GetRandomWord();
                    wordText.text = currentWord;
                    currentIndex = 0;
                }
                else
                {
                    // Update the display to show the next letter
                    wordText.text = currentWord.Substring(currentIndex);
                }
            }
        }
    }

    private string GetRandomWord()
    {
        // Replace this with your own word selection logic
        string[] words = { "cat", "dog", "bird", "fish" };
        return words[Random.Range(0, words.Length)];
    }

    private KeyCode GetNextKeyCode()
    {
        char nextChar = currentWord[currentIndex];
        KeyCode keyCode = (KeyCode)System.Enum.Parse(typeof(KeyCode), nextChar.ToString().ToUpper());
        return keyCode;
    }

    private KeyCode GetKeyPressed()
    {
        foreach (KeyCode keyCode in System.Enum.GetValues(typeof(KeyCode)))
        {
            if (Input.GetKeyDown(keyCode))
            {
                return keyCode;
            }
        }
        return KeyCode.None;
    }
}

GetNextKeyCode method converts the next letter in the word to a KeyCode value by converting the letter to uppercase and using System.Enum.Parse to look up the corresponding KeyCode value.

GetKeyPressed method checks which key was pressed and returns the corresponding KeyCode value by looping over all possible KeyCode values and checking which one has been pressed using Input.GetKeyDown.

huangapple
  • 本文由 发表于 2023年2月18日 21:11:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493558.html
匿名

发表评论

匿名网友

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

确定