Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UNITY_VERSION broken in 2018.2 ?

Discussion in 'Shaders' started by larsbertram1, Jul 12, 2018.

  1. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    hi there,

    i just tried to add support for Unity 2018.2 to some shaders – but had to face the fact that:
    #if UNITY_VERSION > 20180202
    or
    #if UNITY_VERSION > 201822
    did not work out at all.

    so is it broken or do i do something completely wrong here?

    thanks,
    lars
     
    ModLunar likes this.
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    #if UNITY_VERSION > 201822
    This should work. This will include the code inside the '#if' part for Unity versions higher than 2018.2.2
    What you probably want is '#if UNITY_VERSION >= 201820'. This will include the code for any Unity version starting from 2018.2.0
     
  3. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    thanks for the tip, but unfortunately it does not work... :(
    i tried:
    #if UNITY_VERSION >= 201820
    #pragma multi_compile_vertex LOD_FADE_PERCENTAGE
    #else
    #pragma multi_compile LOD_FADE_CROSSFADE LOD_FADE_PERCENTAGE
    #endif

    but the compiler complains about "redefinition of _MainTex"

    only if i use:

    #pragma multi_compile_vertex LOD_FADE_PERCENTAGE
    and skip the #if #else and 2nd #pragma the shader compiles properly.
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Well, that's a different thing. Preprocessor doesn't work on #pragma multi_compile directives.
     
  5. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    so is there any other way then to just get one shader file suitable for unity 5.6.3 and unity 2018?
     
  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    There is no way to have different #pragma multi_compile for different Unity versions in a single shader file. Or at least, I'm not aware of those.

    What exactly are you trying to achieve?
     
  7. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    i just want to write a surface shader that supports lod fading on 5.6.3 – 2018.2.
     
  8. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    And why can't you have #pragma multi_compile LOD_FADE_CROSSFADE LOD_FADE_PERCENTAGE in both versions?
     
  9. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    because it results in cross fading even between lods which shall use percentage (when using speedtree like fading).
     
  10. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Hm. You could try doing something like
    Code (CSharp):
    1. #if UNITY_VERSION >= 201820
    2. #undef LOD_FADE_CROSSFADE
    3. #endif
    as the first thing in your CGPROGRAM block
     
  11. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    that works in the shader caster pass (cg) but not the surface shader.
     
  12. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Can you check the output of the surface shader? I wonder, where this #undef ends up at (if it does).
     
  13. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    cool, i can't login on my 2nd machine... two factor authentication.

    but i looked into the generated code:

    #if UNITY_VERSION >=201822 is part of the block "UNITY: Original start of shader".
    but unlike the #pragmas it is NOT commented.
     
  14. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    OK, this means that it's inserted after the #include directives.
    Then, I guess, you need to have two shaders.
     
  15. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    thnaks anyway.
    but wasn't there a change during the 5.x cycle which would let you include something before the standard includes?
     
  16. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    afaik, it's not possible
    what you could try to do is take a look, which include file is included first, make your own .cginc with the same name, put the #if there and include the global one afterwards
     
  17. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    thanks – but i guess i will go with 2 shaders then.
     
  18. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    A bit of a necro, but it appears UNITY_VERSION really is legitimately broken for editor versions which reach double digit revision numbers.

    For example: 2018.2.20

    Going by the documentation, or this thread, one might expect the UNITY_VERSION to be 2018220, but that messes with the original numbering system as now the 6 digit long version for 2019.1.0 (201910) would be less than the 7 digits of the expected version number. The actual UNITY_VERSION for 2018.2.20 is:

    201840

    It appears to be combining the ".2.20" in to 40 which kind of makes sense if you think about the easiest way to convert the Unity version into a single number.

    (major version, "2018") * 100 + (minor version, "2") + 10 + (revision, "20") = UNITY_VERSION

    2018 * 100 = 201800
    2 * 10 = 20
    201800 + 20 + 20 = 201840


    Of course collides with what you expect 2018.4.0 to be, which is 201840. It also collides with 2018.3.10 which also uses 201840! 2018.3.11 through 2018.3.14 all use 201839, so someone noticed the problem. Just be warned the number system is inconsistent now.
     
  19. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Hmm. As far as I remember, the fix to clamp versions to max of 9 should have landed to 2017.4 LTS, 2018.3 & 2018.4 LTS, and is in all 2019.x versions. So yeah 2018.2 is probably missing it... is that a big problem?
     
  20. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Big problem? Not really. The fact this isn’t in the documentation isn’t great.
     
    ModLunar likes this.
  21. Danoli3

    Danoli3

    Joined:
    Sep 12, 2017
    Posts:
    14
    For Reference: >= 2019.3.0
    #if UNITY_VERSION >= 201930
     
  22. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Definitely a shame this docs page doesn't mention more detailed info on the UNITY_VERSION define.

    I hope it gets updated eventually with more useful information (like a summary of the info from this forum thread!)
     
  23. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Wait, the double-digit Revision number is fixed?
    What does this fix look like, can we see examples and/or links to the docs that explain this please? :)
     
    MUGIK likes this.
  24. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    313
    Sorry for the re-necro, but it seems like this is still broken.
    No way to identify 2022.3.21 and it is a big problem when this is the only way to identify URP version, but the same URP version is different between versions of unity.

     
  25. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    yeah, i just had the same problem adding the foveated rendering stuff for meta: highest version i could set was 202239 - but i would need 2022319.
    @aleksandrk any idea?
     
  26. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    It's still clamped to 9. There's no easy fix for this since adding a digit would break those comparisons.
    2021319 (which translates into 2021.3.19) is greater than 202239 (2022.3.9).
    Please report this in the URP forum.
     
  27. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    313
    ok, I reported this in the URP forum
    So basically it can never be fixed to avoid breaking compatibility, I see.

    Then how is it going to work for Unity 6? Is it going to be defined as 600000 ?

    Thanks!
     
  28. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    No, we're actually going to use this opportunity and make it a bit more future proof for Unity 6 :)
     
  29. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    So, for Unity 6.123.7654 the UNITY_VERSION macro will be 61237654.
     
    Phantom_X likes this.
  30. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,899
    :)