Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Docs/VS_Scratch_Mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
/// <summary>
/// Input System が有効な環境でキー押下状態を確認し、押されていなければ false を返します。
/// Keyboard.current が取得できないケースにも対応し、例外を防ぎます。
/// </summary>
/// <param name="keyCode">判定対象の KeyCode。</param>
/// <returns>押されている場合は true。それ以外は false。</returns>
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;
}

/// <summary>
/// KeyCode を Input System の Key へ変換します。対応していないキーの場合は false を返します。
/// Scratch で重視される A-Z / 0-9 / 矢印キー / スペース / エンター / エスケープを網羅します。
/// </summary>
/// <param name="keyCode">変換元の KeyCode。</param>
/// <param name="inputSystemKey">変換に成功した場合の出力先。</param>
/// <returns>変換できた場合は true。それ以外は false。</returns>
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;
}
Comment on lines +105 to +109
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Fix Input System mapping for numeric keys

In the Input System path, numeric keycodes are converted via Key.Digit0 + offset, but Unity’s Key enum follows HID usage codes where Digit1Digit9 precede Digit0 (Digit0 is 39, Digit1 is 30, etc.). As a result, when ENABLE_INPUT_SYSTEM is defined, pressing 19 (and the keypad digits just below this block) resolves to Keyboard.current[Key.Enter/Escape/…] instead of the digit controls, so KeyIsPressedUnit reports false for those numbers even though they’re held. Only 0 maps correctly. Please map each digit explicitly (or base off Key.Digit1 with a special case for 0) so numeric keys work under the Input System.

Useful? React with 👍 / 👎.


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
}
}