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

Project wide conditional compilation

Discussion in 'iOS and tvOS' started by gutripper, Jan 27, 2009.

  1. gutripper

    gutripper

    Joined:
    Sep 30, 2008
    Posts:
    45
    In a regular .NET application, it's possible to define symbols used for conditional compilation at a project level. For example, a debug build of a VS solution defines the symbol 'DEBUG'.

    I know that in Unity it's possible to #define symbols in C# code at a file level, but I'm wondering if there is a way to define symbols across the entire project?

    An example of the purpose of this would be to create a 'lite' version of a game that contains less features, and maybe a nag screen.

    Thanks!
     
  2. Poita_

    Poita_

    Joined:
    Dec 18, 2008
    Posts:
    146
    I would also like to know this.
     
  3. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Yup, do it all the time.

    Create a main script called something like GameManager and make it a singleton.

    http://www.unifycommunity.com/wiki/index.php?title=AManagerClass

    This has been covered quite a bit in the forums, so there is plenty to search for.

    Once you have a singleton class/script, you can create static var's that are available all throughout your game lifespan.

    I use var's like demo and pause etc to allow every script in every level to know if my game is in demo mode or paused etc....
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    that gives you globals, but no compiler level features which #ifdef etc are.

    yours can only disable it instead of prevent the stuff from beeing in the app at all, which at least on the iphone is a day / night difference.

    Would love to see that feature or to know how to use it if already present.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, that's not really what's being asked...just having a "isDemo" variable means that all your code is being included in all projects. With #define symbols, the demo code simply wouldn't be included in the non-demo project at all.

    However, I don't know the actual answer, sorry. ;)

    --Eric
     
  6. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Ok, but mine way WILL give them the functionality to do what they specifically asked for, if there is no way to actually implement/use compiler directives.

    Original OP asked for a way to have his entire game know it is in demo mode etc. Not a way to specifically exclude code at compile time.

    Yes, the extra benefits would be great to have, but the OP can do what he needs with this.
     
  7. Poita_

    Poita_

    Joined:
    Dec 18, 2008
    Posts:
    146
    If you had a global constant, and then used that in a conditional statement, would the .NET compiler not optimize the unreachable code away?

    eg.

    Code (csharp):
    1. const bool alwaysTrue = true;
    2.  
    3. /* ... */
    4.  
    5. if(alwaysTrue)
    6.   // Do this
    7. else
    8.   // Do that
    Would the "Do that" code actaully be included in the assembly?
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    The question was mainly and only targeted at how to exclude code at compile time.
    If you reread it you will see that he mentions a compiler flag (DEBUG), as well as a preprocessor functionality (#define), both only existant at compile time.



    peter: not sure if it does. But given that C# has #ifdef and related, there is the chance it does not remove unreachable code. Instead it will likely give you a warning that you have unreachable code.
    But thats something which likely only ReJ or another dev can answer
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Good point...I gave that a try and the unreachable code is not, in fact, included in the build. (You don't have to be a Unity dev to be able to see what code is included in the build. ;) )

    --Eric
     
  10. silmaril

    silmaril

    Joined:
    May 6, 2009
    Posts:
    3
  11. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Actually, this is a Unity feature I had been requesting for quite a while now ... it's not really related to Unity iPhone IMHO. In fact, Unity iPhone does use conditional compilation symbols ... at least one: UNITY_IPHONE (if I remember correctly), which is automatically passed to the compiler when compiling with Unity iPhone, see also:

    http://forum.unity3d.com/viewtopic.php?t=15650

    ... but what we'd need is really to be able to set those directives in the editor.

    One workaround (that's what I'm currently using) is have #define XYZ in every file that uses the relevant directive (or, simply, all class files). Then, if you do builds, go through all code-files and replace #define XYZ with //#define XYZ (in other words "comment it out") if you want to disable it.

    It's not too hard to write a little Mono or .NET script that does this for you ;-)
     
  12. jcaprio

    jcaprio

    Joined:
    Oct 26, 2011
    Posts:
    1
    Project-wide conditional compilation is possible in Visual Studio by opening the Project Properties (right-click on Project in Solution Explorer pane, select Properties), opening the "Build" tab, and entering the desired symbols in the text box labeled "Conditional compilation symbols".
     
  13. striche

    striche

    Joined:
    Jan 4, 2011
    Posts:
    61
    You could also try an Editor script that iterates through all of your files and adds the appropriate #define the start of each before creating the build.
     
  14. kersk

    kersk

    Joined:
    Jan 2, 2012
    Posts:
    56
    Since this old thread was just resurrected, I figured I should leave this info below for anyone searching for this in the future:

    There is support for global defines but it is undocumented. You just need to create a special file in your root /Assets directory.

    See this thread for more info: http://forum.unity3d.com/threads/71445-How-To-Set-Project-Wide-pragma-Directives-with-JavaScript

    Misc Notes:
    us.rsp = UnityScript / JavaScript
    gmcs.rsp / smcs.rsp = C#, which one varies depending on the .NET profile you are targeting
    Any changes to these files will not kick off a recompile of your code, so you need to manually force this to happen if you want to see your changes.
     
  15. unimechanic

    unimechanic

    Joined:
    Jan 9, 2013
    Posts:
    155
    We are improving the documentation on Custom Defines, and it's important to update these threads with the correct way of using them in the current versions of Unity. If you want to modify only global defines, you should use Scripting Define Symbols in Player Settings, because this will cover all the compilers. If you choose the .rsp files instead, you'll have to provide one file for every compiler Unity uses, and you won't know when one or another compiler is used. To do this you must add a text file with the extra directives to the "Assets/" folder:

    [table="width: 500"]
    [tr]
    [td]C#[/td]
    [td]<Project Path>/Assets/smcs.rsp[/td]
    [/tr]
    [tr]
    [td]C# - Editor Scripts[/td]
    [td]<Project Path>/Assets/gmcs.rsp[/td]
    [/tr]
    [tr]
    [td]UnityScript[/td]
    [td]<Project Path>/Assets/us.rsp[/td]
    [/tr]
    [tr]
    [td]Boo[/td]
    [td]<Project Path>/Assets/boo.rsp[/td]
    [/tr]
    [/table]

    As an example, if you include the single line "-define:UNITY_DEBUG" in your smcs.rsp file the define UNITY_DEBUG will exist as a global define for C# scripts, except for Editor scripts. Every time you make changes to .rsp files you will need to recompile for them to be effective. You can do this by updating or reimporting a single script (.js, .cs or .boo) file. All this information is being added to our documentation.