6.代码检测输入-手柄输入
6.1 知识点
获取当前手柄
- 获得当前手柄 最好判空
Gamepad gamePad = Gamepad.current;
if (gamePad == null)
return;
手柄摇杆
摇杆方向
- 左摇杆方向向量 右和上是正方向
print(gamePad.leftStick.ReadValue());
- 右摇杆方向向量
print(gamePad.rightStick.ReadValue());
摇杆按下
- 右摇杆 按下抬起长按相关
//gamePad.rightStickButton
- 左摇杆 按下抬起长按相关
if (gamePad.leftStickButton.wasPressedThisFrame) { } if (gamePad.leftStickButton.wasReleasedThisFrame) { } if (gamePad.leftStickButton.isPressed) { }
左摇杆
if (Gamepad.current.leftStickButton.wasPressedThisFrame)
{
print("左摇杆按下");
}
if (Gamepad.current.leftStickButton.wasReleasedThisFrame)
{
print("左摇杆抬起");
}
if (Gamepad.current.leftStickButton.isPressed)
{
print("左摇杆长按");
}
手柄方向键
对应手柄上4个方向键 上下左右
- gamePad.dpad.left
- gamePad.dpad.right
- gamePad.dpad.up
- gamePad.dpad.down
左键例子
if (gamePad.dpad.left.wasPressedThisFrame)
{
}
if (gamePad.dpad.left.wasReleasedThisFrame)
{
}
if (gamePad.dpad.left.isPressed)
{
}
- 左方向键监听
if (Gamepad.current.dpad.left.wasPressedThisFrame)
{
print("左方向键按下");
}
if (Gamepad.current.dpad.left.wasReleasedThisFrame)
{
print("左方向键抬起");
}
if (Gamepad.current.dpad.left.isPressed)
{
print("左方向键长按");
}
手柄右侧按键
通用
- Y、△ 北向按钮
- gamePad.buttonNorth
- A、X 南向按钮
- gamePad.buttonSouth
- X、□ 西向按钮
- gamePad.buttonWest
- B、○ 动向按钮
- gamePad.buttonEast
手柄右侧按钮 x ○ △ □ A B Y 按名字监听
- ○
- gamePad.circleButton
- △
- gamePad.triangleButton
- □
- gamePad.squareButton
- X
- gamePad.crossButton
- x
- gamePad.xButton
- a
- gamePad.aButton
- b
- gamePad.bButton
- Y
- gamePad.yButton
- ○
if (Gamepad.current.buttonNorth.wasPressedThisFrame)
{
print("上方按键 三角形键(PS)按下");
}
手柄中央按键
中央键
开始键
- gamePad.startButton
选择键
- gamePad.selectButton
if (Gamepad.current.startButton.wasPressedThisFrame)
{
print("开始键按下");
}
if (Gamepad.current.selectButton.wasPressedThisFrame)
{
print("选择键按下");
}
手柄肩部按键
左上右上 肩部键位
- 左右前方肩部键
- gamePad.leftShoulder
- gamePad.rightShoulder
- 左右前方肩部键
左右后方触发键
- gamePad.leftTrigger
- gamePad.rightTrigger
if (Gamepad.current.leftShoulder.wasPressedThisFrame)
{
print("左侧肩部前方键");
}
if (Gamepad.current.leftTrigger.wasPressedThisFrame)
{
print("左侧肩部后方键");
}
6.2 知识点代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Lesson06_代码检测输入_手柄输入 : MonoBehaviour
{
void Start()
{
#region 知识点一 获取当前手柄
//获得当前手柄 最好判空
Gamepad gamePad = Gamepad.current;
if (gamePad == null)
return;
#endregion
#region 知识点二 手柄摇杆
//摇杆方向
//左摇杆方向向量 右和上是正方向
print(gamePad.leftStick.ReadValue());
//右摇杆方向向量
print(gamePad.rightStick.ReadValue());
//摇杆按下
//右摇杆 按下抬起长按相关
//gamePad.rightStickButton
//左摇杆 按下抬起长按相关
if (gamePad.leftStickButton.wasPressedThisFrame)
{
}
if (gamePad.leftStickButton.wasReleasedThisFrame)
{
}
if (gamePad.leftStickButton.isPressed)
{
}
#endregion
#region 知识点三 手柄方向键
//对应手柄上4个方向键 上下左右
//gamePad.dpad.left
//gamePad.dpad.right
//gamePad.dpad.up
//gamePad.dpad.down
//左键例子
if (gamePad.dpad.left.wasPressedThisFrame)
{
}
if (gamePad.dpad.left.wasReleasedThisFrame)
{
}
if (gamePad.dpad.left.isPressed)
{
}
#endregion
#region 知识点四 手柄右侧按键
//通用
//Y、△ 北向按钮
//gamePad.buttonNorth
//A、X 南向按钮
//gamePad.buttonSouth
//X、□ 西向按钮
//gamePad.buttonWest
//B、○ 动向按钮
//gamePad.buttonEast
//wasPressedThisFrame
//wasReleasedThisFrame
//isPressed
//手柄右侧按钮 x ○ △ □ A B Y 按名字监听
//○
//gamePad.circleButton
//△
//gamePad.triangleButton
//□
//gamePad.squareButton
//X
//gamePad.crossButton
//x
//gamePad.xButton
//a
//gamePad.aButton
//b
//gamePad.bButton
//Y
//gamePad.yButton
#endregion
#region 知识点五 手柄中央按键
//中央键
//开始键
//gamePad.startButton
//选择键
//gamePad.selectButton
//wasPressedThisFrame
//wasReleasedThisFrame
//isPressed
#endregion
#region 知识点六 手柄肩部按键
//左上右上 肩部键位
//左右前方肩部键
//gamePad.leftShoulder
//gamePad.rightShoulder
//左右后方触发键
//gamePad.leftTrigger
//gamePad.rightTrigger
//wasPressedThisFrame
//wasReleasedThisFrame
//isPressed
#endregion
}
void Update()
{
#region 知识点二 手柄摇杆
//左摇杆
//print(Gamepad.current.leftStick.ReadValue());
//右摇杆
//print(gamePad.rightStick.ReadValue());
if (Gamepad.current.leftStickButton.wasPressedThisFrame)
{
print("左摇杆按下");
}
if (Gamepad.current.leftStickButton.wasReleasedThisFrame)
{
print("左摇杆抬起");
}
if (Gamepad.current.leftStickButton.isPressed)
{
print("左摇杆长按");
}
#endregion
#region 知识点三 手柄方向键
if (Gamepad.current.dpad.left.wasPressedThisFrame)
{
print("左方向键按下");
}
if (Gamepad.current.dpad.left.wasReleasedThisFrame)
{
print("左方向键抬起");
}
if (Gamepad.current.dpad.left.isPressed)
{
print("左方向键长按");
}
#endregion
#region 知识点四 手柄右侧按键
if (Gamepad.current.buttonNorth.wasPressedThisFrame)
{
print("上方按键 三角形键(PS)按下");
}
#endregion
#region 知识点五 手柄中央按键
if (Gamepad.current.startButton.wasPressedThisFrame)
{
print("开始键按下");
}
if (Gamepad.current.selectButton.wasPressedThisFrame)
{
print("选择键按下");
}
#endregion
#region 知识点六 手柄肩部按键
if (Gamepad.current.leftShoulder.wasPressedThisFrame)
{
print("左侧肩部前方键");
}
if (Gamepad.current.leftTrigger.wasPressedThisFrame)
{
print("左侧肩部后方键");
}
#endregion
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com