正如您大概知道的那樣,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