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).
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.