Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by SirSquid · Dec 07, 2021 at 06:21 PM · shadershadersshader programming

Is there a way to save global shader variables?

Is there a good way to save global shader variables/properties?

I've got several different shaders which are supposed to all share the same variable, and I want the player to be able to change this value whenever they want. Right now I'm controlling this with a C# script and using the Shader.SetGlobalInt function to set the variable, and this does largely work, however, every time I exit Unity, the global variable isn't saved and resets to zero next time I open, and this is particularly annoying for this set of shaders specifically because a value of zero makes things invisible.

If I press play, the objects will become visible again and remain visible after exiting play mode (this is because Shader.SetGlobalInt will be used to correct the global variable in a start function on an empty game object), however, it's still rather annoying and I feel like there's probably a better way to do this.

Something I've noticed in writing shaders is that there are some built-in adjustable variables which can be used by shader code like unity_FogStart or unity_FogEnd. I think that ideally I'd want to be able to simply create a new custom variable like those which doesn't rely on any game object if that's possible.

Generally I just want to know what'd be the best way to go about this sort of problem. Thanks in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SirSquid · Dec 11, 2021 at 09:16 PM

Okay, I managed to find a solution:

I saved my variable with PlayerPrefs.SetInt so it'll persist every time I open/close Unity or the game.

I made a script utilizing [InitializeOnLoad] to set the value as soon as I open the editor, and I put this script in a folder named "Editor" which Unity excludes from builds so I wouldn't have build errors.

And I made another script to cover setting the value when the game loads utilizing this: https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html and using Shader.SetGlobalInt("Name", PlayerPrefs.GetInt("Name")) to get the value from PlayerPrefs.

It's working great in both the editor and builds, so it's all good now.

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
avatar image
1

Answer by Eno-Khaon · Dec 07, 2021 at 10:53 PM

If I'm not mistaking this, the problem comes up when starting the Unity Editor, and not the game itself (based on your mention of "press[ing] play" to get it into the right state).

You can make use of an editor script utilizing [InitializeOnLoad] as mentioned in Unity's documentation on Running Editor Script Code on Launch.

 using UnityEngine;
 using UnityEditor;

 [InitializeOnLoad]
 public class Startup
 {
     static Startup()
     {
         Shader.SetGlobalInt(myGlobalShaderValue);
     }
 }
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 SirSquid · Dec 08, 2021 at 03:57 AM 0
Share

I tried it out and does work, however, the [InitializeOnLoad] line also prevents the game from building for some reason (at this point the startup script won't actually be needed, but this means in order to build I need to get rid of that line in the startup script every time I do it).

avatar image Eno-Khaon SirSquid · Dec 12, 2021 at 07:07 AM 0
Share

Yeah, editor scripts won't build into a game. You can get around that by putting them into an "Editor" folder in your project (any such folder is culled during the build process), or use conditional compilation blocks around them:

 using UnityEngine;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 [InitializeOnLoad]
 public class Startup
 {
     #if UNITY_EDITOR
     static Startup()
     {
         Shader.SetGlobalInt(myGlobalShaderValue);
     }
     #endif
 }
avatar image SirSquid Eno-Khaon · Dec 12, 2021 at 12:49 PM 0
Share

Thanks! That's good to know!

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

211 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 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 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 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 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 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 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 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 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 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

Scene Color Node in Shader Graph not working with Unity's 2D Renderer and URP 5 Answers

Distortion Shader does not render transparent objects (Shader Graph) 2 Answers

Making shader not ignore Light on transparent areas ? 0 Answers

Shader for distorting a texture as if it were on a sphere 3 Answers

Wireframe shader with constant width and no diagonals 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