截获鼠标,键盘的消息 监听事件我们都是在Update里面监听的。 Unity的虚拟轴打开:Edit-->Project Settings-->Input,打开的各个Name就是双引号里面要填的,记不住就打开查,也可以自己定义一些按键。
一、Input的主要变量 1: mousePosition: 当前鼠标的像素坐标;
二、Input的主要方法 1: GetAxis/GetAxisRaw: 返回表示虚拟轴的值:
三、Input使用实例 1.创建一个Canvas 5.Game_scene脚本内容: |
void Update () {
// 以屏幕的左下脚为原点;
// Debug.Log(Input.mousePosition); // 当前鼠标的位置;
// 如果一个按键一直按住,那么每帧都会返回True,只到弹起;
// 键盘,鼠标的key
/*if (Input.anyKey) {
Debug.Log(Input.anyKey);
}*/
// 检测按键按下,但是,一旦按下以后,后面就不触发了。
// 如果一个按键一直按着,那么这个只会按下的那帧触发true,
/*if (Input.anyKeyDown) {
Debug.Log(Input.anyKeyDown);
}*/
// 返回你在本次update里面输入的assic字符
/*if (Input.inputString.Length > 0) {
Debug.Log(Input.inputString);
}*/
// end
// 重力加速度的方向,那你就可以根据这个方向来获得输入。
// 如果没有重力传感器,那么就是(0, 0, 0); Vector3的对象;
// Debug.Log(Input.acceleration);
// Debug.Log(Input.touchCount); // 获得当前的触摸的事件的个数;
/*if (Input.touchCount > 0) {
// Input.touches // Touch [];
}*/
// Horizontal 是这个虚拟轴,Eidt --> ProjectSetting -->Input
// 由1-->0, 递减的插值的;
// GetAxis 变化的时候,是有插值的。
// float value = Input.GetAxis("Horizontal");
/*float value = Input.GetAxisRaw("Horizontal");
if (value != 0.0f) {
Debug.Log(value);
}*/
// 距离上一次鼠标的变化; dx, dy
/*float mouse_dx = Input.GetAxis("Mouse X");
float mouse_dy = Input.GetAxis("Mouse Y");
Debug.Log(mouse_dx + ":" + mouse_dy);*/
// 不会有插值的,1,0
// float value = Input.GetAxisRaw("Jump");
// Debug.Log(value);
// 鼠标左键,
/*if (Input.GetButton("Fire1")) {
Debug.Log("fire1 click");
}*/
// 只是在按钮,按下那一刻,为真,
/*if (Input.GetButtonDown("Fire1")) {
Debug.Log("fire1 down");
}
//弹起来那一刻为真
if (Input.GetButtonUp("Fire1")) {
Debug.Log("fire1 up");
}*/
// 键盘上的P按键, 如果按下不松,会一直为true
// GetKeyDown, GetKeyUp,只在状态改变的时候为真
/*if (Input.GetKeyDown(KeyCode.P)) {
Debug.Log("Key p down");
}
if (Input.GetKeyUp(KeyCode.P))
{
Debug.Log("Key p up");
}
if (Input.GetKey(KeyCode.P)) {
Debug.Log("P");
}*/
// 判断鼠标按键有没有按下, int button(0(左), 1(右), 2(中))
if (Input.GetMouseButtonDown(2)) {
Debug.Log("mouse left down");
}
if (Input.GetMouseButtonUp(2)) {
Debug.Log("mouse left up");
}
// Move来使用。
if (Input.GetMouseButton(2)) {
Debug.Log("mouse left all ways");
}
}
四、虚拟轴定义 1: Name: 轴的名字;
五、Touch对象 1:fingerID; 手指ID
|