Search Unity

Using #if platform defines in shaders?

Discussion in 'Shaders' started by ronronmx, Mar 13, 2012.

  1. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    Is it possible to use platform defines in a shader?
    For example:

    #if UNITY_EDITOR
    Use surface shader code...

    #if UNITY_IPHONE
    Use vertex lit shader code...

    etc...

    Or are those defines available only in scripting and not shaderLab? If yes, is there a similar way to achieve this in a shader?

    Thanks!
    Stephane
     
  2. Owen

    Owen

    Joined:
    Apr 20, 2011
    Posts:
    79
    It would also be extremely useful for version migration and code sharing if we had access to Unity version defines like in c#. For instance:

    #if UNITY_3_5
    // use new shader features
    #else
    // use fallback
    #endif
     
  3. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
  4. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    So I tried using #if UNITY_EDITOR and #if UNITY_IPHONE defines in my shader, and although it didn't throw any errors, it also didn't work. Is there any way of implementing platform specific #defines in shaders?

    Stephane
     
  5. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    you can make your own custom definitions and pass them to your shader via the below commands:
    Shader.EnableKeyword("MYDEFINE")
    Shader.DisableKeyword

    and use them in your shader as;
    #if defined(MYDEFINE)
    ...etc
    #endif

    look at the water3 shader and scripts.
     
    easycardgame likes this.
  6. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    Awesome, thanks for the help, i knew there was a way, I just didn't look in the right places ;)
    Thanks again Aubergine!
     
  7. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    So i looked at the water3 shader and script, and tried to implement the keywords as it does, but couldn't get it to work with my shader. The question I have is when doing:

    Code (csharp):
    1. Shader.EnableKeyword("MYDEFINE")
    does "Shader" return the shader used by the material of the gameObject this script is attached to? What if i don't attach the script to any gameObject with a shader(like a parent object)? I have tried that, by assigning the shader I want to change to a public property in the inspector:

    Code (csharp):
    1. public Shader myShader;    // Assigned shader in inspector, not null
    2.  
    3. void Start()
    4. {
    5.     // The problem is that "EnableKeyword" isn't available anymore:
    6.     myShader.(NO EnableKeyword)
    7.  
    8.     // But if using Shader, it is available:
    9.     Shader.EnableKeyword("myDefine");
    10. }
    Any ideas?
    Thanks for your time!
    Stephane

    PS: Maybe I should point out that I am NOT using Unity PRO, I am using Unity IPHONE basic (ver. 3.5)
     
  8. jethrogillgren

    jethrogillgren

    Joined:
    Jan 11, 2017
    Posts:
    28
    Although an old thread this comes up in the top couple results on a google search for platform defines in shaders.

    Unity now has Platform Defines using macros in shaders
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Though it's handy to note there still is no UNITY_EDITOR equivalent available to shaders.
     
    JoeStrout likes this.
  10. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    This is nuts, still not seeing a UNITY_EDITOR equivalent in shaders. Such a simple thing but would be really helpful
     
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I think there was discussion about this recently, Unity's added the tech for conditionally compiling for any Unity version from inside the shader now with defines. I can't find the thread but it exists to help shader authors, asset store etc. This could very easily be made to include editor detection, so I recommend telling them while it's hot :)
     
  12. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    This one?

    https://forum.unity.com/threads/package-requirements-in-shaderlab.1108832/

    Thought it was package only which doesn't solve targeting editor only.

    As for requesting features, I doubt Unity will take my suggestions for a while since I'm raising such a stink in other areas :p Also a bit sick of trying to talk to them to be honest
     
  13. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    We have it in the backlog :)
    Unfortunately, it's a bit more involved than just adding this macro there.
     
  14. Jean-Moreno

    Jean-Moreno

    Joined:
    Jul 23, 2012
    Posts:
    593
    @aleksandrk Just wondering if this is still in the backlog a couple years later?
     
  15. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    Yes, it's still there. There are more pressing things to work on.
     
    Propagant and Jean-Moreno like this.