Material(string) is obsolete. How else can a shader be loaded at runtime?

For some time now I’ve been a fan and modder of a certain game which uses its own asset database similar to the one in the Unity editor - at the start, it loads all the models and textures in its directory and converts them into GameObjects, etc. so as to make the game expandable and mod-friendly.

Currently I have a mod written that uses a custom shader, which can be imported into the game using “Material(string)”. However, recently when using this function in the Unity Editor, I’ve been presented with a message that Material(string) is “obsolete” and will be removed in the future. In the interest of not being blindsided when this happens, I’d like to know what alternate options there are for loading a shader at runtime like this.
I’ve already looked over Material(string) is Obsolete - Questions & Answers - Unity Discussions and done some Google searches that took me to old webpages (February 2015 or earlier) that are no longer relevant.

The “obvious” solution as recommended in the editor and most places I’ve seen is to “use shader assets”, but unless I myself am the developer of the game, I can’t do that - and even if I were, I can’t be expected to recompile the game for every user out there based on whether or not they want my mod.

So someone please tell me if there is some way to load shaders during game runtime, perhaps from text, from a compiled shader file I have created previously, or otherwise!

Or if not, can anyone confirm that when this function does get removed, the Unity team has plans to introduce a substitute?

You could try asset bundles. They would work as follows

  1. Package you shader into an asset bundle that will be part of your mod.
  2. Next, in your script, load the assetbundle from a file using www.LoadFromCacheOrDownload (it can be a file path or a web link).
  3. Finally get the shader from the assetbundle using AssetBundle.LoadAsset.

Not sure if the game you are trying to mod will support this and it would be tricky to support since you’d basically have to let modders write scripts. However any game that does support this would give modders an easy way to add any non script asset, from models and textures to materials, animations, and scenes.

Shader sdr = Resources.Load (“Diffuse”) ;
renderer.sharedMaterial.shader = sdr;

“Diffuse” is the shader name, not the shader file name.