I have a shader graph with multiple boolean keywords, and some material in included scenes which use this shader graph. The keywords are set to “multi-compile” in shader graph. However, if I switch the boolean keyword in build, the variant gets not activated. I would assume that I don’t need materials with all variants in included scenes if I use multi-compile in shader graph?
Yes, it works in Play mode. Funnily, I just tested, and on restart of Editor, the bool switches on the material did NOT work in Scene view. Than I started in play mode, and there the switches (toggled by script), worked fine, but shader got newly compiled. After stopping, it worked also in Scene view…
Keywords are local, I set them from script like this:
var shader = material.shader;
LocalKeyword keyword = shader.keywordSpace.FindKeyword(name);
if (keyword != null)
{
material.EnableKeyword(name);
material.SetKeyword(keyword, on);
}
References are set by the Unity Hair package (which is still in experimental mode afaik); maybe this is the issue?
One other thing I noticed is that before I tried the above, I was referencing two different materials (both from hair script), one with the keywords enabled, and one not. Even than it did not work in build.
I don’t think that’s the issue, it worked before in older builds, also the gameobjects and the material get’s never destroyed or unloaded.
Only after updating the shader, and with this triggering a re-compilation, the compilation in build seems incomplete.
in fact, after I triggered the compilation manually in play mode, and now building again, I can see the extra compilation of the shader happening as it should!
So there must be some sort of issue in the editor, maybe it’s really the way the hair package is doing the referencing?
you mean the managed stripping level? No, it’s set to “minimal”.
Also as an update: After manually triggering recompile in Editor, and then building, it now works in build. Very likely until next time I update the shader.
Another update: I was already referencing the material also via a normal Script reference (as “public Material material”). So the referencing by the hair package can’t be the issue either. The only thing different to let’s say “classical” usage of Material is that it’s not referenced by any MeshRenderer in the scene.
Is it necessary to have a reference from a MeshRenderer in order to trigger the multi-compilation? Would be strange if it is IMO.
Any reference of a Material instance, from a MonoBehaviour or ScriptableObject or any other should make that Shader compile its variants at build.
If it doesn’t, it’s a bug.
Which version of Unity are you using?