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!

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);
	}
}

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: Unity - Scripting API: RuntimeInitializeOnLoadMethodAttribute 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.