一、Input类
二、Input获取键盘事件
三、Input获取鼠标事件
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
void Start () {
}
//获取用户事件需要使用Input类
void Update () {
//每帧都需要监听用户事件
//在当前这一帧中,如果用户按下W就会返回true,否则返回false
if (Input.GetKeyDown (KeyCode.W)) {
print ("往前走");
}
//Input.GetKeyDown()方法用来检测按键按下的事件
if (Input.GetKeyDown (KeyCode.S)) {
//print只能在MonoBehaviour的子类中使用,其他情况下只能使用Debug.Log()打印
Debug.Log ("往后退");
}
//Input.GetKeyUp()方法用来检测按键弹起的事件
if(Input.GetKeyUp(KeyCode.Alpha1)){
print ("弹起了1键");
}
//Input.GetKey()方法用来检测持续按键的事件
if(Input.GetKey(KeyCode.F)){
print ("按下了F键");
}
//Input.GetMouseButtonDown()方法用来检测鼠标按键按下的事件
//参数0表示鼠标左键
//参数1表示鼠标右键
//参数2表示鼠标中键
if(Input.GetMouseButtonDown(0)){
print ("按下鼠标左键");
}
//Input.GetMouseButtonUp()方法用来检测鼠标按键弹起的事件
if(Input.GetMouseButtonUp(0)){
print ("弹起了鼠标左键");
}
//Input.GetMouseButton()方法用来检测鼠标持续按下的事件
if(Input.GetMouseButton(0)){
print ("持续按下鼠标左键");
}
}
}
一、Input类
二、Input获取键盘事件
三、Input获取鼠标事件
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
void Start () {
}
//获取用户事件需要使用Input类
void Update () {
//每帧都需要监听用户事件
//在当前这一帧中,如果用户按下W就会返回true,否则返回false
if (Input.GetKeyDown (KeyCode.W)) {
print ("往前走");
}
//Input.GetKeyDown()方法用来检测按键按下的事件
if (Input.GetKeyDown (KeyCode.S)) {
//print只能在MonoBehaviour的子类中使用,其他情况下只能使用Debug.Log()打印
Debug.Log ("往后退");
}
//Input.GetKeyUp()方法用来检测按键弹起的事件
if(Input.GetKeyUp(KeyCode.Alpha1)){
print ("弹起了1键");
}
//Input.GetKey()方法用来检测持续按键的事件
if(Input.GetKey(KeyCode.F)){
print ("按下了F键");
}
//Input.GetMouseButtonDown()方法用来检测鼠标按键按下的事件
//参数0表示鼠标左键
//参数1表示鼠标右键
//参数2表示鼠标中键
if(Input.GetMouseButtonDown(0)){
print ("按下鼠标左键");
}
//Input.GetMouseButtonUp()方法用来检测鼠标按键弹起的事件
if(Input.GetMouseButtonUp(0)){
print ("弹起了鼠标左键");
}
//Input.GetMouseButton()方法用来检测鼠标持续按下的事件
if(Input.GetMouseButton(0)){
print ("持续按下鼠标左键");
}
}
}