Input常用的几个函数

键盘相关的输入函数:

        1. public static bool GetKey(string name);

     描述:

              当用户保持某个键(name)为按下状态时返回true。比如自动发射。

              要处理输入的时候推荐使用Input.GetAxis 和 Input.GetButton, 因为它们允许终端用户自定义键的名称。

if (Input.GetKey("up"))

if (Input.GetKey("down"))


        

        public static bool GetKey(KeyCodekey);

     描述:

                当用户保持某个由KeyCode定义的键为按下状态时返回true。

if (Input.GetKey(KeyCode.UpArrow))

if (Input.GetKey(KeyCode.DownArrow))

 

      2. public static boolGetKeyDown(stringname);

      描述:

                当用户开始按下由name定义的键时返回true。

                你需要在Update函数中调用这个函数,因为按键状态在每一帧被重置。如果用户没有释放按键并重新按下它,函数将不    会返回true。

 

        public static boolGetKeyDown(KeyCodekey);

 


        3. public static boolGetKeyUp(string name);

       描述:

                  当用户释放由name定义的键时返回true。

 

           public staticboolGetKeyUp(KeyCodekey);

 

总结:GetKeyDownGetKeyUp 分别代表了按键的down和up状态,而hold状态则使用了函数GetKey。string类型的参数name表示键盘上每一个键项对应的名字,并使用小写(比如空格键--space, 向上方向键--up,A键--a)。

 

 

鼠标相关的输入函数:

               1. public static bool GetMouseButton(int button);

          描述:

                    当指定的鼠标键保持按下状态时返回true。

                    button的值表示: 0 左键, 1右键, 2 滚轮键。

        if(Input.GetMouseButton(0))

           Debug.Log("Pressed left click.");

       

        if(Input.GetMouseButton(1))

           Debug.Log("Pressed right click.");

       

        if(Input.GetMouseButton(2))

           Debug.Log("Pressed middle click.");

      

            2. public static bool GetMouseButtonDown(int button);

          描述:

                     指定的键按下时返回true。

 

             3. public static bool GetMouseButtonUp(int button);

          描述:

                    指定的键释放时返回true。

 

总结:GetMouseButtonDownGetMouseButtonUp 分别代表了鼠标键的down 和 up 状态,而hold 状态则使用函数GetMouseButton 。 参数 button 用整数型来表示按下哪个键:0 左键, 1 右键, 2 滚轮键。

 

 

自定义轴(Axes)相关的输入函数:


                       1. public static bool GetButton(string buttonName);

              描述:

                         当由buttonName 定义的虚拟键保持按下状态时返回true。

                         只有当执行的事件触发一个动作时使用这个函数。当输入控制持续的变化时使用GetAxis

using UnityEngine;

using System.Collections;

 

public class ExampleClass : MonoBehaviour {

    publicGameObject projectile;

    public floatfireRate = 0.5F;

    private floatnextFire = 0.0F;

    void Update() {

        if(Input.GetButton("Fire1") && Time.time > nextFire) {

           nextFire = Time.time + fireRate;

           GameObject clone = Instantiate(projectile, transform.position,transform.rotation) as GameObject;

        }

    }

}

 

              2. public static bool GetButtonDown(string buttonName);

              描述:

                         当由buttonName定义的虚拟键被按下时返回true。

 

              3. public static boolGetButtonUp(string buttonName);

              描述:

                      当由buttonName定义的虚拟键被释放时返回true


            4. public static float GetAxis(string axisName);

             描述:

                           返回由axisName定义的虚拟轴的值。

                           如果是键盘或者操作杆,返回值在 -1 到 1 之间。如果是鼠标的移动,则返回值时敏感性(sensitivity)的倍数,其范围不是 -1 到 1。

 

总结: 如果要获得虚拟键的down , up, hold状态,使用的函数分别是 GetButtonDownGetButtonDown ,GetButtonUp, GetButton 。 如果要获得的值是动态的,则使用GetAxis。参数都是自定义的键名

   

 


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值