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 ronronmx · Mar 13, 2012 at 07:04 AM · shaderruntimeshader-replacement

Using replacement shader at runtime?

My dilemma:
When building levels, I like all my scene assets to use a surface shader, so I can preview within the Editor how the lighting will look like after setting up all my lights, without having to bake all lightmaps to see how the lights are affecting the objects.

But I am working on an mobile game, so using surface shaders is definitively not the best for speed. Before making a build, I assign a cheaper, vertex lit shader to the scene objects, and if I haven't rendered any lightmaps yet, the scene looks like it's being lit by only one light, my main directional light.

Having to manually switch shaders everytime I change the lighting setup or make changes to the scene is a time waster and a pain :)

What I would like to do:
I want tell Unity to use surface shaders when the game is being run in the editor, and use vertex lit shaders when it's running on a mobile platform. I could do that using replacement shaders, or writing the logic directly within a shader (if isEditor use surface else use vertex lit).

Which would be the better approach, and is there any other way of doing this? Also, is using replacement shaders at runtime have an impact on performance?

Thanks for your time!
Stephane

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
1
Best Answer

Answer by Meltdown · Mar 13, 2012 at 08:13 AM

You can use a variety of preprocessor directives to achieve what you want..

  #if UNITY_EDITOR
     // Change your material's shader here when it's being run in the editor
   #endif
 
   #if UNITY_IPHONE
    // Change your material' shader here when being run on iOS
   #endif
 
 
   #if UNITY_ANDROID
    // Change your material' shader here when being run on Android
   #endif


Comment
Add comment · Show 3 · 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
avatar image ronronmx · Mar 13, 2012 at 05:47 PM 0
Share

Exactly what I was looking for! Would it be better to do this in script - like your above example - or to do this within the shader? Doing it within the shader means i would only need 1 shader, smart enough to know which SubShader to use depending on the platform, but doing in script is easier and less prone to errors. Can you even use #if UNITY_EDITOR/UNITY_IPHONE in a shader?

Thanks a lot for your help!

avatar image Meltdown · Mar 13, 2012 at 06:08 PM 0
Share

As far as I know it can't be used in a shader.

avatar image ronronmx · Mar 13, 2012 at 08:34 PM 0
Share

Got it, thx a lot for the help!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Replacing Shader At Runtime. 1 Answer

Replacement Shaders and Tags 0 Answers

Material doesn't have a color property '_Color' 4 Answers

Runtime javascript 1 Answer


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