
GetButton 및 GetKey
Tutorial
Beginner
+10XP
5 mins
(37)
Unity Technologies

이 튜토리얼에서는 Unity 프로젝트에서 버튼 입력 또는 키 입력을 받는 방법을 안내하고, 해당 축이 어떤 식으로 동작하며 Unity 입력 관리자를 통해 어떻게 수정될 수 있는지 알아보겠습니다.
이 튜토리얼은 스크립팅의 기초 프로젝트에 포함되어 있습니다.
이전 튜토리얼: Destroy
다음 튜토리얼: GetAxis
1. GetButton 및 GetKey
KeyInput
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class KeyInput : MonoBehaviour
{
public Image graphic;
public Sprite standard;
public Sprite downgfx;
public Sprite upgfx;
public Sprite heldgfx;
public Text boolDisplay1;
public Text boolDisplay2;
public Text boolDisplay3;
void Start()
{
graphic.sprite = standard;
}
void Update()
{
bool down = Input.GetKeyDown(KeyCode.Space);
bool held = Input.GetKey(KeyCode.Space);
bool up = Input.GetKeyUp(KeyCode.Space);
if(down)
{
graphic.sprite = downgfx;
}
else if (held)
{
graphic.sprite = heldgfx;
}
else if (up)
{
graphic.sprite = upgfx;
}
else
{
graphic.sprite = standard;
}
boolDisplay1.text = " " + down;
boolDisplay2.text = " " + held;
boolDisplay3.text = " " + held;
}
}ButtonInput
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ButtonInput : MonoBehaviour
{
public Image graphic;
public Sprite standard;
public Sprite downgfx;
public Sprite upgfx;
public Sprite heldgfx;
public Text boolDisplay1;
public Text boolDisplay2;
public Text boolDisplay3;
void Start()
{
graphic.sprite = standard;
}
void Update()
{
bool down = Input.GetButtonDown("Jump");
bool held = Input.GetButton("Jump");
bool up = Input.GetButtonUp("Jump");
if(down)
{
graphic.sprite = downgfx;
}
else if (held)
{
graphic.sprite = heldgfx;
}
else if (up)
{
graphic.sprite = upgfx;
}
else
{
graphic.sprite = standard;
}
boolDisplay1.text = " " + down;
boolDisplay2.text = " " + held;
boolDisplay3.text = " " + held;
}
}