Is there any way to convert a String to KeyCode? For example, to be so.
string stof = "J"; KeyCode kk = KeyCode.stof; Debug.Log(kk.ToString()); ΠΡΠ²ΠΎΠ΄ΠΈΡ J Since KeyCode is enum , enum.Parse should help enum.Parse . But you need to "guess" the name.
It turns out that:
(KeyCode)Enum.Parse(typeof(KeyCode), stof) It supports only letters.
A more rigorous version of this (at the same time supports the numbers):
char c = 'J'; // Π½Π°ΡΠΈΠ½Π°Π΅ΠΌ Ρ ΡΠΈΠΌΠ²ΠΎΠ»Π°, Ρ. ΠΊ. Π² ΡΡΡΠΎΠΊΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡ
ΠΌΠ½ΠΎΠ³ΠΎ string keyName; if (c >= 'A' && c <= 'Z') keyName = c.ToString(); else if (c >= '0' && c <= '9') keyName = "Alpha" + c; else throw new ArgumentException("Don't know name for this key"); KeyCode kk = (KeyCode)Enum.Parse(typeof(KeyCode), keyName); (I can't check, I don't have Unity.)
Source: https://ru.stackoverflow.com/questions/597783/
All Articles