unity3d中用incontrol检测输入设备切换
2017-07-07 10:31
701 查看
pc上同时插着手柄的情况下,巫师3的玩家一动键盘,操作提示立即变成键盘的,一动手柄,提示立马变成手柄的了。
我们的游戏用的incontrol插件做的多输入设备管理,也有这样的需求。我问过作者,没有直接的接口可以使用,需要用incontrol的两种事件组合着来做。
直接上代码吧,注释和清楚。
我们的游戏用的incontrol插件做的多输入设备管理,也有这样的需求。我问过作者,没有直接的接口可以使用,需要用incontrol的两种事件组合着来做。
直接上代码吧,注释和清楚。
/// /// 1.incontrol 里面的device不包括键盘鼠标,只包括各种外设手柄。 /// 要检测键盘鼠标和device之间的切换,只能用一个PlayerActionSet注册所有要触发切换的键, /// 然后监听它的PlayerActionSet.OnLastInputTypeChanged事件。 /// /// 2.手柄device之间的切换用InControl.InputManager.OnActiveDeviceChanged了。 /// /// 3.在系统上同时插多种手柄的情况下,以上两个事件可能都会触发, /// 所以需要在自身的update里面来检测变动,发送事件,保证事件的唯一性。 /// namespace Pathea.InputSolutionNs { public enum InputDevice { MouseKeyboard = 0, XboxJoystick, Ps4Joystick, Max } public class InputDeviceDetector : UpdateDestroySingleton { private class ControllerDetector:InControl.PlayerActionSet { public void Init() { InControl.PlayerAction a = CreatePlayerAction("ControllerDetector"); for (int i = (int)InControl.InputControlType.LeftStickUp; i <= (int)InControl.InputControlType.Action12; i++) { a.AddDefaultBinding((InControl.InputControlType)i); } for (int i = (int)InControl.Mouse.LeftButton; i <= (int)InControl.Mouse.Button9; i++) { a.AddDefaultBinding((InControl.Mouse)i); } for (int i = (int)InControl.Key.Shift; i <= (int)InControl.Key.CapsLock; i++) { a.AddDefaultBinding((InControl.Key)i); } } } private InputDevice curDevice = InputDevice.Max; private InputDevice nextDevice = InputDevice.MouseKeyboard; private ControllerDetector controllerDetector = new ControllerDetector(); public event System.Action OnDeviceChanged; public InputDevice CurDevice { get { return curDevice; } } private static bool IsPs4Joystick() { if (InControl.InputManager.ActiveDevice == null) { return false; } if (InControl.InputManager.ActiveDevice.DeviceStyle != InControl.InputDeviceStyle.PlayStation4) { return false; } return true; } private static bool IsXboxJoystick() { if (InControl.InputManager.ActiveDevice == null) { return false; } if (InControl.InputManager.ActiveDevice.DeviceStyle != InControl.InputDeviceStyle.XboxOne && InControl.InputManager.ActiveDevice.DeviceStyle != InControl.InputDeviceStyle.Xbox360) { return false; } return true; } protected override void Update() { if (curDevice != nextDevice) { curDevice = nextDevice; if (OnDeviceChanged != null) { OnDeviceChanged(curDevice); } } } protected override void OnInit() { base.OnInit(); controllerDetector.Init(); controllerDetector.OnLastInputTypeChanged += ControllerDetector_OnLastInputTypeChanged; InControl.InputManager.OnActiveDeviceChanged += InputManager_OnActiveDeviceChanged; } //键盘,鼠标,手柄类型之间切换会受到这个消息 private void ControllerDetector_OnLastInputTypeChanged(InControl.BindingSourceType obj) { if (obj != InControl.BindingSourceType.DeviceBindingSource) { SetNextInputDevice(InputDevice.MouseKeyboard); } else { CheckController(); } } //只有手柄之间切换会收到这个消息。 private void InputManager_OnActiveDeviceChanged(InControl.InputDevice obj) { CheckController(); } private void CheckController() { if (IsXboxJoystick()) { SetNextInputDevice(InputDevice.XboxJoystick); } else if(IsPs4Joystick()) { SetNextInputDevice(InputDevice.Ps4Joystick); } } private void SetNextInputDevice(InputDevice device) { nextDevice = device; } protected override void Destroy() { base.Destroy(); controllerDetector.OnLastInputTypeChanged -= ControllerDetector_OnLastInputTypeChanged; InControl.InputManager.OnActiveDeviceChanged -= InputManager_OnActiveDeviceChanged; } } }
相关文章推荐
- 02_Unity3D的输入(Input)——移动设备输入
- 使用Pygame检测在窗口中输入设备的动作
- 03_Unity3D的输入(Input)——移动设备重力感应
- Untiy检测各类设备输入
- 04_Unity3D的输入(Input)——移动设备方向
- unity3D rigidibody / collision /检测用户输入
- Android2.1 输入设备检测BUG修正
- DirectX11 输入设备——DirectInput检测鼠标、键盘状态
- UGUI -(unity3d 5)判断是否点击在UI 上 Bug,IsPointerOverGameObject()在移动输入模式检测失败
- 【Unity3D】利用物体碰撞检测、键盘输入处理完成平衡球游戏
- Android 实现密码输入框动态明文/密文切换显示效果
- unity3d input输入
- Linux输入子系统:输入设备编程指南 -- input-programming.txt .
- 检测设备是否支持Google Play服务
- udev(九)-- 写个程序检测我们的设备插拔
- js如何检测浏览设备类型
- 一种用于人脸检测的设备上的深度神经网络
- JQuery:在input中只能输入数字,并且不能复制粘贴,不能切换输入法
- ZigBee协议栈中终端设备的状态切换详解
- vim小技巧:不用切换到输入模式交换前后字符