接入 Hud 输入提示并完善 InputModule 提示映射
This commit is contained in:
parent
e5630f0896
commit
21d0b410b6
|
|
@ -93,3 +93,4 @@ Assets/GameMain/Configs/ResourceBuilder.xml
|
||||||
/.vscode
|
/.vscode
|
||||||
/openspec/changes/archive
|
/openspec/changes/archive
|
||||||
/.omc
|
/.omc
|
||||||
|
/.claude
|
||||||
|
|
|
||||||
|
|
@ -2,5 +2,6 @@ namespace SepCore.UI
|
||||||
{
|
{
|
||||||
public class HudContext : UIContext
|
public class HudContext : UIContext
|
||||||
{
|
{
|
||||||
|
public string MovePrompt { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
using Cysharp.Threading.Tasks;
|
using Cysharp.Threading.Tasks;
|
||||||
using SepCore.Definition;
|
using SepCore.Definition;
|
||||||
|
using SepCore.InputModule;
|
||||||
using UnityGameFramework.Runtime;
|
using UnityGameFramework.Runtime;
|
||||||
|
|
||||||
namespace SepCore.UI
|
namespace SepCore.UI
|
||||||
|
|
@ -13,9 +14,32 @@ namespace SepCore.UI
|
||||||
form.RefreshUI(context);
|
form.RefreshUI(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void SubscribeCustomEvents()
|
||||||
|
{
|
||||||
|
GameEntry.InputModule.DeviceKindChanged += OnDeviceKindChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void UnsubscribeCustomEvents()
|
||||||
|
{
|
||||||
|
GameEntry.InputModule.DeviceKindChanged -= OnDeviceKindChanged;
|
||||||
|
}
|
||||||
|
|
||||||
private static HudContext BuildHudContext()
|
private static HudContext BuildHudContext()
|
||||||
{
|
{
|
||||||
return new HudContext();
|
return new HudContext
|
||||||
|
{
|
||||||
|
MovePrompt = BuildPromptContext(InputActionId.Move)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string BuildPromptContext(InputActionId actionId)
|
||||||
|
{
|
||||||
|
if (GameEntry.InputModule == null || !GameEntry.InputModule.TryGetPrompt(actionId, out InputPrompt prompt))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return InputPromptTextUtility.BuildTmpText(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async UniTask OpenUIAsync(object userData = null, float timeout = 30f)
|
public override async UniTask OpenUIAsync(object userData = null, float timeout = 30f)
|
||||||
|
|
@ -33,5 +57,16 @@ namespace SepCore.UI
|
||||||
{
|
{
|
||||||
Log.Info("HudFormController doesn't need UseCase");
|
Log.Info("HudFormController doesn't need UseCase");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnDeviceKindChanged(InputDeviceKind deviceKind)
|
||||||
|
{
|
||||||
|
if (Context == null || Form == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Context.MovePrompt = BuildPromptContext(InputActionId.Move);
|
||||||
|
Form.RefreshMovePrompt(Context.MovePrompt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,8 +36,26 @@ namespace SepCore.UI
|
||||||
[SerializeField] private TMP_Text _enemyCountText;
|
[SerializeField] private TMP_Text _enemyCountText;
|
||||||
private EnemyManagerComponent _enemy;
|
private EnemyManagerComponent _enemy;
|
||||||
|
|
||||||
|
[SerializeField] private GameObject _movePromptRoot;
|
||||||
|
[SerializeField] private TMP_Text _movePromptText;
|
||||||
|
|
||||||
public void RefreshUI(HudContext hudContext)
|
public void RefreshUI(HudContext hudContext)
|
||||||
{
|
{
|
||||||
|
RefreshMovePrompt(hudContext?.MovePrompt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RefreshMovePrompt(string prompt)
|
||||||
|
{
|
||||||
|
bool visible = !string.IsNullOrEmpty(prompt);
|
||||||
|
if (_movePromptRoot != null)
|
||||||
|
{
|
||||||
|
_movePromptRoot.SetActive(visible);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_movePromptText != null)
|
||||||
|
{
|
||||||
|
_movePromptText.text = prompt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region FSM
|
#region FSM
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,142 @@ MonoBehaviour:
|
||||||
m_FillOrigin: 0
|
m_FillOrigin: 0
|
||||||
m_UseSpriteMesh: 0
|
m_UseSpriteMesh: 0
|
||||||
m_PixelsPerUnitMultiplier: 1
|
m_PixelsPerUnitMultiplier: 1
|
||||||
|
--- !u!1 &1181223948078793352
|
||||||
|
GameObject:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
serializedVersion: 6
|
||||||
|
m_Component:
|
||||||
|
- component: {fileID: 844544797351374585}
|
||||||
|
- component: {fileID: 8628784289326000741}
|
||||||
|
- component: {fileID: 4622934279017998176}
|
||||||
|
m_Layer: 5
|
||||||
|
m_Name: InputPrompt
|
||||||
|
m_TagString: Untagged
|
||||||
|
m_Icon: {fileID: 0}
|
||||||
|
m_NavMeshLayer: 0
|
||||||
|
m_StaticEditorFlags: 0
|
||||||
|
m_IsActive: 1
|
||||||
|
--- !u!224 &844544797351374585
|
||||||
|
RectTransform:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 1181223948078793352}
|
||||||
|
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||||
|
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||||
|
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
|
m_ConstrainProportionsScale: 0
|
||||||
|
m_Children: []
|
||||||
|
m_Father: {fileID: 5845023051097607336}
|
||||||
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
|
m_AnchorMin: {x: 1, y: 0}
|
||||||
|
m_AnchorMax: {x: 1, y: 0}
|
||||||
|
m_AnchoredPosition: {x: -50, y: 100}
|
||||||
|
m_SizeDelta: {x: 600, y: 150}
|
||||||
|
m_Pivot: {x: 1, y: 0.5}
|
||||||
|
--- !u!222 &8628784289326000741
|
||||||
|
CanvasRenderer:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 1181223948078793352}
|
||||||
|
m_CullTransparentMesh: 1
|
||||||
|
--- !u!114 &4622934279017998176
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 1181223948078793352}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
m_Material: {fileID: 0}
|
||||||
|
m_Color: {r: 1, g: 1, b: 1, a: 1}
|
||||||
|
m_RaycastTarget: 1
|
||||||
|
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
|
||||||
|
m_Maskable: 1
|
||||||
|
m_OnCullStateChanged:
|
||||||
|
m_PersistentCalls:
|
||||||
|
m_Calls: []
|
||||||
|
m_text: "<sprite name=\"keyboard_w\"><sprite name=\"keyboard_a\"><sprite name=\"keyboard_s\"><sprite
|
||||||
|
name=\"keyboard_d\">\u79FB\u52A8"
|
||||||
|
m_isRightToLeft: 0
|
||||||
|
m_fontAsset: {fileID: 11400000, guid: 99d811b0183246646a2ce8df996f4bca, type: 2}
|
||||||
|
m_sharedMaterial: {fileID: -1106088975554028259, guid: 99d811b0183246646a2ce8df996f4bca,
|
||||||
|
type: 2}
|
||||||
|
m_fontSharedMaterials: []
|
||||||
|
m_fontMaterial: {fileID: 0}
|
||||||
|
m_fontMaterials: []
|
||||||
|
m_fontColor32:
|
||||||
|
serializedVersion: 2
|
||||||
|
rgba: 4294967295
|
||||||
|
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
|
||||||
|
m_enableVertexGradient: 0
|
||||||
|
m_colorMode: 3
|
||||||
|
m_fontColorGradient:
|
||||||
|
topLeft: {r: 1, g: 1, b: 1, a: 1}
|
||||||
|
topRight: {r: 1, g: 1, b: 1, a: 1}
|
||||||
|
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
|
||||||
|
bottomRight: {r: 1, g: 1, b: 1, a: 1}
|
||||||
|
m_fontColorGradientPreset: {fileID: 0}
|
||||||
|
m_spriteAsset: {fileID: 0}
|
||||||
|
m_tintAllSprites: 0
|
||||||
|
m_StyleSheet: {fileID: 0}
|
||||||
|
m_TextStyleHashCode: -1183493901
|
||||||
|
m_overrideHtmlColors: 0
|
||||||
|
m_faceColor:
|
||||||
|
serializedVersion: 2
|
||||||
|
rgba: 4294967295
|
||||||
|
m_fontSize: 80
|
||||||
|
m_fontSizeBase: 80
|
||||||
|
m_fontWeight: 400
|
||||||
|
m_enableAutoSizing: 0
|
||||||
|
m_fontSizeMin: 18
|
||||||
|
m_fontSizeMax: 100
|
||||||
|
m_fontStyle: 0
|
||||||
|
m_HorizontalAlignment: 4
|
||||||
|
m_VerticalAlignment: 512
|
||||||
|
m_textAlignment: 65535
|
||||||
|
m_characterSpacing: 0
|
||||||
|
m_wordSpacing: 0
|
||||||
|
m_lineSpacing: 0
|
||||||
|
m_lineSpacingMax: 0
|
||||||
|
m_paragraphSpacing: 0
|
||||||
|
m_charWidthMaxAdj: 0
|
||||||
|
m_enableWordWrapping: 1
|
||||||
|
m_wordWrappingRatios: 0.4
|
||||||
|
m_overflowMode: 0
|
||||||
|
m_linkedTextComponent: {fileID: 0}
|
||||||
|
parentLinkedComponent: {fileID: 0}
|
||||||
|
m_enableKerning: 1
|
||||||
|
m_enableExtraPadding: 0
|
||||||
|
checkPaddingRequired: 0
|
||||||
|
m_isRichText: 1
|
||||||
|
m_parseCtrlCharacters: 1
|
||||||
|
m_isOrthographic: 1
|
||||||
|
m_isCullingEnabled: 0
|
||||||
|
m_horizontalMapping: 0
|
||||||
|
m_verticalMapping: 0
|
||||||
|
m_uvLineOffset: 0
|
||||||
|
m_geometrySortingOrder: 0
|
||||||
|
m_IsTextObjectScaleStatic: 0
|
||||||
|
m_VertexBufferAutoSizeReduction: 0
|
||||||
|
m_useMaxVisibleDescender: 1
|
||||||
|
m_pageToDisplay: 1
|
||||||
|
m_margin: {x: 0, y: 0, z: 0, w: 0}
|
||||||
|
m_isUsingLegacyAnimationComponent: 0
|
||||||
|
m_isVolumetricText: 0
|
||||||
|
m_hasFontAssetChanged: 0
|
||||||
|
m_baseMaterial: {fileID: 0}
|
||||||
|
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
|
||||||
--- !u!1 &1588863174275057922
|
--- !u!1 &1588863174275057922
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|
@ -798,6 +934,7 @@ RectTransform:
|
||||||
- {fileID: 2701155363628883645}
|
- {fileID: 2701155363628883645}
|
||||||
- {fileID: 5367559285010484074}
|
- {fileID: 5367559285010484074}
|
||||||
- {fileID: 716378031281921353}
|
- {fileID: 716378031281921353}
|
||||||
|
- {fileID: 844544797351374585}
|
||||||
m_Father: {fileID: 0}
|
m_Father: {fileID: 0}
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
m_AnchorMin: {x: 0, y: 0}
|
m_AnchorMin: {x: 0, y: 0}
|
||||||
|
|
@ -825,6 +962,8 @@ MonoBehaviour:
|
||||||
_coinText: {fileID: 2927128461236740550}
|
_coinText: {fileID: 2927128461236740550}
|
||||||
_levelTimeLeftText: {fileID: 4547091306916251724}
|
_levelTimeLeftText: {fileID: 4547091306916251724}
|
||||||
_enemyCountText: {fileID: 1716982582738894949}
|
_enemyCountText: {fileID: 1716982582738894949}
|
||||||
|
_movePromptRoot: {fileID: 1181223948078793352}
|
||||||
|
_movePromptText: {fileID: 4622934279017998176}
|
||||||
--- !u!1 &5990189277179061188
|
--- !u!1 &5990189277179061188
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,61 @@
|
||||||
"action": "Move",
|
"action": "Move",
|
||||||
"isComposite": false,
|
"isComposite": false,
|
||||||
"isPartOfComposite": true
|
"isPartOfComposite": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "2D Vector",
|
||||||
|
"id": "61c92933-75b8-40e0-9171-5ccae6ba5298",
|
||||||
|
"path": "2DVector",
|
||||||
|
"interactions": "",
|
||||||
|
"processors": "",
|
||||||
|
"groups": "",
|
||||||
|
"action": "Move",
|
||||||
|
"isComposite": true,
|
||||||
|
"isPartOfComposite": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "up",
|
||||||
|
"id": "079c3966-b795-40d2-9400-124ac96eaf07",
|
||||||
|
"path": "<Gamepad>/leftStick/up",
|
||||||
|
"interactions": "",
|
||||||
|
"processors": "",
|
||||||
|
"groups": "",
|
||||||
|
"action": "Move",
|
||||||
|
"isComposite": false,
|
||||||
|
"isPartOfComposite": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "down",
|
||||||
|
"id": "d9e7829c-e750-467d-b073-a4e2cd508d61",
|
||||||
|
"path": "<Gamepad>/leftStick/down",
|
||||||
|
"interactions": "",
|
||||||
|
"processors": "",
|
||||||
|
"groups": "",
|
||||||
|
"action": "Move",
|
||||||
|
"isComposite": false,
|
||||||
|
"isPartOfComposite": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "left",
|
||||||
|
"id": "d14fc289-fcf1-482a-9d88-d116a702705c",
|
||||||
|
"path": "<Gamepad>/leftStick/left",
|
||||||
|
"interactions": "",
|
||||||
|
"processors": "",
|
||||||
|
"groups": "",
|
||||||
|
"action": "Move",
|
||||||
|
"isComposite": false,
|
||||||
|
"isPartOfComposite": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "right",
|
||||||
|
"id": "3f06f4d3-6d40-4eba-8e0e-766a8a6d7788",
|
||||||
|
"path": "<Gamepad>/leftStick/right",
|
||||||
|
"interactions": "",
|
||||||
|
"processors": "",
|
||||||
|
"groups": "",
|
||||||
|
"action": "Move",
|
||||||
|
"isComposite": false,
|
||||||
|
"isPartOfComposite": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
using SepCore.InputModule;
|
||||||
|
|
||||||
|
namespace SepCore.UI
|
||||||
|
{
|
||||||
|
public static class InputPromptTextUtility
|
||||||
|
{
|
||||||
|
public static string BuildTmpText(InputPrompt prompt)
|
||||||
|
{
|
||||||
|
string spriteText = BuildSpriteTags(prompt.SpriteName);
|
||||||
|
if (string.IsNullOrEmpty(spriteText))
|
||||||
|
{
|
||||||
|
return prompt.TextLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
return string.IsNullOrEmpty(prompt.TextLabel)
|
||||||
|
? spriteText
|
||||||
|
: $"{spriteText} {prompt.TextLabel}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string BuildSpriteTags(string spriteNames)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(spriteNames))
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
string[] names = spriteNames.Split('|');
|
||||||
|
string result = string.Empty;
|
||||||
|
for (int i = 0; i < names.Length; i++)
|
||||||
|
{
|
||||||
|
string name = names[i].Trim();
|
||||||
|
if (string.IsNullOrEmpty(name))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(result))
|
||||||
|
{
|
||||||
|
result += " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
result += $"<sprite name=\"{name}\">";
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 314a19cffa89e7a448d576d26d8787bf
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
{
|
{
|
||||||
"reference": "GUID:73a59a40d41e9ce48b3e0515aeec4217"
|
"reference": "GUID:0e1d182005e0ae647ab3fa40f5492dbb"
|
||||||
}
|
}
|
||||||
|
|
@ -87,7 +87,7 @@ namespace SepCore.InputModule.Runtime
|
||||||
Add(InputActionId.Navigate, InputDeviceKind.KeyboardMouse, "WASD");
|
Add(InputActionId.Navigate, InputDeviceKind.KeyboardMouse, "WASD");
|
||||||
Add(InputActionId.Confirm, InputDeviceKind.KeyboardMouse, "Enter", "keyboard_enter");
|
Add(InputActionId.Confirm, InputDeviceKind.KeyboardMouse, "Enter", "keyboard_enter");
|
||||||
Add(InputActionId.Cancel, InputDeviceKind.KeyboardMouse, "Esc", "keyboard_esc");
|
Add(InputActionId.Cancel, InputDeviceKind.KeyboardMouse, "Esc", "keyboard_esc");
|
||||||
Add(InputActionId.Move, InputDeviceKind.KeyboardMouse, "WASD");
|
Add(InputActionId.Move, InputDeviceKind.KeyboardMouse, "移动", "keyboard_w|keyboard_a|keyboard_s|keyboard_d");
|
||||||
Add(InputActionId.Sprint, InputDeviceKind.KeyboardMouse, "L Shift", "keyboard_shift");
|
Add(InputActionId.Sprint, InputDeviceKind.KeyboardMouse, "L Shift", "keyboard_shift");
|
||||||
Add(InputActionId.Interact, InputDeviceKind.KeyboardMouse, "E", "keyboard_e");
|
Add(InputActionId.Interact, InputDeviceKind.KeyboardMouse, "E", "keyboard_e");
|
||||||
}
|
}
|
||||||
|
|
@ -95,10 +95,10 @@ namespace SepCore.InputModule.Runtime
|
||||||
private void PopulateGamepad()
|
private void PopulateGamepad()
|
||||||
{
|
{
|
||||||
Add(InputActionId.Pause, InputDeviceKind.Gamepad, "Menu", "gamepad_menu");
|
Add(InputActionId.Pause, InputDeviceKind.Gamepad, "Menu", "gamepad_menu");
|
||||||
Add(InputActionId.Navigate, InputDeviceKind.Gamepad, "L Stick", "gamepad_full_ls");
|
Add(InputActionId.Navigate, InputDeviceKind.Gamepad, "L Stick", "gamepad_ls");
|
||||||
Add(InputActionId.Confirm, InputDeviceKind.Gamepad, "A", "gamepad_a");
|
Add(InputActionId.Confirm, InputDeviceKind.Gamepad, "A", "gamepad_a");
|
||||||
Add(InputActionId.Cancel, InputDeviceKind.Gamepad, "B", "gamepad_b");
|
Add(InputActionId.Cancel, InputDeviceKind.Gamepad, "B", "gamepad_b");
|
||||||
Add(InputActionId.Move, InputDeviceKind.Gamepad, "L Stick", "gamepad_full_ls");
|
Add(InputActionId.Move, InputDeviceKind.Gamepad, "移动", "gamepad_ls");
|
||||||
Add(InputActionId.Sprint, InputDeviceKind.Gamepad, "L3", "gamepad_press_ls");
|
Add(InputActionId.Sprint, InputDeviceKind.Gamepad, "L3", "gamepad_press_ls");
|
||||||
Add(InputActionId.Interact, InputDeviceKind.Gamepad, "A", "gamepad_a");
|
Add(InputActionId.Interact, InputDeviceKind.Gamepad, "A", "gamepad_a");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,8 @@ namespace SepCore.InputModule.Tests
|
||||||
{
|
{
|
||||||
_map.TryGetPrompt(InputActionId.Move, InputDeviceKind.KeyboardMouse, out InputPrompt prompt);
|
_map.TryGetPrompt(InputActionId.Move, InputDeviceKind.KeyboardMouse, out InputPrompt prompt);
|
||||||
|
|
||||||
Assert.That(prompt.TextLabel, Is.EqualTo("WASD"));
|
Assert.That(prompt.TextLabel, Is.EqualTo("移动"));
|
||||||
|
Assert.That(prompt.SpriteName, Is.EqualTo("keyboard_w|keyboard_a|keyboard_s|keyboard_d"));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|
|
||||||
|
|
@ -589,7 +589,7 @@ GameEntry.InputModule.RegisterListener(InputActionId.Confirm, _ =>
|
||||||
|
|
||||||
- `InputPrompt`(Base 层 readonly struct):承载提示数据
|
- `InputPrompt`(Base 层 readonly struct):承载提示数据
|
||||||
- `TextLabel`:文本标签(如 `”A”`, `”Enter”`, `”E”`)
|
- `TextLabel`:文本标签(如 `”A”`, `”Enter”`, `”E”`)
|
||||||
- `SpriteName`:可选的 sprite 资源键(项目自定义,模块不管理 sprite 资源)
|
- `SpriteName`:可选的 sprite 资源键(项目自定义,模块不管理 sprite 资源);多个 sprite 可用 `|` 分隔,如 `keyboard_w|keyboard_a|keyboard_s|keyboard_d`
|
||||||
- `HasSprite` / `IsValid`:便捷检查属性
|
- `HasSprite` / `IsValid`:便捷检查属性
|
||||||
- `IInputPromptMap`(Base 层接口):项目可自定义实现
|
- `IInputPromptMap`(Base 层接口):项目可自定义实现
|
||||||
- `TryGetPrompt(InputActionId, InputDeviceKind, out InputPrompt)`:查找提示
|
- `TryGetPrompt(InputActionId, InputDeviceKind, out InputPrompt)`:查找提示
|
||||||
|
|
@ -623,6 +623,21 @@ map.TryGetPrompt(InputActionId.Confirm, InputDeviceKind.Gamepad, out InputPrompt
|
||||||
GameEntry.InputModule.PromptMap = new MyPlayStationPromptMap();
|
GameEntry.InputModule.PromptMap = new MyPlayStationPromptMap();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**TMP 文本工具:**
|
||||||
|
|
||||||
|
`Assets/Plugins/InputModule/Presentation/InputPromptTextUtility.cs` 提供了将 `InputPrompt` 转成 TMP 文本的工具:
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
if (GameEntry.InputModule.TryGetPrompt(InputActionId.Move, out InputPrompt prompt))
|
||||||
|
{
|
||||||
|
_movePromptText.text = InputPromptTextUtility.BuildTmpText(prompt);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `BuildTmpText(InputPrompt)`:输出 `"<sprite name=\"keyboard_w\"> <sprite name=\"keyboard_a\"> ... 移动"` 这类可直接给 `TMP_Text.text` 的字符串
|
||||||
|
- `BuildSpriteTags(string)`:只把 `SpriteName` 转成 TMP sprite tag
|
||||||
|
- `SpriteName` 使用 `|` 分隔时会输出多个 sprite tag;单个 sprite 仍按原方式输出
|
||||||
|
|
||||||
**自定义 PromptMap 示例:**
|
**自定义 PromptMap 示例:**
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
|
|
@ -651,30 +666,70 @@ public sealed class PlayStationPromptMap : IInputPromptMap
|
||||||
|
|
||||||
**触发器:** `DeviceKindChanged` 事件。无需单独的 `PromptsChanged` 事件。
|
**触发器:** `DeviceKindChanged` 事件。无需单独的 `PromptsChanged` 事件。
|
||||||
|
|
||||||
**消费模式:**
|
**推荐接入现有 UI 架构:**
|
||||||
|
|
||||||
|
当前项目 UI 采用 `Controller -> Context -> Form`,输入提示也应沿用这条数据流:
|
||||||
|
|
||||||
|
- Controller:监听 `GameEntry.InputModule.DeviceKindChanged`,查询 `TryGetPrompt()`,用 `InputPromptTextUtility.BuildTmpText()` 生成 TMP 文本
|
||||||
|
- Context:保存当前 UI 需要展示的提示字符串,如 `MovePrompt`、`ConfirmPrompt`、`CancelPrompt`
|
||||||
|
- Form:只负责展示,不直接判断设备类型,也不直接查询 InputModule
|
||||||
|
|
||||||
|
**Hud 示例:**
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
private Action<InputDeviceKind> _onDeviceChanged;
|
public class HudContext : UIContext
|
||||||
|
|
||||||
private void OnEnable()
|
|
||||||
{
|
{
|
||||||
_onDeviceChanged = _ => RefreshPrompts();
|
public string MovePrompt { get; set; }
|
||||||
GameEntry.InputModule.DeviceKindChanged += _onDeviceChanged;
|
}
|
||||||
RefreshPrompts(); // 初始状态
|
```
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
private static HudContext BuildHudContext()
|
||||||
|
{
|
||||||
|
return new HudContext
|
||||||
|
{
|
||||||
|
MovePrompt = BuildPromptText(InputActionId.Move)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDisable()
|
private static string BuildPromptText(InputActionId actionId)
|
||||||
{
|
{
|
||||||
GameEntry.InputModule.DeviceKindChanged -= _onDeviceChanged;
|
if (GameEntry.InputModule == null || !GameEntry.InputModule.TryGetPrompt(actionId, out InputPrompt prompt))
|
||||||
_onDeviceChanged = null;
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return InputPromptTextUtility.BuildTmpText(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RefreshPrompts()
|
protected override void SubscribeCustomEvents()
|
||||||
{
|
{
|
||||||
if (GameEntry.InputModule.TryGetPrompt(InputActionId.Interact, out InputPrompt p))
|
GameEntry.InputModule.DeviceKindChanged += OnDeviceKindChanged;
|
||||||
_interactLabel.text = p.TextLabel;
|
}
|
||||||
if (GameEntry.InputModule.TryGetPrompt(InputActionId.Cancel, out InputPrompt c))
|
|
||||||
_cancelLabel.text = c.TextLabel;
|
protected override void UnsubscribeCustomEvents()
|
||||||
|
{
|
||||||
|
GameEntry.InputModule.DeviceKindChanged -= OnDeviceKindChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDeviceKindChanged(InputDeviceKind deviceKind)
|
||||||
|
{
|
||||||
|
if (Context == null || Form == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Context.MovePrompt = BuildPromptText(InputActionId.Move);
|
||||||
|
Form.RefreshMovePrompt(Context.MovePrompt);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public void RefreshMovePrompt(string prompt)
|
||||||
|
{
|
||||||
|
bool visible = !string.IsNullOrEmpty(prompt);
|
||||||
|
_movePromptRoot.SetActive(visible);
|
||||||
|
_movePromptText.text = prompt;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -792,17 +847,16 @@ private void RefreshPrompts()
|
||||||
```csharp
|
```csharp
|
||||||
if (GameEntry.InputModule.TryGetPrompt(InputActionId.Interact, out InputPrompt prompt))
|
if (GameEntry.InputModule.TryGetPrompt(InputActionId.Interact, out InputPrompt prompt))
|
||||||
{
|
{
|
||||||
// 文本标签
|
_label.text = InputPromptTextUtility.BuildTmpText(prompt);
|
||||||
_label.text = prompt.TextLabel;
|
|
||||||
|
|
||||||
// 图标(通过 TMP Sprite Tag)
|
|
||||||
if (prompt.HasSprite)
|
|
||||||
{
|
|
||||||
_iconText.text = $"<sprite=\"InputPrompt\" name={prompt.SpriteName}>";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
如果只想显示图标,可以使用:
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
_iconText.text = InputPromptTextUtility.BuildSpriteTags(prompt.SpriteName);
|
||||||
|
```
|
||||||
|
|
||||||
**说明:**
|
**说明:**
|
||||||
|
|
||||||
- 此步骤完全可选;不设置 Default Sprite Asset 不影响任何核心功能
|
- 此步骤完全可选;不设置 Default Sprite Asset 不影响任何核心功能
|
||||||
|
|
|
||||||
|
|
@ -29,9 +29,10 @@ MonoBehaviour:
|
||||||
m_defaultTextMeshProTextContainerSize: {x: 20, y: 5}
|
m_defaultTextMeshProTextContainerSize: {x: 20, y: 5}
|
||||||
m_defaultTextMeshProUITextContainerSize: {x: 200, y: 50}
|
m_defaultTextMeshProUITextContainerSize: {x: 200, y: 50}
|
||||||
m_autoSizeTextContainer: 0
|
m_autoSizeTextContainer: 0
|
||||||
|
m_IsTextObjectScaleStatic: 0
|
||||||
m_fallbackFontAssets: []
|
m_fallbackFontAssets: []
|
||||||
m_matchMaterialPreset: 1
|
m_matchMaterialPreset: 1
|
||||||
m_defaultSpriteAsset: {fileID: 11400000, guid: c41005c129ba4d66911b75229fd70b45,
|
m_defaultSpriteAsset: {fileID: 11400000, guid: 63ffe10cc9d517140bc4b2cb93c4e5e4,
|
||||||
type: 2}
|
type: 2}
|
||||||
m_defaultSpriteAssetPath: Sprite Assets/
|
m_defaultSpriteAssetPath: Sprite Assets/
|
||||||
m_enableEmojiSupport: 1
|
m_enableEmojiSupport: 1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue