01_Unity3D的输入(Input)——键盘和鼠标

 Unity3D使用input类控制用户的输入,输入包括了用户键盘,鼠标,触摸,重力感应以及地理位置输入等输入方式。

     本节我们介绍Unity3D中如何监听用户的鼠标、键盘输入。

     首先我们介绍一个很重要的类KeyCode,此类中的值记录各种按键,详细键码参照Unity圣典:http://game.ceeger.com/Script/Enumerations/KeyCode/KeyCode.html


一.键盘的输入

GetKey,GetKeyDown,GetKeyUp三个方法分别获取用户键盘按键的输入

     GetKey:用户长按按键有效;

     GetKeyDown:用户按下按键时有效;

     GetKeyUp:用户抬起按键时有效;

示例:

public class Test1 : MonoBehaviour {
void Update ()
    {
        bool down = Input.GetKeyDown(KeyCode.Space);
        bool held = Input.GetKey(KeyCode.Space);
        bool up = Input.GetKeyUp(KeyCode.Space);
        if(down)
        {
               GUI.Label(new Rect(100,100,400,100),"down");
         }
        else if(held)
        {
               GUI.Label(new Rect(100,100,400,100),"held");
        }
        else if(up)
        {
               GUI.Label(new Rect(100,100,400,100),"up");
        }
        else
        {
               GUI.Label(new Rect(100,100,400,100),"else");
        }
       
    }
}


• anyKey,anyKeyDown

anyKey用于监听用户的任意按键输入或鼠标点击,事件触发后返回true。anyKeyDown:当任意按键松开时返回true。

示例:

public class Test2 : MonoBehaviour {
     void OnGUI(){
          if(Input.anyKey){
               GUI.Label(new Rect(100,100,400,100),"测试任意按键");         
          }
     }
}



GetButton(string buttonName)

GetButton用于监听Button的输入,参数为自己指定的button的名字,在Unity中可以使用管理器添加或修改Input参数。unity已经为用户预设了多种输入情况,在button中预设了Fire、Jump等输入情况。用户也可以自行修改,打开Edit-->Project   Settings-->Input可以看到预设如下:


以Fire1为例,可以看到使用左ctil键或者鼠标左键控制Fire1按钮。


示例:


public class Test3 : MonoBehaviour {
     void OnGUI(){
          if(Input.GetButton("Fire1")){
               GUI.Label(new Rect(100,100,400,100),"测试按钮点击");         
          }
     }
}

另外还有GetButtonDown( 当某个按钮被按下的那一帧返回true)


GetButtonUp( 当某个按钮弹起时返回true)两个方法,就不详细介绍了。



二.鼠标的输入

mousePosition

     Input.mousePosition返回当前鼠标的位置,这里指的是距离原点的像素位置,说明一下,Unity中的原点(0,0)位置为左下角,上位y轴正方向,右为x轴正方向。返回值类型为Vector3。


示例:


public class mono2 : MonoBehaviour {
     void OnGUI(){
               GUI.Label(new Rect(100,100,400,100),"x="+Input.mousePosition.x+"  y="+Input.mousePosition.y+"  z="+Input.mousePosition.z);         
     }
}

当用户移动鼠标是即可显示x,y坐标,z为0。



•GetMouseButton

       GetMouseButton 对应的键处于按下状态时返回true

          GetMouseButtonDown 对应的键被按下时返回true

          GetMouseButtonUp 对应的键弹起时返回true

示例:


public class mono2 : MonoBehaviour {
     void OnGUI(){
<span style="white-space:pre">	</span>  GUI.Label(new Rect(100,100,400,100),"X="+Input.GetAxis("Fire1")+"Y="+Input.GetAxis("Fire1"));
          if(Input.GetMouseButton(0)){
          GUI.Label(new Rect(100,100,400,100),"鼠标左键按下");
          }else if(Input.GetMouseButton(1)){
          GUI.Label(new Rect(100,100,400,100),"鼠标右键按下");
          }else if(Input.GetMouseButton(2)){
          GUI.Label(new Rect(100,100,400,100),"鼠标中键按下");
          }
    }
}

在例子中可以看出参数为int型,含义为:0左键,1右键,2中键。




  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值