对Unity Input 输入控制的深入探讨

Unity Input

Unity需要通过键盘、鼠标、手柄、摇杆来获取玩家对游戏的控制。你可以在 Input窗口设置按钮和虚拟轴,在代码中通过Input类监听玩家控制。游戏发布后玩家也可以在设置对话框中自行设置相关按键。
游戏设置对话框

添加及设置输入轴

Unity中打开Edit->Project Settings->Input面板,界面如下:
Input面板
此项目中只有10个输入轴,如果你想添加自己的输入轴,只需要更改Size属性。Size表示你所有输入轴的数量。接下来我们看下每个输入轴里面的各个属性。
Input Axis属性
* Name: 该输入轴的名称,相当于ID,在代码中我们可以根据该属性引用对应的输入轴;
* Descriptive Name: 游戏设置对话框中输入轴的名称,方便玩家设置对应按键;
* Descriptive Negative Name: 和Descriptive Name差不多,只是表示相反方向;
* Negative Button :对应于键盘、鼠标、手柄上的按键,并且是该控制的反向轴;
* Positive Button: 对应于键盘、鼠标、手柄上的按键,并且是该控制的正向轴;
* Alt Negative Button: Negative Button的备选按键;
* Alt Positive Button: Positive Button的备选按键;
Descriptive Name
* Gravity: 当不再按下此按键的时候,该输入轴的数值会以Gravity的速度变为默认值;
* Dead: 盲区。如果输入轴数值在此范围内,则以默认值替换;
* Sensitivity 当按下此按键的时候,该输入轴的数值从默认值以Sensitivity的速度向最大值增加,可视为Gravity属性的相反属性;
* Snap 如果选中,在按下一个按键的同时按下相反方向的按键输入轴数值立刻归零,比如在控制角色移动时,要想同时按下前进键和后退键角色站在原地不动时,可以勾选该属性;
* Invert: 如果选中,Negative Button和Positive Button的效果互换。如果要实现按下前进键时后退效果,可以勾选此项;
* Type 输入轴的输入设备类型,有Key or Mouse Button(j键盘按键、鼠标点击的等)、Mouse Movement(鼠标移动)、Joystick Axis(摇杆控制);
* Axis: 外设的输入轴,有x控制轴、y控制轴、摇杆及滚轮轴等;
* Joy Num: 使用的摇杆序号。
关于Gravity和Sensitivity我们来看一个例子:
Gravity与Sensitivity
以水平轴为例,在代码中我们这样写:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {

    public int Speed = 10;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");
        Debug.Log("x:" + x + ";y:" + y + ".");
    }

}

我们按下D键的一瞬间,x值从默认值0逐渐增大,在Sensitivity=10的情况下经过7个Uptate时间(一帧的时间)到达目标值1,在Sensitivity=20的情况下经过4个Uptate时间到达目标值1;在我们松开D键的一瞬间x会以Gravity的速度向0变化。

使用代码监听输入轴

我们会用到Input接口来进行监听。在上面的例子中我们使用GetAxis()方法来获取输入轴的数值,参数为输入轴的名字(在Input窗口中的Name)。Input中还有好多监听方法,每个方法都有不同的用处。

GetAxis()和GetAxisRaw()

二者用法相似,区别在于GetAxis()的返回值是从1到-1之间的数,而GetAxisRaw()的返回值只能是0、1、-1。也就是说相比与GetAxisRaw(),GetAxis()提供了按键按下、按键松开的瞬时值。

GetKey* ()和GetButton* ()

GetButton*()和GetKey*()都要求传入一个String类型的参数,GetKey*()要求该参数是预设的枚举类型参数,在KeyCode.cs中定义;而GetButton*()中形参既可以是KeyCode.cs中定义好的枚举类型参数,也可以使在Input面板中我们自定义的参数Name,所以我们一般使用GetButton* ()。
GetButton* ()主要有GetButton()、GetButtonDown()、GetButtonUp()。在按下Button的那一帧,GetButton()、GetButtonDown()返回值为true。之后在按住Button时,GetButton()返回值为true,GetButtonDown()的返回值为false。在松开Button的那一帧,GetButton()的返回值变为false,GetButtonUp()的返回值变为true。在之后的一帧中,GetButonUp()的返回值变false。

GetMouseButton*()

监听鼠标按键。传入参数为整数0、1、2,分别代表左键、右键、滚轮键。GetMouseButton* ()有GetMouseButtonDown()、GetMouseButton()、GetMouseButtonUp(),其用法和GetButton* ()相似。

GetTouch()

Unity提供了对移动设备监听的接口,包括对触摸屏、加速度计、地理位置的监听。Input类中的GetTouch()用于对触摸屏监听,其返回值为预定义的Touch结构体,代表在屏幕上的一次触摸,该结构组成如下:
Touch
一般我们经常会用到Touch里面的phase。phase是TouchPhase类,而TouchPhase是一个枚举类型,定义如下:

public enum TouchPhase
    {
        //     A finger touched the screen.
        Began = 0,
        //     A finger moved on the screen.
        Moved = 1,
        //     A finger is touching the screen but hasn't moved.
        Stationary = 2,
        //     A finger was lifted from the screen. This is the final phase of a touch.
        Ended = 3,
        //     The system cancelled tracking for the touch.
        Canceled = 4
    }

其中,Began表示手指刚刚触摸屏幕(一次Touch的开始);Moved表示手指在屏幕上移动;Stationary表示手指触摸屏幕且静止不动;Ended表示手指离开屏幕,Touch结束;Canceled表示系统取消了触控跟踪,比如说在触摸过程中用户将脸或者大腿贴在了屏幕上(显然没有哪款游戏是用脸或大腿触屏来控制的)。
Unity圣典给出了一个示例,用户在屏幕上点击,将射出一条光线:

var particle : GameObject;
function Update () {
    for (var touch : Touch in Input.touches) {
        if (touch.phase == TouchPhase.Began) {
            // Construct a ray from the current touch coordinates
            //从当前的触摸坐标建一条光线
            var ray = Camera.main.ScreenPointToRay (touch.position);
            if (Physics.Raycast (ray)) {
                // Create a particle if hit
                //如果触摸就创建一个例子
                Instantiate (particle, transform.position, transform.rotation);
            }
        }
    }
}
KeyCode.cs

在GetButton()和GetCode()调用中,需要传入KeyCode.cs中预定义的枚举类型参数。KeyCode.cs中所有枚举参数如下:

键值描述
NoneNot assigned (never returned as the result of a keystroke).
BackspaceThe backspace key.
DeleteThe forward delete key.
TabThe tab key.
ClearThe Clear key.
ReturnReturn key.
PausePause on PC machines.
EscapeEscape key.
SpaceSpace key.
Keypad0Numeric keypad 0.
Keypad1Numeric keypad 1.
Keypad2Numeric keypad 2.
Keypad3Numeric keypad 3.
Keypad4Numeric keypad 4.
Keypad5Numeric keypad 5.
Keypad6Numeric keypad 6.
Keypad7Numeric keypad 7.
Keypad8Numeric keypad 8.
Keypad9Numeric keypad 9.
KeypadPeriodNumeric keypad ‘.’.
KeypadDivideNumeric keypad ‘/’.
KeypadMultiplyNumeric keypad ‘*’.
KeypadMinusNumeric keypad ‘-‘.
KeypadPlusNumeric keypad ‘+’.
KeypadEnterNumeric keypad enter.
KeypadEqualsNumeric keypad ‘=’.
UpArrowUp arrow key.
DownArrowDown arrow key.
RightArrowRight arrow key.
LeftArrowLeft arrow key.
InsertInsert key key.
HomeHome key.
EndEnd key.
PageUpPage up.
PageDownPage down.
F1F1 function key.
F2F2 function key.
F3F3 function key.
F4F4 function key.
F5F5 function key.
F6F6 function key.
F7F7 function key.
F8F8 function key.
F9F9 function key.
F10F10 function key.
F11F11 function key.
F12F12 function key.
F13F13 function key.
F14F14 function key.
F15F15 function key.
Alpha0The ‘0’ key on the top of the alphanumeric keyboard.
Alpha1The ‘1’ key on the top of the alphanumeric keyboard.
Alpha2The ‘2’ key on the top of the alphanumeric keyboard.
Alpha3The ‘3’ key on the top of the alphanumeric keyboard.
Alpha4The ‘4’ key on the top of the alphanumeric keyboard.
Alpha5The ‘5’ key on the top of the alphanumeric keyboard.
Alpha6The ‘6’ key on the top of the alphanumeric keyboard.
Alpha7The ‘7’ key on the top of the alphanumeric keyboard.
Alpha8The ‘8’ key on the top of the alphanumeric keyboard.
Alpha9The ‘9’ key on the top of the alphanumeric keyboard.
ExclaimExclamation mark key ‘!’.
DoubleQuoteDouble quote key ‘”’.
HashHash key ‘#’.
DollarDollar sign key ‘$’.
AmpersandAmpersand key ‘&’.
QuoteQuote key ‘.
LeftParenLeft Parenthesis key ‘(‘.
RightParenRight Parenthesis key ‘)’.
AsteriskAsterisk key ‘*’.
PlusPlus key ‘+’.
CommaComma ‘,’ key.
MinusMinus ‘-’ key.
PeriodPeriod ‘.’ key.
SlashSlash ‘/’ key.
ColonColon ‘:’ key.
SemicolonSemicolon ‘;’ key.
LessLess than ‘<’ key.
EqualsEquals ‘=’ key.
GreaterGreater than ‘>’ key.
QuestionQuestion mark ‘?’ key.
AtAt key ‘@’.
LeftBracketLeft square bracket key ‘[‘.
BackslashBackslash key ‘\’.
RightBracketRight square bracket key ‘]’.
CaretCaret key ‘^’.
UnderscoreUnderscore ‘_’ key.
BackQuoteBack quote key ‘`’.
A‘a’ key.
B‘b’ key.
C‘c’ key.
D‘d’ key.
E‘e’ key.
F‘f’ key.
G‘g’ key.
H‘h’ key.
I‘i’ key.
J‘j’ key.
K‘k’ key.
L‘l’ key.
M‘m’ key.
N‘n’ key.
O‘o’ key.
P‘p’ key.
Q‘q’ key.
R‘r’ key.
S’s’ key.
T‘t’ key.
U‘u’ key.
V‘v’ key.
W‘w’ key.
X‘x’ key.
Y‘y’ key.
Z‘z’ key.
NumlockNumlock key.
CapsLockCapslock key.
ScrollLockScroll lock key.
RightShiftRight shift key.
LeftShiftLeft shift key.
RightControlRight Control key.
LeftControlLeft Control key.
RightAltRight Alt key.
LeftAltLeft Alt key.
LeftCommandLeft Command key.
LeftAppleLeft Command key.
LeftWindowsLeft Windows key.
RightCommandRight Command key.
RightAppleRight Command key.
RightWindowsRight Windows key.
AltGrAlt Gr key.
HelpHelp key.
PrintPrint key.
SysReqSys Req key.
BreakBreak key.
MenuMenu key.
Mouse0The Left (or primary) mouse button.
Mouse1Right mouse button (or secondary mouse button).
Mouse2Middle mouse button (or third button).
Mouse3Additional (fourth) mouse button.
Mouse4Additional (fifth) mouse button.
Mouse5Additional (or sixth) mouse button.
Mouse6Additional (or seventh) mouse button.
JoystickButton0Button 0 on any joystick.
JoystickButton1Button 1 on any joystick.
JoystickButton2Button 2 on any joystick.
JoystickButton3Button 3 on any joystick.
JoystickButton4Button 4 on any joystick.
JoystickButton5Button 5 on any joystick.
JoystickButton6Button 6 on any joystick.
JoystickButton7Button 7 on any joystick.
JoystickButton8Button 8 on any joystick.
JoystickButton9Button 9 on any joystick.
JoystickButton10Button 10 on any joystick.
JoystickButton11Button 11 on any joystick.
JoystickButton12Button 12 on any joystick.
JoystickButton13Button 13 on any joystick.
JoystickButton14Button 14 on any joystick.
JoystickButton15Button 15 on any joystick.
JoystickButton16Button 16 on any joystick.
JoystickButton17Button 17 on any joystick.
JoystickButton18Button 18 on any joystick.
JoystickButton19Button 19 on any joystick.
Joystick1Button0Button 0 on first joystick.
Joystick1Button1Button 1 on first joystick.
Joystick1Button2Button 2 on first joystick.
Joystick1Button3Button 3 on first joystick.
Joystick1Button4Button 4 on first joystick.
Joystick1Button5Button 5 on first joystick.
Joystick1Button6Button 6 on first joystick.
Joystick1Button7Button 7 on first joystick.
Joystick1Button8Button 8 on first joystick.
Joystick1Button9Button 9 on first joystick.
Joystick1Button10Button 10 on first joystick.
Joystick1Button11Button 11 on first joystick.
Joystick1Button12Button 12 on first joystick.
Joystick1Button13Button 13 on first joystick.
Joystick1Button14Button 14 on first joystick.
Joystick1Button15Button 15 on first joystick.
Joystick1Button16Button 16 on first joystick.
Joystick1Button17Button 17 on first joystick.
Joystick1Button18Button 18 on first joystick.
Joystick1Button19Button 19 on first joystick.
Joystick2Button0Button 0 on second joystick.
Joystick2Button1Button 1 on second joystick.
Joystick2Button2Button 2 on second joystick.
Joystick2Button3Button 3 on second joystick.
Joystick2Button4Button 4 on second joystick.
Joystick2Button5Button 5 on second joystick.
Joystick2Button6Button 6 on second joystick.
Joystick2Button7Button 7 on second joystick.
Joystick2Button8Button 8 on second joystick.
Joystick2Button9Button 9 on second joystick.
Joystick2Button10Button 10 on second joystick.
Joystick2Button11Button 11 on second joystick.
Joystick2Button12Button 12 on second joystick.
Joystick2Button13Button 13 on second joystick.
Joystick2Button14Button 14 on second joystick.
Joystick2Button15Button 15 on second joystick.
Joystick2Button16Button 16 on second joystick.
Joystick2Button17Button 17 on second joystick.
Joystick2Button18Button 18 on second joystick.
Joystick2Button19Button 19 on second joystick.
Joystick3Button0Button 0 on third joystick.
Joystick3Button1Button 1 on third joystick.
Joystick3Button2Button 2 on third joystick.
Joystick3Button3Button 3 on third joystick.
Joystick3Button4Button 4 on third joystick.
Joystick3Button5Button 5 on third joystick.
Joystick3Button6Button 6 on third joystick.
Joystick3Button7Button 7 on third joystick.
Joystick3Button8Button 8 on third joystick.
Joystick3Button9Button 9 on third joystick.
Joystick3Button10Button 10 on third joystick.
Joystick3Button11Button 11 on third joystick.
Joystick3Button12Button 12 on third joystick.
Joystick3Button13Button 13 on third joystick.
Joystick3Button14Button 14 on third joystick.
Joystick3Button15Button 15 on third joystick.
Joystick3Button16Button 16 on third joystick.
Joystick3Button17Button 17 on third joystick.
Joystick3Button18Button 18 on third joystick.
Joystick3Button19Button 19 on third joystick.
Joystick4Button0Button 0 on forth joystick.
Joystick4Button1Button 1 on forth joystick.
Joystick4Button2Button 2 on forth joystick.
Joystick4Button3Button 3 on forth joystick.
Joystick4Button4Button 4 on forth joystick.
Joystick4Button5Button 5 on forth joystick.
Joystick4Button6Button 6 on forth joystick.
Joystick4Button7Button 7 on forth joystick.
Joystick4Button8Button 8 on forth joystick.
Joystick4Button9Button 9 on forth joystick.
Joystick4Button10Button 10 on forth joystick.
Joystick4Button11Button 11 on forth joystick.
Joystick4Button12Button 12 on forth joystick.
Joystick4Button13Button 13 on forth joystick.
Joystick4Button14Button 14 on forth joystick.
Joystick4Button15Button 15 on forth joystick.
Joystick4Button16Button 16 on forth joystick.
Joystick4Button17Button 17 on forth joystick.
Joystick4Button18Button 18 on forth joystick.
Joystick4Button19Button 19 on forth joystick.
Joystick5Button0Button 0 on fifth joystick.
Joystick5Button1Button 1 on fifth joystick.
Joystick5Button2Button 2 on fifth joystick.
Joystick5Button3Button 3 on fifth joystick.
Joystick5Button4Button 4 on fifth joystick.
Joystick5Button5Button 5 on fifth joystick.
Joystick5Button6Button 6 on fifth joystick.
Joystick5Button7Button 7 on fifth joystick.
Joystick5Button8Button 8 on fifth joystick.
Joystick5Button9Button 9 on fifth joystick.
Joystick5Button10Button 10 on fifth joystick.
Joystick5Button11Button 11 on fifth joystick.
Joystick5Button12Button 12 on fifth joystick.
Joystick5Button13Button 13 on fifth joystick.
Joystick5Button14Button 14 on fifth joystick.
Joystick5Button15Button 15 on fifth joystick.
Joystick5Button16Button 16 on fifth joystick.
Joystick5Button17Button 17 on fifth joystick.
Joystick5Button18Button 18 on fifth joystick.
Joystick5Button19Button 19 on fifth joystick.
Joystick6Button0Button 0 on sixth joystick.
Joystick6Button1Button 1 on sixth joystick.
Joystick6Button2Button 2 on sixth joystick.
Joystick6Button3Button 3 on sixth joystick.
Joystick6Button4Button 4 on sixth joystick.
Joystick6Button5Button 5 on sixth joystick.
Joystick6Button6Button 6 on sixth joystick.
Joystick6Button7Button 7 on sixth joystick.
Joystick6Button8Button 8 on sixth joystick.
Joystick6Button9Button 9 on sixth joystick.
Joystick6Button10Button 10 on sixth joystick.
Joystick6Button11Button 11 on sixth joystick.
Joystick6Button12Button 12 on sixth joystick.
Joystick6Button13Button 13 on sixth joystick.
Joystick6Button14Button 14 on sixth joystick.
Joystick6Button15Button 15 on sixth joystick.
Joystick6Button16Button 16 on sixth joystick.
Joystick6Button17Button 17 on sixth joystick.
Joystick6Button18Button 18 on sixth joystick.
Joystick6Button19Button 19 on sixth joystick.
Joystick7Button0Button 0 on seventh joystick.
Joystick7Button1Button 1 on seventh joystick.
Joystick7Button2Button 2 on seventh joystick.
Joystick7Button3Button 3 on seventh joystick.
Joystick7Button4Button 4 on seventh joystick.
Joystick7Button5Button 5 on seventh joystick.
Joystick7Button6Button 6 on seventh joystick.
Joystick7Button7Button 7 on seventh joystick.
Joystick7Button8Button 8 on seventh joystick.
Joystick7Button9Button 9 on seventh joystick.
Joystick7Button10Button 10 on seventh joystick.
Joystick7Button11Button 11 on seventh joystick.
Joystick7Button12Button 12 on seventh joystick.
Joystick7Button13Button 13 on seventh joystick.
Joystick7Button14Button 14 on seventh joystick.
Joystick7Button15Button 15 on seventh joystick.
Joystick7Button16Button 16 on seventh joystick.
Joystick7Button17Button 17 on seventh joystick.
Joystick7Button18Button 18 on seventh joystick.
Joystick7Button19Button 19 on seventh joystick.
Joystick8Button0Button 0 on eighth joystick.
Joystick8Button1Button 1 on eighth joystick.
Joystick8Button2Button 2 on eighth joystick.
Joystick8Button3Button 3 on eighth joystick.
Joystick8Button4Button 4 on eighth joystick.
Joystick8Button5Button 5 on eighth joystick.
Joystick8Button6Button 6 on eighth joystick.
Joystick8Button7Button 7 on eighth joystick.
Joystick8Button8Button 8 on eighth joystick.
Joystick8Button9Button 9 on eighth joystick.
Joystick8Button10Button 10 on eighth joystick.
Joystick8Button11Button 11 on eighth joystick.
Joystick8Button12Button 12 on eighth joystick.
Joystick8Button13Button 13 on eighth joystick.
Joystick8Button14Button 14 on eighth joystick.
Joystick8Button15Button 15 on eighth joystick.
Joystick8Button16Button 16 on eighth joystick.
Joystick8Button17Button 17 on eighth joystick.
Joystick8Button18Button 18 on eighth joystick.
Joystick8Button19Button 19 on eighth joystick.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值