Search Unity

Bug A users own InputSettings breaks the Input System

Discussion in 'Input System' started by tertle, Nov 8, 2023.

  1. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    This has bothered me for ages but never got around to reporting it.
    If you have your own SO in your project called InputSettings it breaks the Project settings for Input System

    The problem is you just do a AssetDatabase.FindAssets("t:InputSettings") but you just assume it's the type of
    UnityEngine.InputSystem.InputSettings

    But if anyone creates another type of InputSettings and FindAsset returns it in [0]

    Code (CSharp):
    1. AssetDatabase.LoadAssetAtPath<InputSettings>(m_AvailableInputSettingsAssets[0])
    returns null as you never check the type of correct or any elements in the array because the actual settings would be in [1]

    Problem code below

    Code (CSharp):
    1.  
    2. private static string[] FindInputSettingsInProject()
    3. {
    4.     var guids = AssetDatabase.FindAssets("t:InputSettings");
    5.     return guids.Select(guid => AssetDatabase.GUIDToAssetPath(guid)).ToArray();
    6. }
    7.  
    8. private void InitializeWithCurrentSettings()
    9. {
    10.     // Find the set of available assets in the project.
    11.     m_AvailableInputSettingsAssets = FindInputSettingsInProject();
    12.  
    13.     // See which is the active one.
    14.     m_Settings = InputSystem.settings;
    15.     m_SettingsDirtyCount = EditorUtility.GetDirtyCount(m_Settings);
    16.     var currentSettingsPath = AssetDatabase.GetAssetPath(m_Settings);
    17.     if (string.IsNullOrEmpty(currentSettingsPath))
    18.     {
    19.         if (m_AvailableInputSettingsAssets.Length != 0)
    20.         {
    21.             m_CurrentSelectedInputSettingsAsset = 0;
    22.             m_Settings = AssetDatabase.LoadAssetAtPath<InputSettings>(m_AvailableInputSettingsAssets[0]);
    23.             InputSystem.settings = m_Settings;
    24.         }
    25.     }