InputSystem icon indicating copy to clipboard operation
InputSystem copied to clipboard

MyDevice is re-added every time entering play mode

Open borishsu opened this issue 8 years ago • 2 comments

I am trying to add a custom device and using SimpleDevice.cs in the package for testing. The code is shown below. I use RuntimeInitializeOnLoadMethod attribute to initialize the registration.

[RuntimeInitializeOnLoadMethod]
public static void Initialize()
{
    InputSystem.RegisterLayout<MyDevice>();

    if (!InputSystem.devices.Any(x => x is MyDevice))
        InputSystem.AddDevice<MyDevice>();
}

However, the if statement doesn't seem to find any MyDevice in the system so a new one is added every time we enter play mode.

2018-09-12 11_12_25

Is this a bug or did I miss something? Thanks.

borishsu avatar Sep 12 '18 03:09 borishsu

This is a chicken-and-egg kind of problem. The system needs InitializeOnLoad code to run to have the up-to-date layout information in order to have all the layout information registered to be able to re-create devices it had before the domain reload (which happens when you enter play mode or change code) but the code above needs devices to have been restored already in order to work correctly.

Something that needs looking at.

The system used to take layout information across domain reloads and restore devices based on that information. However, aside from adding a great deal of data to domain reloads, it has the unfortunate side-effect of repeatedly recreating devices as InitializeOnLoad code was doing RegisterLayout calls.

Rene-Damm avatar Sep 14 '18 02:09 Rene-Damm

This does not happen on 0.2.0-preview and very obvious fixed in the past.

JiaUnity avatar Mar 05 '19 03:03 JiaUnity