InvalidOperationException while resolving binding ‘CameraRotate:Vector2WithOneModifier’ in action map ‘xxx (UnityEngine.InputSystem.InputActionAsset):UI’

Unity2019.4.6f1 InputSystem1.0.0

InputSystemサンプルのCustom compositeを使ったactionMapを作ります。そして起動シーンでそのアクションマップを登録するとこのエラーが発生します。

PlayerInput, InputSystemUIInputModuleなどですね。

スポンサーリンク

原因

[RuntimeInitializeOnLoadMethod]
    private static void Initialize()
    {
        InputSystem.RegisterBindingComposite<Vector2WithOneModifier>();
    }

サンプルではこのように自作compositeを登録してます。そして登録されたcompositeはInputSystemUIInputModule.OnEnableで使われます。

しかしRuntimeInitializeOnLoadMethod attributeは実はOnEnableの後で呼ばれます!! そしてエラーになるわけです。

対策

Custom composite binding in package breaks test in the project that uses it
Hi, I have a Vector2WithOneModifier composite binding written and working correctly in one package, in another package I am using that composite, this...

テラシュールブログやforumでも書かれてますが

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    private static void Initialize()
    {
        InputSystem.RegisterBindingComposite<Vector2WithOneModifier>();
    }

OnEnableより前に呼ばれるようにしてやります。

まぁこれサンプルが悪いっていう話なんですが…