LoginSignup
0
0

More than 1 year has passed since last update.

Unity スクリプトからゲームオブジェクトとクラスの適用

Last updated at Posted at 2021-09-13

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

    }
}

0
0
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
0