Search Unity

C# pre-processor and Unity version check

Discussion in 'General Discussion' started by jjxtra, Mar 9, 2020.

  1. jjxtra

    jjxtra

    Joined:
    Aug 30, 2013
    Posts:
    1,464
    It is getting kind of ridiculous how many version pre-processors Unity has to define UNITY_2017_4_OR_NEWER, etc... This will only get worse as years go by.

    I've opened a github issue to enhance the C# language to allow >=, <=, >, < operators against pre-processors. This would allow the following to be defined by unity in the pre-processors:

    UNITY_VERSION=201900030004


    The in C# code:

    Code (CSharp):
    1. #if UNITY_VERSION >= 20190003
    2.  
    3. ... do Unity 2019.3 stuff ...
    4.  
    5. #endif
    Here is the github link for the issue: https://github.com/dotnet/csharplang/issues/3254

    If we all make enough noise, maybe we'll get luck and this will be come a reality :)
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    Actually I prefer this scheme to numeric alternative.
    Unreal uses numeric versioning (split into multiple constants) and it is not exaclty convenient for ifdefs.

    This will break the moment you'll need to ifdef for something like *f3 release, and additionally they don't quite fit into Int32.
     
  3. jjxtra

    jjxtra

    Joined:
    Aug 30, 2013
    Posts:
    1,464
    Sure, it could be stored as a string or long internally, that part is easy
     
  4. jjxtra

    jjxtra

    Joined:
    Aug 30, 2013
    Posts:
    1,464
    Something like:

    UNITY_VERSION_MAJOR=2019, UNITY_VERSION_MINOR=3 and UNITY_VERSION_PATCH=36


    Code (CSharp):
    1. #if UNITY_VERSION_MAJOR >= 2019 && UNITY_VERSION_MINOR >= 3
    2. ...
    3. #endif
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    This.

    I worked with this in unreal, and it is awful in practice. You end up defining intermediate defs.
    Code (csharp):
    1.  
    2. #if UNITY_VERSION_MAJOR >= 2019 && UNITY_VERSION_MINOR >= 3
    3. #define UN_VER_GE_2019_3
    4. #endif
    5.  
    Which is exactly what happens in unity right now.