LoginSignup
0
1

More than 1 year has passed since last update.

Unity 独自の仮想キー

Last updated at Posted at 2021-09-13

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

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1