Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Shouldn't there be pre-processor directives for all versions greater than a certain version?

Discussion in 'Editor & General Support' started by VatelsRevenge, Aug 28, 2014.

  1. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
    I want something like:

    Code (CSharp):
    1. #if UNITY_4_5_PLUS
    2.  
    3. //Code that only happens in Unity versions 4.5 and greater.
    4.  
    5. #endif
    and while I'm at it something like

    Code (CSharp):
    1. #if UNITY_3_0_TO_UNITY_4_3
    2.  
    3. //Code that only happens in Unity versions between 3.0 and 4.3.
    4.  
    5. #endif
    For example, If a new feature is added for Unity 4.5 that requires version specific code, then I want to assume that it'll continue to work on future versions. When 4.6 comes out, I don't want to go through every piece of version specific code and add "|| UNITY_4_6". These lines of code quickly begin getting ridiculous.

    For KeyWord searching: Platform Dependent Compilation. Compile code selectively depending on the version of the engine you are working on.
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Unfortunately that would completely pollute the compiler directive list. You'd have to do 4_5_Plus, 4_5_2_Plus, 4_5_3_Plus... and on and on and on for every version. What you could do though is use a define but you'd have to add it to the top of every file you need it as such:

    #if UNITY_3_5 || UNITY_4_2
    #define YES_COMPILE
    #endif

    Then in your code:

    #if YES_COMPILE
    //do your thing
    #endif
     
    VatelsRevenge likes this.
  3. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
    I can work with that. At least with that I only have to change one line each time Unity updates. Thanks!
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Well one line in each file where it's needed, but a global find/replace would be sufficient.
     
    VatelsRevenge likes this.
  5. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
    Oh I skipped that part.. I see. Well, that's still better than what I was doing. But I really don't like adding obscure development steps that I have to hope I remember to do each time a new version comes out. I like the idea of a "UNITY_4_5_PLUS" concept because everything will simply work until the day unity creates a version where the functionality no longer works. And that's when I'd WANT a crash or compilation error.

    Is there any documentation I could comb through so I can better understand why my suggestion would pollute the compiler directive list to an unacceptable degree? I'm not exactly sure what that means. Would this bog anything down even if the list was thousands of options long?
     
  6. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    No documentation but for each version you would have something like this:

    Unity 3.5 -> UNITY_3_5_PLUS
    Unity 4 -> UNITY_3_5_PLUS, UNITY_4_0_PLUS
    Unity 4.2 -> UNITY_3_5_PLUS, UNITY_4_0_PLUS, UNITY_4_2_PLUS
    Unity 4.5 -> UNITY_3_5_PLUS, UNITY_4_0_PLUS, UNITY_4_2_PLUS, UNITY_4_5_PLUS

    They'd have to add new directives for every single release and it would get really hairy. There are already a lot of directives in there. You should see my asset. I have directives for multiple platforms but also for iOS for different versions of Unity.
     
  7. kingstone426

    kingstone426

    Joined:
    Jun 21, 2013
    Posts:
    43
    Necroing this 8 year old thread for anyone googling a solution; there is: UNITY_X_Y_OR_NEWER.

    You can also compile code selectively based on the earliest version of Unity required to compile or execute a given portion of code. Given the same version format as above (X.Y), Unity exposes one global #define in the format UNITY_X_Y_OR_NEWER, that you can use for this purpose.
    https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    You can apply conditional compilation like this:

    Code (CSharp):
    1. #if UNITY_2021_1_OR_NEWER && !UNITY_2022_2_1_OR_NEWER
    2. // Your code here
    3. #endif
     
    Phil_Z likes this.