Search Unity

global #define?

Discussion in 'Scripting' started by mindlube, Jun 21, 2011.

  1. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
    Supposedly there is a trick to make a global #define (preprocessor tag) by creating a special file in the root of your project. Would someone share the specifics? I am sick of having to put my #defines in every .cs file that uses it.
     
    h4sjohnson likes this.
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    There is no trick, you can't make global defines on a per project base.

    the only thing you could do are unity wide defines that apply everywhere and that requires a little "hack"
     
  3. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
    Dreamora, I believe you are mistaken. I have it on good authority (mikeD from prime31) there is a way to make a global #define and not have to put it at the top of every .cs file that uses it. I'll see if he can share the details, but he's on the road and wasn't able to tell me right away.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    There is one way and thats modifying the compiler control script.

    But thats not per project but per unity install
     
  5. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
  6. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
    GuruJeya14 likes this.
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    good catch, thats indeed unknown, never heard of it at least
     
  8. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    That's really neat we were looking for a feature like that for ages.

    I noticed there is a us.rsp listed at the link too, I'm curious what other options are available and how we can go about figuring them out.
     
  9. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
    Unfortunately, it seems that Monodevelop does not always notice these defines :( Compilation seems to work as expected, but the source code isn't highlighted correctly and document outline is suspect. Reverting to putting the #define in .cs files, in some case.
     
    Last edited: Jun 22, 2011
  10. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    MonoDevelop has it's own Define list, that you can add to. Unity handles it's own compilation (hence why the MonoDevelop output is called irrelvant.dll). Just go to Project Options -> Build -> Compiler -> Define Symbols and add it in. You'll notice the Unity defines are already manually included there.
     
  11. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
    Thanks Ntero!
     
  12. Fehr

    Fehr

    Joined:
    Mar 3, 2011
    Posts:
    23

    Attached Files:

  13. prime31

    prime31

    Joined:
    Oct 9, 2008
    Posts:
    6,426
    mcroswell_unity and taxvi like this.
  14. Fehr

    Fehr

    Joined:
    Mar 3, 2011
    Posts:
    23
    Prime31. . . brilliant as always. Thanks for sharing that.
     
    mcroswell_unity likes this.
  15. Rob_Kuato

    Rob_Kuato

    Joined:
    Jul 16, 2012
    Posts:
    1
    Thanks from all 6 of the coders here at Kuato :)
     
  16. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
    Yeah thanks Mike D. / Prime31 !
     
  17. znoey

    znoey

    Joined:
    Oct 27, 2011
    Posts:
    174
    Thanks from coders at school zone :D
     
  18. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
  19. chsu

    chsu

    Joined:
    Sep 26, 2012
    Posts:
    4
    Last edited: Feb 14, 2013
    zoran404 likes this.
  20. mindlube

    mindlube

    Joined:
    Oct 3, 2008
    Posts:
    993
    chsu, indeed it does - all I use now is the defines in playerSettings! thanks for following up this thread. One note to anyone trying it: the player settings are *per platform* which is confusing the first time you set a define, then switch platforms.
     
  21. 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.
     
  22. PAHeartBeat

    PAHeartBeat

    Joined:
    Jul 11, 2012
    Posts:
    76
    Hi
    @unimechanic
    Is There any option to use Global Pre-Processor (custom) in Unity 3.5.x version
     
  23. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    Hey gang -

    is there any way to include a DISABLE directive, eg

    #pragma warning disable 414

    project-wide?

    THANK YOU, REALLY.
     
  24. karl_

    karl_

    Joined:
    Mar 4, 2010
    Posts:
    464
    tequyla and Thorny2000 like this.
  25. jitwtttl

    jitwtttl

    Joined:
    Aug 29, 2014
    Posts:
    8
    @karl_'s solution works very well! Thanks.
     
  26. NlaakStudios

    NlaakStudios

    Joined:
    May 19, 2016
    Posts:
    12
    I do it this way. FYI Unity 5.6.2 >
    Code (CSharp):
    1. #define DEBUG     //Must be very first line or you get error. ONLY define it once in your main script
    2.  
    3. using System.Reflection;
    4. using UnityEngine;
    5.  
    6. //Please use name spaces in your Apps and API's  => ;)
    7. namespace apicompany.apiname
    8. {
    9.  
    10.     public class MyGameObject : MonoBehaviour
    11.     {
    12.         [SerializeField]
    13.         [Tooltip("Enable this for logging details informations about this class.")]
    14.         protected bool VerboseLogging = true;
    15.  
    16.  
    17.         private void Awake()
    18.         {
    19.             //***[You can use it this way]***/
    20.             if (mycompany.myapp.APIConfig.DEBUG_ENABLED && VerboseLogging)
    21.                 print(string.Format("{0}:{1} - Debug Mode {2} enabled.", GetType(), MethodBase.GetCurrentMethod(), ((mycompany.myapp.APIConfig.DEBUG_ENABLED) ? "is" : "is not")));
    22.             /// This prints to console =>
    23.             /// apicompany.apiname.MyGameObject:Void Start() - Debug Mode is enabled.
    24.             /// or
    25.             ///  apicompany.apiname.MyGameObject:Void Start() - Debug Mode is not enabled.
    26.         }
    27.  
    28.         // Or This Way - without any need of the static global
    29.         void Start()
    30.         {
    31.             #if DEBUG
    32.             if (VerboseLogging)
    33.                 print(string.Format("{0}:{1} - Debug Mode {2} enabled.", GetType(), MethodBase.GetCurrentMethod(), ((mycompany.myapp.APIConfig.DEBUG_ENABLED) ? "is" : "is not")));
    34.             #endif
    35.         }
    36.  
    37.  
    38.     }
    39. }
    40.  
    41. namespace mycompany.myapp
    42. {
    43. #if DEBUG
    44.     class APIConfig
    45.     {
    46.         public static bool DEBUG_ENABLED = true;
    47.     }
    48. #endif
    49. #if !DEBUG
    50.     //use this api - let's say SecurePlayerPrefs for example
    51.     class APIConfig
    52.     {
    53.         public static bool DEBUG_ENABLED = false;
    54.     }
    55. #endif
    56.  
    57.     ///
    58.     /// This does not work - Wish it did. But the above is a work around.
    59.     ///
    60.     /*
    61.     #if apiname
    62.         //Is Defined
    63.     #elif //<= Error on else? Bug?
    64.         //Not Defined
    65.     #endif
    66.     */
    67. }
     
    Last edited: Jul 5, 2017
    Luckymouse likes this.
  27. NBAH79

    NBAH79

    Joined:
    Dec 19, 2015
    Posts:
    1
    Edit -> Project Settings -> Player
    Inspector -> (Settings for PC,Mac and Linux standalone) -> Other Settings -> Scripting define symbols
    Always press Enter after editing. You'll see the difference in open VS immediately.