Search Unity

#define directives for Input System used

Discussion in 'Input System' started by nx-sm, Aug 17, 2019.

  1. nx-sm

    nx-sm

    Joined:
    Jul 4, 2015
    Posts:
    10
    Hi, I am currently implementing both Input Systems in my project. I am wondering if there is a define directive to check if Unity is currently set to legacy, new or both input systems. Thus, only a specific code gets compiled for the build. I checked the docs which mentioned that we could do our own custom directives but I never did that before.

    Thank you
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Unfortunately, there's neither a #define nor a runtime API for that. The best I can offer ATM is a rather lame hack to manually update a #define in your project in the editor based on the current project settings.

    Code (CSharp):
    1. static class UpdateBuildSettings
    2. {
    3.     [MenuItem("Tools/Sync New Input System Define")]
    4.     public static void SyncNewInputSystemDefine()
    5.     {
    6.         var playerSettings = Resources.FindObjectsOfTypeAll<PlayerSettings>().First();
    7.         var playerSettingsObject = new SerializedObject(playerSettings);
    8.         var newInputSystemEnabled =
    9.             playerSettingsObject.FindProperty("enableNativePlatformBackendsForNewInputSystem")
    10.                 .boolValue;
    11.  
    12.         var buildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
    13.         var scriptingDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(
    14.             buildTargetGroup);
    15.         scriptingDefines.Replace(";NEW_INPUT_SYSTEM_ENABLED", "");
    16.         if (newInputSystemEnabled)
    17.             scriptingDefines += ";NEW_INPUT_SYSTEM_ENABLED";
    18.         PlayerSettings.SetScriptingDefineSymbolsForGroup(
    19.             buildTargetGroup, scriptingDefines);
    20.     }
    21. }
    22.  
    (There's probably better places than running this from a menu item)

    Disclaimer: I haven't actually tried this code. Could be it doesn't even compile. Conceptually, this should work though.
     
  3. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    This is a very useful thing to add, so I am in the process of adding such defines to Unity. Just not sure how far back we can get them in Unity releases (might be 19.3+ only).
     
  4. kassskata

    kassskata

    Joined:
    Sep 24, 2019
    Posts:
    16
    On the last update (v. 2019.2+ ) on Unity, you have directives but be careful. Doesn't work very well. I am not sure why.
    Documentation: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
    Example:
    Code (CSharp):
    1. #if (ENABLE_LEGACY_INPUT_MANAGER)
    2.         Debug.Log("INPUT_Manager");
    3. #elif (ENABLE_INPUT_SYSTEM)
    4.         Debug.Log("INPUT_System");
    5. #elif (ENABLE_INPUT_SYSTEM && ENABLE_LEGACY_INPUT_MANAGER)
    6.         Debug.Log("INPUT_System");
    7. #else
    8.         Debug.Log("INPUT_Manager");
    9. #endif
     
    Doc_Martins and Oyedoyin1 like this.
  5. kassskata

    kassskata

    Joined:
    Sep 24, 2019
    Posts:
    16
    When switch player settings make sure you compile the code again before play mode. Restart Unity editor is not enough.
     
    Doc_Martins and Oyedoyin1 like this.
  6. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Holy F*** thank you so much! I was tearing my hair out over this one :)
     
    Oyedoyin1 likes this.