Input クラスの GetButton と GetKey 関数
Tutorial
Beginner
+0XP
5 mins
13
Unity Technologies

本チュートリアルでは、Unity プロジェクトで入力用のボタンやキーを取得する方法と、これらの軸がどのように動作するか、また Unity Input マネージャーでどのように変更できるかを説明します。
本チュートリアルは Beginner Scripting プロジェクトに含まれています。
前回のチュートリアルは:Destroy
次のチュートリアルは:GetAxis
Languages available:
1. GetButton と GetKey 関数
KeyInput
using UnityEngine;
using System.Collections;
public class KeyInput : MonoBehaviour
{
public GUITexture graphic;
public Texture2D standard;
public Texture2D downgfx;
public Texture2D upgfx;
public Texture2D heldgfx;
void Start()
{
graphic.texture = standard;
}
void Update ()
{
bool down = Input.GetKeyDown(KeyCode.Space);
bool held = Input.GetKey(KeyCode.Space);
bool up = Input.GetKeyUp(KeyCode.Space);
if(down)
{
graphic.texture = downgfx;
}
else if(held)
{
graphic.texture = heldgfx;
}
else if(up)
{
graphic.texture = upgfx;
}
else
{
graphic.texture = standard;
}
guiText.text = " " + down + "\n " + held + "\n " + up;
}
}ButtonInput
using UnityEngine;
using System.Collections;
public class ButtonInput : MonoBehaviour
{
public GUITexture graphic;
public Texture2D standard;
public Texture2D downgfx;
public Texture2D upgfx;
public Texture2D heldgfx;
void Start()
{
graphic.texture = standard;
}
void Update ()
{
bool down = Input.GetButtonDown("Jump");
bool held = Input.GetButton("Jump");
bool up = Input.GetButtonUp("Jump");
if(down)
{
graphic.texture = downgfx;
}
else if(held)
{
graphic.texture = heldgfx;
}
else if(up)
{
graphic.texture = upgfx;
}
else
{
graphic.texture = standard;
}
guiText.text = " " + down + "\n " + held + "\n " + up;
}
}