Unity篇09:Unity输入Input类 - kudan-game/ArtArtist-Repo GitHub Wiki

本篇介绍unity封装好的对于用户的输入响应。

Unity3d的所有输入,包括键盘鼠标和智能手机触摸等都是通过这一个类来完成 –Input类。 见API:http://docs.unity3d.com/ScriptReference/Input.html

输入Input类使用:建议在Update()方法内使用。官方文档如是说: –Note also that the Input flags are not reset until “Update()”, so its suggested you make all the Input Calls in the Update Loop.

输入管理

输入管理–InputManager

通过菜单Edit–>Project Settings–>Input可打开InputManager。

鼠标输入

Input.mousePosition获得相应的鼠标位置:Input的静态变量

点击鼠标:鼠标左键为0,右键为1,滚轮为2.

Input.mousePosition//获得鼠标位置
Input.GetMouseButton(0)//按下状态瞬间为true
Input.GetMouseButtonDown(0)//鼠标左键按下,按下瞬间为true
Input.GetMouseButtonDown(1)//鼠标右键按下,按下瞬间为true
Input.GetMouseButtonUp(0)//鼠标左键抬起,弹起瞬间为true
Input.GetMouseButtonUp(1)//鼠标右键抬起,弹起瞬间为true

触摸输入

主要有以下变量与方法

Input.multiTouchEnabled
Input.simulateMouseWithTouches
Input.touchCount
Input.touches
Input.touchSupported
Input.GetTouch()

键盘输入

Input.GetAxis() //返回float值而不是布尔值,从-1到1之间
Input.GetAxisRaw()//返回非区间,而是端点值(-1,0,1)或者摇杆的位置
Input.GetButton() //按下状态瞬间为true
Input.GetButtonDown()//按下瞬间为true
Input.GetButtonUp()//弹起瞬间为true
Input.GetKey()//点击返回true,其他情况返回false
Input.GetKeyDown(KeyCode.A)//当键盘上某个键被一直按住的时候,其返回值为true,否则为false。
Input.GetKeyUp(KeyCode.A)//当键盘上某个键按下之后抬起的时候,其返回值为true,否则为false。

getKey()方法

在方法内输入Key值即可

    void Update() {
        if (Input.GetKey("up"))//直接输入Key值
            print("up arrow key is held down");
        if (Input.GetKey(KeyCode.UpArrow))//通过KeyCode类输入Key值
            print("KeyCode.UpArrow key is held down");
    }

常用的键值:  
KeyCode.大写字母A-Z                              //字母键  
KeyCode.UpArrow  
KeyCode.DownArrow  
KeyCode.LeftArrow  
KeyCode.RightArrow  
KeyCode.Return                                   //回车  
KeyCode.Escape                                   //Esc返回  
KeyCode.Space                                    //空格  
KeyCode.LeftControl  
KeyCode.RightControl  
KeyCode.LeftShift  
KeyCode.RightShift  
KeyCode.Tab  
KeyCode.Delete  
KeyCode.Backspace

GetAxis()方法

返回float值而不是布尔值,从-1到1之间 。输入管理类InputManager专门为Axis设置了参数 。

参数:Horizontal:表示水平方向,其值从-1到1之间变化,当A键被按下的时候其在水平方向上的向量为0-1(取不到0),当D键被按下的时候其在水平方向上的向量为-1-0(取不到0),当A键和D键都没有被按下的时候,其水平方向上的向量为0;Vertical:表示竖直方向,其值从-1到1之间的变化,当W键被按下的时候其在竖直方向上的向量为0-1(取不到0),当S键被按下的时候其在竖直方向上的向量为-1-0(取不到0),当S键和W键都没有被按下的时候,其竖直方向上的向量为0。

返回值:float—按下设定的某个键之后,表示变化的数值,从-1到1之间变化。

含义:检测键盘上某个按键被一直按住的时候,其在设定的方向上的变化。

MonoBehavior的鼠标事件

类似Start()和Update()方法一样,以下鼠标方法在MonoBehavior子类可以直接使用。 以下方法要求组件的Collider必须打开,否则都将检测不到鼠标。

void OnMouseEnter(){}//鼠标进入区域
void OnMouseOver(){}//鼠标停留在区域内
void OnMouseExit(){}//鼠标离开区域
void OnMouseDown(){}//鼠标按下
void OnMouseUp(){}  //鼠标松开
void OnMouseDrag(){}//鼠标拖拽
void OnMouseUpAsButton(){}

anyKey获取任意键按住、anyKeyDown获取任意键按下

using UnityEngine;  
using System.Collections;  
using UnityEngine.UI;  
public class KeyCode : MonoBehaviour {        
//显示键值信息的UI文本组件      
public Text keycodeText;        
//只能在OnGUI内获取键值      
void OnGUI()      
{          
    if (Input.anyKeyDown)          
    {              
       Event e = Event.current;              
       if (e.isKey)             
       {                  
           keycodeText.text ="按下的键值:" + e.keyCode.ToString();           
       }          
    }      
}  
}