U3D Input类之键位输入GetKey

  Input类中包含许多属性和方法,下面介绍一下和键盘输入有关的内容

一、有关接收键盘信息的属性

属性名 类型 属性类型 含义
anyKey bool get 当前是否按住任意键或鼠标按钮
anyKeyDown bool get 是否按下任意键
using UnityEngine;

public class Example : MonoBehaviour
{
    // Detects if any key has been pressed.

    void Update()
    {
        if (Input.anyKey)
        {
            Debug.Log("A key or mouse click has been detected");
        }
    }
}
using UnityEngine;

public class Example : MonoBehaviour
{
    // Detects if any key has been pressed down.

    void Update()
    {
        if (Input.anyKeyDown)
        {
            Debug.Log("A key or mouse click has been detected");
        }
    }
}

注:对于anyKeyDown,您应该从Update函数中轮询此变量,因为每个帧都会重置状态。在用户释放所有键/按钮并再次按下任何键/按钮之前,它不会返回true。

二、键盘按键的keyCode键码

  在网上看到别人关于键码的内容非常详细,这里就直接上链接了

  点击查看:keyCode键码大全

三、有关接收键盘信息的方法

  3.1  public static bool GetKey(KeyCode key)  

          参数:key———键盘上的某个键。

          返回值:bool———当通过名称指定的按键被用户按住时返回true 

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            print("up arrow key is held down");
        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            print("down arrow key is held down");
        }
    }
}

  它还有一个重载方法: public static bool GetKey(string name),它的参数为字符串

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey("up"))
        {
            print("up arrow key is held down");
        }

        if (Input.GetKey("down"))
        {
            print("down arrow key is held down");
        }
    }
}

  3.2  public static bool GetKeyDown(KeyCode key)

    当用户按下指定名称的按键期间的帧返回true。

    您需要从Update函数调用此函数,因为每个帧都会重置状态。在用户释放该键并再次按下该键之前,它不会返回true。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            print("space key was pressed");
        }
    }
}

    重载方法:public static bool GetKeyDown(string name)

  3.3  public static bool GetKeyUp(KeyCode key)

    在用户释放给定名字的按键的那一帧返回true。

    您需要从Update函数调用此函数,因为每个帧都会重置状态。在用户按下键并再次释放它之前,它不会返回true。

using UnityEngine;

public class Example : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            print("space key was released");
        }
    }
}

  重载方法:public static bool GetKeyUp(string name)

  下面例子演示了如何使物体随着按键的方向移动

void Update()
    {
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
            dir = Vector2.right;
        else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
            dir = Vector2.left;
        else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
            dir = Vector2.down;
        else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
            dir = Vector2.up;
    }
原文地址:https://www.cnblogs.com/forever-Ys/p/10374832.html