- Home /
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 http://answers.unity3d.com/questions/987078/materialstring-is-obsolote.html 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?
What's wrong with the solution in the url you attach? You can do exactly the same as that.
I need to be able to load a shader AFTER the game has been compiled. The solutions in the linked thread all involve saving a file in the project folder, which means that when the game is compiled, the Unity Shader Compiler will compile it into the game. Adding a shader file to the folder after this point will not cause it to be compiled, and it will thus be inaccessible to Shader.Find().
I think you've mentioned that there is a shader file created previously. Why don't you put that into your folder before compiling the game and load in during game runtime? I think all runtime support for shaders are removed after Unity 5.2.
But I guess you still can create a shader which takes some values at runtime to do what you want.
Answer by maccabbe · Sep 16, 2015 at 03:03 AM
You could try asset bundles. They would work as follows
Package you shader into an asset bundle that will be part of your mod.
Next, in your script, load the assetbundle from a file using www.LoadFromCacheOrDownload (it can be a file path or a web link).
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.
It took a while to get around to testing this approach (as it is a bit more involved than I'd expected and required me to get the hang of AssetBundles), but so far it does indeed work with no obvious problems. I suspect that AssetBundles are the new standard for importing custom content from now on.
Answer by kumbayaco · Sep 15, 2015 at 12:41 PM
Shader sdr = Resources.Load ("Diffuse") ; renderer.sharedMaterial.shader = sdr;
"Diffuse" is the shader name, not the shader file name.
This will work fine if I want to load the built-in "Diffuse" shader. What I need is to be able to compile the game, then add a new shader that isn't already included and create a material based on that.
Your answer
Follow this Question
Related Questions
Shader.SetGlobal for properties that materials already have 0 Answers
procedurally generated gradient 2 Answers
How to avoid Texture.Apply() 1 Answer
How do you hide a shader property? 2 Answers
Semitransparent shader not visible in front of objects 1 Answer