【Unity】버튼을 누르면 InputField 의 값을 취득하는 방법

2483 단어 Unity

환경



Unity 2019.4.22f
Windows 10

할 일



다음 동영상과 같이 입력 필드에 무언가를 입력하고 버튼을 누르면 입력 필드의 값이 콘솔에 표시됩니다.
 


방법



Unity 2D 프로젝트를 새롭게 작성.

Hierarchy > + > UI 에서 아래 그림과 같이 InputField 와 Button 을 배치.



다음 스크립트 작성

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

스크립트를 버튼의 객체에 연결


Button의 Inspector를 아래 그림과 같이 한다.



이상.

좋은 웹페이지 즐겨찾기