- Home /
Shader.Find() equivalent for Compute Shaders?
I'd like to reference a Compute Shader from within a script without having to assign it to a public variable using the editor. This can be done with normal shaders using Shader.Find(). Is there an equivalent for Compute Shaders?
Answer by NavyFish · Jul 06, 2015 at 08:45 AM
Turns out you can load it like any other asset. Put the compute shader in a "Resources" folder, and then load it during runtime with:
ComputeShader cs = (ComputeShader)Resources.Load("NameOfShader")
Do not include the file's extension (i.e *.compute).
Answer by handypandy · Feb 15, 2019 at 09:05 PM
ComputeShader cs;
string shaderName = "name_of_shader";
ComputeShader[] compShaders = (ComputeShader[])Resources.FindObjectsOfTypeAll(typeof(ComputeShader));
for (int i = 0; i < compShaders.Length; i++)
{
if (compShaders[i].name == shaderName)
{
cs = compShaders[i];
break;
}
}
Ok, even though it sounds like this would work, right off the top of my head, this is a bad idea for some use cases. Specifically, this will force adding the compute shader to builds that don't need the compute shader. If the compute shader's only use is for an editor level process, then there really is no need to include it in the builds.