Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作

2019-12-30 10:56:17于丽

易采站长站为您分析Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作的相关资料,需要的朋友可以参考下

获取当前键盘按键,代码如下:

 

 
  1. using UnityEngine;  using System.Collections; 
  2. public class GetCurrentKey : MonoBehaviour {  KeyCode currentKey; 
  3. void Start ()  { 
  4. currentKey = KeyCode.Space;  } 
  5. void OnGUI()  { 
  6. if (Input.anyKeyDown)  { 
  7. Event e = Event.current;  if (e.isKey) 
  8. {  currentKey = e.keyCode; 
  9. Debug.Log("Current Key is : " + currentKey.ToString());  } 
  10. }  } 

下面给大家介绍Unity3D鼠标、键盘的基本操作

键盘:

GetKey 当通过名称指定的按键被用户按住时返回true

GetKeyDown 当用户按下指定名称的按键时的那一帧返回true。

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

GetAxis(“Horizontal")和GetAxis(“Verical”) 用方向键或WASD键来模拟-1到1的平滑输入

键盘判断:

If(Input.GetKeyDown(KeyCode.A)){//KeyCode表示包含键盘所有键

print(“按下A键”); } If(Input.GetKeyUp(KeyCode.D)){//当按D键松开时

print(“松开D键”); } If(Input.GetAxis(“Horizontal")){//当按下水平键时

print(“按下水平键”); } If(Input.GetKeyUp("Verical“)){当按下垂直键时

print(“按下垂直键”); }

鼠标:

GetButton 根据按钮名称返回true当对应的虚拟按钮被按住时。