Is there a non-obsolete way to compile materials *in the editor* using a string of shader code?
I've searched around, but all the answers I found were either a) extremely outdated or b) trying to do it during gameplay.
I'm working on a plugin that involves generating and compiling shaders to do things like generate noise into textures using the GPU. I know that the Material constructor that takes shader code is being removed because not all platforms can use it, but is there a way to still do it in the editor, where we obviously know the platform supports it?
Additionally, I'd like to know if there's a way to invoke that material to render into a render texture (again, in the editor).
I appeared to have found a workaround from Here, but it's annoying to have to jump through all these hoops just to compile a shader. Plus, I'd still like to know whether it's possible to use the material in the editor to render to a render texture. So I'm not going to consider this "answered" just yet.
Answer by heyx3 · Oct 08, 2015 at 06:42 AM
Figured it out myself! As mentioned in my comment, the way to do it is kind of hacky -- create a ".shader" file with the code, then call AssetDatabase.ImportAsset("Assets/MyShader.shader", ImportAssetOptions.ForceSynchronousImport)
to make Unity immediately compile the shader, then use GL and RenderTexture classes to immediately render a quad with that shader into a texture. This all worked correctly inside the "OnGUI" method (because it happens when I press a button), so I'm pretty sure it works regardless of the current state of the editor/rendering context.
I'm pretty sure there's no better way to do it than that without using the obsolete Material constructor. Fortunately, it all happens pretty quickly anyways.