1.適当にクラスを用意する。
public class InpGameObject : MonoBehaviour
{
public async Task<string> GetKey()
{
var ret="";
while (ret==""){
ret = Input.inputString;
await Task.Delay(16);
}
return ret;
}
}//class
2.外からクラスを割り当てて呼び出す。(MonoBehaviourの無いクラスを想定)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
// Start is called before the first frame update
async void Start()
{
var o =new GameObject("_inp");//空のゲームオブジェクトを生成
o.AddComponent<InpGameObject>();//コンポーネントを割り当てる。
while(!Input.GetKeyDown(KeyCode.Space)){
var ret=await o.GetComponent<InpGameObject>().GetKey();//call
Debug.Log(ret);
await System.Threading.Tasks.Task.Delay(5);
}
}
}