迁移到 1.0.0-beta 输入 SDK

本指南介绍了如何将您的 Unity 游戏迁移到最新的输入 SDK。1.0.0-beta SDK 相较于之前的 0.0.4 预览版有实质性改进。您应尽快从早期预览版迁移。在 2023 年 3 月之前,0.0.4 SDK 仍将继续运行。

更新引用

类添加了 Play 前缀,以避免与 Unity 发生命名冲突。当您看到类似以下错误消息时:

error CS0246: The type or namespace name 'InputMappingProvider' could not be found (are you missing a using directive or an assembly reference?)

您必须向类名添加 Play 前缀。例如,InputMappingProvider 变为 PlayInputMappingProvider

更新每个 InputAction

InputAction 现在通过调用 PlayInputAction.Create 而不是创建具有命名字段的新 struct 来构造。

找到所有调用 new InputAction 的代码

var driveAction = new InputAction
{
    ActionLabel = "Drive",
    UniqueId = (int)InputEventIds.DRIVE,
    InputControls = new InputControls
    {
        AndroidKeycodes = new[] { AndroidKeyCode.KEYCODE_SPACE }
    }
};

并将其替换为对 PlayInputAction.Create 的调用。

var driveAction = PlayInputAction.Create(
    "Drive",
    (int)InputEventIds.DRIVE,
    PlayInputControls.Create(
        new[] { AndroidKeyCode.KEYCODE_SPACE },
        null
    )
);

更新每个 InputGroup

InputAction 类似,InputGroup 现在调用 PlayInputGroup.Create,而不再需要您手动填充 struct

这意味着您应该找到所有调用 new InputGroup 的代码

var gameInputGroup = new InputGroup
{
    GroupLabel = "Game controls",
    InputActions = new List<InputAction>
    {
        driveAction,
        turboAction,
        openGarageAction,
        openStoreAction
    }
};

并将其替换为对 PlayInputGroup.Create 的调用。

var gameInputGroup = PlayInputGroup.Create(
    "Game controls",
    new List<PlayInputAction>
    {
        driveAction,
        turboAction,
        openGarageAction,
        openStoreAction
    }
);

更新 InputMap

InputMap 也使用 PlayInputMap.Create,而不是构造新的结构体。

找到所有调用 new InputMap 的代码

return new InputMap
{
    InputGroups = new List<InputGroup>
    {
        gameInputGroup,
        menuInputGroup
    },
    MouseSettings = new MouseSettings
    {
        AllowMouseSensitivityAdjustment = false,
        InvertMouseMovement = false
    }
};

并将其替换为对 PlayInputMap.Create 的调用。

return PlayInputMap.Create(
    new List<PlayInputGroup>
    {
        gameInputGroup,
        menuInputGroup
    },
    PlayMouseSettings.Create(false, false)
);

重命名 PlayInputMappingClient 方法

对于 PlayInputMappingClientRegisterInputMappingProvider 已重命名为 SetInputMappingProvider

因此,找到所有调用 RegisterInputMappingProvider 的代码

Input.GetInputMappingClient().RegisterInputMappingProvider(_inputMappingProvider);

并将其替换为对 SetInputMappingProvider 的调用。

PlayInputMappingClient inputMappingClient =
    Google.Play.InputMapping.PlayInput.GetInputMappingClient();
inputMappingClient.SetInputMappingProvider(_inputMapProvider);

UnregisterInputMappingProvider 也已重命名为 ClearInputMappingProvider,并且不再需要您先前注册的 InputMappingProvider 作为参数。

找到所有调用 UnregisterInputMappingProvider 的代码

Input.GetInputMappingClient().UnregisterInputMappingProvider(_inputMapProvider);

并将其替换为 ClearInputMappingProvider

PlayInput.GetInputMappingClient().ClearInputMappingProvider();