蓝鸥Unity开发教程之课时7 Input类
2016-09-09 10:27
323 查看
蓝鸥Unity开发教程之课时7 Input类
一、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 ("持续按下鼠标左键");
}
}
}
一、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 ("持续按下鼠标左键");
}
}
}
相关文章推荐
- 蓝鸥Unity开发教程之课时18 射线【完结】
- 蓝鸥Unity开发教程之课时2 游戏对象和基本操作
- 蓝鸥Unity开发教程之课时10 Transform
- 蓝鸥Unity开发教程之课时4 3D开发基础
- 蓝鸥Unity开发教程之课时8 GameObject
- 【蓝鸥Unity开发基础三】课时7 Input类
- 蓝鸥Unity开发教程之课时13 鼠标事件
- 蓝鸥Unity开发教程之课时16 触发事件
- 蓝鸥Unity开发教程之课时6 脚本生命周期
- 蓝鸥Unity开发教程之课时17 物理材质
- 蓝鸥Unity开发教程之课时16 碰撞事件
- 蓝鸥Unity开发教程之课时11 Time和Mathf类
- 蓝鸥Unity开发教程之课时15 刚体
- 【蓝鸥Unity开发基础三】课时1 下载并安装unity
- 蓝鸥Unity开发基础二——课时12 递归
- 蓝鸥Unity开发基础二——课时22 ArrayList
- 蓝鸥Unity开发教程之课时5 Unity组件开发
- 蓝鸥Unity开发基础二——课时26 委托
- 蓝鸥Unity开发教程之课时5 Unity组件开发之脚本组件
- 蓝鸥Unity开发基础二——课时23 List