GetButton 和 GetKey
Tutorial
Beginner
+0XP
5 mins
173
Unity Technologies

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