集成 Input SDK

Input SDK提供了一个统一的界面,让玩家在 Google Play Games For PC 上方便地找到游戏的鼠标和键盘绑定,从而提升玩家的游戏体验。

准备工作

如果你要在一个全新的 Android 构建上使用 Input SDK,只需要在构建面板上勾选 Input SDK 选项。

如果这个 Anrdoid 工程是之前构建的,就需要手动在 文件 native/engine/android/app/build.gradle 中添加下面的代码:

  1. dependencies {
  2. implementation 'com.google.android.libraries.play.games:inputmapping:1.0.0-beta'
  3. ...
  4. }

添加 InputMapProvider 实现

新建类文件 MyInputMapProvider.java

  1. public class MyInputMapProvider implements InputMappingProvider {
  2. public enum InputEventIds {
  3. JUMP,
  4. LEFT,
  5. RIGHT,
  6. USE,
  7. SPECIAL_JUMP,
  8. SPECIAL_DUCK,
  9. CMB_MOVE,
  10. CMB_DASH,
  11. CMB_WAYPOINT
  12. }
  13. @Override
  14. public InputMap onProvideInputMap() {
  15. InputAction jumpInputAction = InputAction.create(
  16. getString(R.string.key_jump),
  17. InputEventIds.JUMP.ordinal(),
  18. InputControls.create(
  19. Collections.singletonList(KeyEvent.KEYCODE_SPACE),
  20. Collections.emptyList()
  21. )
  22. );
  23. InputAction leftAction = InputAction.create(
  24. getString(R.string.key_Left),
  25. InputEventIds.LEFT.ordinal(),
  26. InputControls.create(
  27. Collections.singletonList(KeyEvent.KEYCODE_DPAD_LEFT),
  28. Collections.emptyList()
  29. )
  30. );
  31. InputAction rightAction = InputAction.create(
  32. getString(R.string.key_Right),
  33. InputEventIds.RIGHT.ordinal(),
  34. InputControls.create(
  35. Collections.singletonList(KeyEvent.KEYCODE_DPAD_RIGHT),
  36. Collections.emptyList()
  37. )
  38. );
  39. InputAction cmbMove = InputAction.create(
  40. getString(R.string.key_Move),
  41. InputEventIds.CMB_MOVE.ordinal(),
  42. InputControls.create(
  43. Collections.emptyList(),
  44. Collections.singletonList(InputControls.MOUSE_RIGHT_CLICK)
  45. )
  46. );
  47. InputAction cmbDash = InputAction.create(
  48. getString(R.string.key_Dash),
  49. InputEventIds.CMB_DASH.ordinal(),
  50. InputControls.create(
  51. Arrays.asList(KeyEvent.KEYCODE_SHIFT_LEFT, KeyEvent.KEYCODE_SPACE),
  52. Collections.emptyList()
  53. )
  54. );
  55. InputAction cmbWaypoint = InputAction.create(
  56. getString(R.string.key_Waypoint),
  57. InputEventIds.CMB_WAYPOINT.ordinal(),
  58. InputControls.create(
  59. Collections.singletonList(KeyEvent.KEYCODE_SHIFT_LEFT),
  60. Collections.singletonList(InputControls.MOUSE_RIGHT_CLICK)
  61. )
  62. );
  63. InputGroup movementInputGroup = InputGroup.create(
  64. getString(R.string.key_BasicMove),
  65. Arrays.asList(jumpInputAction, leftAction, rightAction, cmbMove, cmbDash, cmbWaypoint)
  66. );
  67. return InputMap.create(
  68. Arrays.asList(movementInputGroup),
  69. MouseSettings.create(true, true)
  70. );
  71. }
  72. }

添加按键的描述

res\values\strings.xml 中定义用于国际化的文本,以供前面步骤中使用。

  1. <resources>
  2. ...
  3. <string name="key_jump">Jump</string>
  4. <string name="key_Left">Left</string>
  5. <string name="key_Right">Right</string>
  6. <string name="key_Move">Move</string>
  7. <string name="key_Dash">Dash</string>
  8. <string name="key_Waypoint">Add Waypoint</string>
  9. <string name="key_BasicMove">Basic Movement</string>
  10. </resources>

修改 AppActivity 注册 InputMapping

AppActivity.java 位于 native\engine\android\app\src\main\cocos\game,需做如下修改。

  1. ...
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. ...
  6. InputMappingClient inputMappingClient = Input.getInputMappingClient(this);
  7. inputMappingClient.setInputMappingProvider(new MyInputMapProvider());
  8. }
  9. ...
  10. @Override
  11. protected void onDestroy() {
  12. InputMappingClient inputMappingClient = Input.getInputMappingClient(this);
  13. inputMappingClient.clearInputMappingProvider();
  14. super.onDestroy();
  15. ...
  16. }

测试效果

在 Google Play Games 提供的 HPE 模拟器中运行,通过 Shift + Tab 调出页面。

key codes

更多的细节请参考 Input SDK 官方文档