键盘
每帧判断
void Update()
{
if (Keyboard.current.spaceKey.wasPressedThisFrame)
{
print("空格键");
}
if(Keyboard.current.dKey.wasReleasedThisFrame)
{
print("d键抬起");
}
if(Keyboard.current.spaceKey.isPressed)
{
print("空格按下");
}
if(Keyboard.current.anyKey.wasPressedThisFrame)
{
print("任意键按下");
}
}
绑定事件的方式
keyboard.onTextInput += (c) =>
{
print("通过Lambda表达式" + c);
};
keyboard.onTextInput += TestInput;
private void TestInput(char c)
{
print("通过函数监听" + c);
}
鼠标
每帧判断
void Update
{
if(Mouse.current.rightButton.wasPressedThisFrame)
{
print("鼠标右键按下");
}
if(Mouse.current.middleButton.wasPressedThisFrame)
{
print("鼠标中建按下");
}
if(Mouse.current.forwardButton.wasPressedThisFrame)
{
print("鼠标前键按下");
}
if(Mouse.current.backButton.wasPressedThisFrame)
{
print("鼠标后键按下");
}
//屏幕坐标(左下角为(0,0)
print(Mouse.current.position.ReadValue());
//两帧之间的偏移
print(Mouse.current.delta.ReadValue());
//滚轮
print(Mouse.current.scroll.ReadValue());
}
触摸屏
每帧判断
void Update
{
Touchscreen ts = Touchscreen.current;
if (ts == null)
{
return;
}
else
{
TouchControl tc = ts.touches[0];
if(tc.press.wasPressedThisFrame)
{
print("按下");
}
if(tc.press.wasReleasedThisFrame)
{
print("抬起");
}
if(tc.press.isPressed)
{
print("按住");
}
if(tc.tap.isPressed)
{
}
//点击次数
print(tc.tapCount);
//点击位置
print(tc.position.ReadValue());
//第一次接触位置
print(tc.startPosition.ReadValue());
//得到的范围
print(tc.radius.ReadValue());
//偏移位置
print(tc.delta.ReadValue());
//返回TouchPhase: None,Began,Moved,Ended,Canceled,Stationary
print(tc.phase.ReadValue());
//判断状态
UnityEngine.InputSystem.TouchPhase tp = tc.phase.ReadValue();
switch (tp)
{
//无
case UnityEngine.InputSystem.TouchPhase.None:
break;
//开始接触
case UnityEngine.InputSystem.TouchPhase.Began:
break;
//移动
case UnityEngine.InputSystem.TouchPhase.Moved:
break;
//结束
case UnityEngine.InputSystem.TouchPhase.Ended:
break;
//取消
case UnityEngine.InputSystem.TouchPhase.Canceled:
break;
//静止
case UnityEngine.InputSystem.TouchPhase.Stationary:
break;
}
}
手柄
Gamepad gamepad = Gamepad.current;
if(gamepad==null)
{
return;
}
Vector2 leftDir= gamepad.leftStick.ReadValue();
Vector2 rightDir= gamepad.rightStick.ReadValue();
//左摇杆按下抬起
if(Gamepad.current.leftStickButton.wasPressedThisFrame)
{
}
if (Gamepad.current.leftStickButton.wasReleasedThisFrame)
{
}
if (Gamepad.current.leftStickButton.isPressed)
{
}
//右摇杆按下抬起
if (Gamepad.current.rightStickButton.wasPressedThisFrame)
{
}
if (Gamepad.current.rightStickButton.wasReleasedThisFrame)
{
}
if (Gamepad.current.rightStickButton.isPressed)
{
}
if(Gamepad.current.dpad.left.wasPressedThisFrame)
{
}
if (Gamepad.current.dpad.left.wasReleasedThisFrame)
{
}
if (Gamepad.current.dpad.left.isPressed)
{
}
//右侧三角方块/XYAB按键
//Gamepad.current.buttonEast;
//Gamepad.current.buttonWest;
//Gamepad.current.buttonSouth;
//Gamepad.current.buttonEast;
if (Gamepad.current.buttonNorth.wasPressedThisFrame)
{
}
if (Gamepad.current.buttonNorth.wasReleasedThisFrame)
{
}
if (Gamepad.current.buttonNorth.isPressed)
{
}
//手柄中央键
if(Gamepad.current.startButton.wasPressedThisFrame)
{
}
if(Gamepad.current.selectButton.wasPressedThisFrame)
{
}
//肩键
if(Gamepad.current.leftShoulder.wasPressedThisFrame)
{
}
if (Gamepad.current.rightShoulder.wasPressedThisFrame)
{
}
if(Gamepad.current.leftTrigger.wasPressedThisFrame)
{
}
if(Gamepad.current.rightTrigger.wasPressedThisFrame)
{
}