From 5298dad52f66ad4767ec9af3d3b2632115d42450 Mon Sep 17 00:00:00 2001 From: papacoder Date: Sat, 13 Dec 2025 14:16:10 +0900 Subject: [PATCH] fix: support input system in key predicate --- Docs/VS_Scratch_Mapping.md | 2 +- .../Units/ScratchUnits/InputPredicateUnits.cs | 90 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/Docs/VS_Scratch_Mapping.md b/Docs/VS_Scratch_Mapping.md index e4dca19..6cf49de 100644 --- a/Docs/VS_Scratch_Mapping.md +++ b/Docs/VS_Scratch_Mapping.md @@ -125,7 +125,7 @@ Scratch ブロックと FUnity 独自 Visual Scripting Unit の対応関係で ## 調べる | Scratch ブロック (日本語) | FUnity Unit クラス | UnitTitle | UnitCategory | 備考 | | --- | --- | --- | --- | --- | -| ○キーが押された? | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.KeyIsPressedUnit | ○キーが押された? | FUnity/Scratch/調べる | 押下中は true。定義: Runtime/.../InputPredicateUnits.cs | +| ○キーが押された? | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.KeyIsPressedUnit | ○キーが押された? | FUnity/Scratch/調べる | Input System 有効時は `Keyboard.current` で押下判定し、無効時は `UnityEngine.Input.GetKey` を使用。未対応キーや Keyboard が null の場合は false。定義: Runtime/.../InputPredicateUnits.cs | | マウスのx座標 | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.Probe.MouseXUnit | マウスのx座標 | FUnity/Scratch/調べる | ステージ中心原点でのマウス x 座標。定義: Runtime/.../Probe/MouseXUnit.cs | | マウスのy座標 | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.Probe.MouseYUnit | マウスのy座標 | FUnity/Scratch/調べる | ステージ中心原点でのマウス y 座標。定義: Runtime/.../Probe/MouseYUnit.cs | | マウスポインターに触れた? | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.TouchingMousePointerPredicateUnit | マウスポインターに触れた? | FUnity/Scratch/調べる | 俳優矩形とマウス座標を判定。定義: Runtime/.../TouchPredicates.cs | diff --git a/Runtime/Integrations/VisualScripting/Units/ScratchUnits/InputPredicateUnits.cs b/Runtime/Integrations/VisualScripting/Units/ScratchUnits/InputPredicateUnits.cs index 11e26ee..f7ac909 100644 --- a/Runtime/Integrations/VisualScripting/Units/ScratchUnits/InputPredicateUnits.cs +++ b/Runtime/Integrations/VisualScripting/Units/ScratchUnits/InputPredicateUnits.cs @@ -2,6 +2,9 @@ using UnityEngine; using FUnity.Runtime.Integrations.VisualScripting; using UInput = UnityEngine.Input; +#if ENABLE_INPUT_SYSTEM +using UnityEngine.InputSystem; +#endif namespace FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits { @@ -54,7 +57,94 @@ private bool Evaluate(Flow flow) return false; } +#if ENABLE_INPUT_SYSTEM + return IsPressedByInputSystem(keyCode); +#else return UInput.GetKey(keyCode); +#endif } + +#if ENABLE_INPUT_SYSTEM + /// + /// Input System が有効な環境でキー押下状態を確認し、押されていなければ false を返します。 + /// Keyboard.current が取得できないケースにも対応し、例外を防ぎます。 + /// + /// 判定対象の KeyCode。 + /// 押されている場合は true。それ以外は false。 + private static bool IsPressedByInputSystem(KeyCode keyCode) + { + var keyboard = Keyboard.current; + if (keyboard == null) + { + return false; + } + + if (!TryMapKeyCodeToInputSystemKey(keyCode, out var mappedKey)) + { + return false; + } + + return keyboard[mappedKey].isPressed; + } + + /// + /// KeyCode を Input System の Key へ変換します。対応していないキーの場合は false を返します。 + /// Scratch で重視される A-Z / 0-9 / 矢印キー / スペース / エンター / エスケープを網羅します。 + /// + /// 変換元の KeyCode。 + /// 変換に成功した場合の出力先。 + /// 変換できた場合は true。それ以外は false。 + private static bool TryMapKeyCodeToInputSystemKey(KeyCode keyCode, out Key inputSystemKey) + { + if (keyCode >= KeyCode.A && keyCode <= KeyCode.Z) + { + inputSystemKey = (Key)((int)Key.A + ((int)keyCode - (int)KeyCode.A)); + return true; + } + + if (keyCode >= KeyCode.Alpha0 && keyCode <= KeyCode.Alpha9) + { + inputSystemKey = (Key)((int)Key.Digit0 + ((int)keyCode - (int)KeyCode.Alpha0)); + return true; + } + + if (keyCode >= KeyCode.Keypad0 && keyCode <= KeyCode.Keypad9) + { + inputSystemKey = (Key)((int)Key.Numpad0 + ((int)keyCode - (int)KeyCode.Keypad0)); + return true; + } + + switch (keyCode) + { + case KeyCode.Space: + inputSystemKey = Key.Space; + return true; + case KeyCode.LeftArrow: + inputSystemKey = Key.LeftArrow; + return true; + case KeyCode.RightArrow: + inputSystemKey = Key.RightArrow; + return true; + case KeyCode.UpArrow: + inputSystemKey = Key.UpArrow; + return true; + case KeyCode.DownArrow: + inputSystemKey = Key.DownArrow; + return true; + case KeyCode.Return: + inputSystemKey = Key.Enter; + return true; + case KeyCode.KeypadEnter: + inputSystemKey = Key.NumpadEnter; + return true; + case KeyCode.Escape: + inputSystemKey = Key.Escape; + return true; + default: + inputSystemKey = default; + return false; + } + } +#endif } }