Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by derHumping · Jun 04, 2014 at 08:59 PM · shadergraphicscrossplatformpreprocessor

Use Predefined Preprocessor Symbols for shaders.

I´d like to optimize the shaders in my project for different platforms. In C#, I make a lot use of preprocessor symbols, and I´m wondering if theres any way to use them in shader code. What I´d like to do is something like

  #if UNITY_IOS
          //some specific stuff here
     #else
         //regular stuff here
     #endif

I have figured out that it is possible to exclude a build target from the shader and use a fallback to get to an alternative version of it, but since the project is quiet big and has a lot of shaders, it would be quiet messy to always create multiple files of every shader. Most times, i just want to change one line of code, since some console versions of unity don´t support unlit shaders while unlit is necessary for the performance on mobile, so I really just need to do something like

        #if UNITY_Console
               Lighting On
         #else
              Lighting Off
         #endif

in every shader of the project.,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by namark · Oct 03, 2016 at 05:59 AM

For anyone who stumbles upon this, like I did, here's a potential solution.

SL-MultipleProgramVariants

In short you can add a line like this to the shader

 #pragma multi_compile FANCY_STUFF_OFF FANCY_STUFF_ON

which will cause two(or more) version of the shader to be created with appropriate symbols/keywords defined. You can then select the symbol/keyword you need from C#

 material.EnableKeyword("FANCY_STUFF_ON"); // for a single material
 Shader.EnableKeyword("FANCY_STUFF_ON"); // globally, for all materials

This is not an exact equivalent of C#'s preprocessors symbols, since variant selection happens at runtime, but it is close enough for all practical purposes. If you want to squeeze out even more performance you can use #pragme shader_feature which apparently will not include unused variants in the build... how exactly it does that, I'm not sure, haven't tested it myself.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Generating a texture through Graphics.Blit with shadergraph 0 Answers

How to replicate renderer.worldToLocalMatrix 0 Answers

Black Texture on Initial Load 1 Answer

Fake PS1 "jittery movement" effect in Unity? 1 Answer

Greyscale shading 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges