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());   
    }