Unity 独自の仮想キー
1.GameObjectのExtensionに。
public static async Task<string> GetVKey(this GameObject @this,int delaytime=1000/200){
...
}
2.リソースフォルダーに入れておくのみ。
//Assets/Resources/InputGetter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
public class InputGetter : MonoBehaviour
{
public Dictionary<string,KeyCode> keymap=new Dictionary<string, KeyCode>(){
{"<",KeyCode.A},
{">",KeyCode.D},
{"^",KeyCode.W},
{"V",KeyCode.S},
{"A",KeyCode.J},
{"B",KeyCode.K},
{"X",KeyCode.L},
{"Y",KeyCode.O},
{"L",KeyCode.U},
{"R",KeyCode.I},
{"S",KeyCode.Space},
{"P",KeyCode.Return},
};
[SerializeField] string vkey="";
public async Task<string> GetKey(int delaytime=1000/200){
vkey ="";
while(vkey=="") await Task.Delay(delaytime);
return vkey;//<>^V ABXYLR SP
}
void Update() {
if(vkey=="")
foreach (var k in keymap)
if (Input.GetKeyDown(k.Value)) vkey = k.Key;
}
}//class
public static class GameObjectInputExtension{
static GameObject getter;
public static async Task<string> GetVKey(this GameObject @this,int delaytime=1000/200){
if(getter==null){
getter = new GameObject("InputGetter");
getter.AddComponent<InputGetter>();
}
return await getter.GetComponent<InputGetter>().GetKey(delaytime);
}
}//class
3.適当なゲームオブジェクトから呼び出す。
async void Start()
{
Debug.Log(await gameObject.GetVKey());
}
Author And Source
この問題について(Unity 独自の仮想キー), 我々は、より多くの情報をここで見つけました https://qiita.com/UnityFoo/items/4796c87c192b21a97a1d著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .