正如您大概知道的那样,Unity3D有可怕的内置输入系统,无法更改配置运行时,所以我决定编写基于SharpDX的自己的输入系统直接输入。我知道directInput不是官方的recomendet,但我喜欢它能够使用各种设备(比如我的Trust双头手柄GTX 28,Originaly为PSX仿真购买)。Unity3D中的SharpDx - 单击编辑器的其他窗口时,按钮不起作用
我使用类下面representate按钮对象
public class InputButton
{
public JoystickOffset button;
public Key key;
public int pressValue;
public int relaseValue;
public bool isJoystick;
public InputButton(JoystickOffset button, int pressValue, int relaseValue)
{
this.button = button;
this.pressValue = pressValue;
this.relaseValue = relaseValue;
isJoystick = true;
}
public InputButton(Key key, int pressValue, int relaseValue)
{
this.key = key;
this.pressValue = pressValue;
this.relaseValue = relaseValue;
isJoystick = false;
}
}
然后我更换统一的(顺便说一句非常可怕的方法)Input.GetKeyDown我自己的(如果你的名字的类一样更换统一的一个类它。我知道一定有人不喜欢使用静态的,但在这里我看到了非常benefical)
public static bool GetKeyDown(InputButton button)
{
bool pressed = false;
keyboard.Poll();
keyboardData = keyboard.GetBufferedData();
if (button.isJoystick == false)
{
foreach (var state in keyboardData)
{
if (state.Key == button.key && state.Value == button.pressValue)
{
pressed = true;
}
}
}
return pressed;
}
但是,一切都在我请Input.Initialize()从另一个类(清醒期间())。它看起来像这样:
public static void Initialize()
{
directInput = new DirectInput();
var joystickGuid = Guid.Empty;
foreach (var deviceInstance in directInput.GetDevices(SharpDX.DirectInput.DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly))
{
joystickGuid = deviceInstance.InstanceGuid;
}
if (joystickGuid == Guid.Empty)
{
foreach (var deviceInstance in directInput.GetDevices(SharpDX.DirectInput.DeviceType.Gamepad, DeviceEnumerationFlags.AttachedOnly))
{
joystickGuid = deviceInstance.InstanceGuid;
}
}
if (joystickGuid != Guid.Empty)
{
joystick = new Joystick(directInput, joystickGuid);
joystick.Properties.BufferSize = 128;
joystick.Acquire();
}
keyboard = new Keyboard(directInput);
keyboard.Properties.BufferSize = 128;
keyboard.Acquire();
}
现在的问题。当我在编辑器中点击游戏窗口外的任何东西时,按键不再响应。我检查了一切,并且directInput和键盘仍然在变量中。最有可能的问题是窗口的“焦点”,因为这个问题看起来像directInput实例或键盘会在游戏窗口失去焦点时立即断开连接(当窗口不活动时焦点丢失窗口,活动窗口不是“活动”但所谓的“聚焦“)。
有人知道为什么这个偶然发生以及如何修复它吗?
编辑:看起来像这个问题是以某种方式连接到窗口(S)。我有设置,我可以切换全屏运行时。只要我在全屏幕,它工作正常,但当我切换到窗口它停止工作。
谢谢。
-Garrom