0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Unity】ボタンを押したら InputField の値を取得する方法

Last updated at Posted at 2021-10-03

環境

Unity 2019.4.22f
Windows 10

やること

以下の動画のように、インプットフィールドに何かを入力し、ボタンを押すと、インプットフィールドの値をコンソールに表示する。
 
Image from Gyazo

やり方

Unity 2D プロジェクトを新規に作成。

Hierarchy > + > UI で下図のように InputField と Button を配置。

Image from Gyazo

以下のスクリプトを作成

ButtonController.cs
using UnityEngine;
using UnityEngine.UI;

public class ButtonController : MonoBehaviour
{
    public GameObject inputGameObject;
    public Button button;

    public void OnClick()
    {
        Debug.Log(inputGameObject.GetComponent<Text>().text);
    }
}

そのスクリプトを ボタンのオブジェクトにアタッチ
Image from Gyazo

Button の Inspector を下画像のようにする。

Image from Gyazo

以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?