• RuntimesUnity
  • Enabling Straight Alpha of Material not working in builds.

Recently, I'm working on a project that will read spine models at runtime, and there is a toggle in UI to control whether the Straight Alpha option of the material is on or not.

Material materialForSpine = new Material(Shader.Find("Spine/Skeleton"));
materialForSpine.EnableKeyword("_StraightAlphaInput");
int isStraightAlphaInt = Convert.ToInt32(isStraightAlpha);
materialForSpine.SetInt("_StraightAlphaInput", isStraightAlphaInt);
SpineAtlasAsset spineAtlasAsset = SpineAtlasAsset.CreateRuntimeInstance(modelAtlas, texture2DArray, materialForSpine, true)
SkeletonDataAsset skeletonDataAsset = SkeletonDataAsset.CreateRuntimeInstance(modelJSON, spineAtlasAsset, true);
skeletonDataAsset.Clear();
skeletonDataAsset.GetSkeletonData(false);

This is the snippet of code that I used.
In editor, everything is fine and toggling also works. But the toggle isn't working after I build it.
I have enabled Strict Shader Variant Matching in order to make program log what happens.
Shader Spine/Skeleton, subshader 0, pass 1, stage vertex: variant <no keywords> not found.
Then this happened.

Thought it might be Texture2D problem, I referred to the instruction on the Internet and wrote a code:
Texture2D addTexture2DTemp = new Texture2D(2, 2, TextureFormat.RGBA32, false);
addTexture2DTemp.LoadImage(File.ReadAllBytes(i));
Texture2D addTexture2D = new Texture2D(addTexture2DTemp.width, addTexture2DTemp.height, TextureFormat.RGBA32, false);
addTexture2D.SetPixels(addTexture2DTemp.GetPixels());
addTexture2D.Apply();
addTexture2D.name = Path.GetFileNameWithoutExtension(i);
texture2DList.Add(addTexture2D);
texture2DArray = texture2DList.ToArray();

But still not working.

Maybe Alpha is Transparency is the problem? But there's no way for me to change the property of Texture2D to true. (Compiler will throw an error.)

Spine/Skeleton is also included in Always Included Shaders in Graphics setting of my project.
Shader Stripping -> Instancing Variants and Batch Renderer Group Variants are set to Keep All.
Also set [Toggle(_STRAIGHT_ALPHA_INPUT)] _StraightAlphaInput("Straight Alpha Texture", Int) = 1, which is 0 by default, but still not working.

Modified original shader to #pragma multi_compile _ _STRAIGHT_ALPHA_INPUT, which is #pragma shader_feature by default, still not working.

After implementing all these measures above, editor player still working fine, but not in builds.

dropdowncontroller.txt
14kB
spine-skeleton.txt
3kB

Script is provided for diagnostic use. (Sorry for my bad coding skill! I have learned C# for only a week.)

Thanks everyone that is willing to help me!

Related Discussions
...

It seems like the issue you're experiencing with the Straight Alpha toggle not working in builds might be related to shader variant stripping. In Unity, shader variants that are not explicitly used in the editor can be stripped from the build to optimize performance. This can cause issues if your build is missing the specific shader variant needed for the Straight Alpha functionality.

To address this, ensure that the shader variant for _STRAIGHT_ALPHA_INPUT is included in your build. You can do this by modifying your shader to use #pragma multi_compile instead of #pragma shader_feature, which you've already done. Additionally, make sure that the shader is included in the "Always Included Shaders" list in your project's Graphics settings, which you've also done.

Another approach is to create a material in the editor with the _STRAIGHT_ALPHA_INPUT keyword enabled and ensure that this material is referenced somewhere in your scenes or prefabs. This can help Unity recognize that the variant is needed and prevent it from being stripped.

If the issue persists, double-check your texture import settings to ensure they match the expected settings for Straight Alpha, as described in the [spine-unity documentation](http://esotericsoftware.com/spine-unity-assets#Advanced

To SpineBot:
Create a material and reference it will work, but it can't be change at runtime.
It will remain the same as what I set in the editor.
What I need is that _STRAIGHT_ALPHA_INPUT can be changed at runtime.

But still thanks!

    jay36065
    (If I create a material in editor and reference it to my script, the straight alpha setting in builds is fixed and is the same as the created material.)

      jay36065 (If I create a material in editor and reference it to my script, the straight alpha setting in builds is fixed and is the same as the created material.)

      Glad to hear you've figured it out. Spinebots answer above was spot-on, shader variant stripping will remove unused (unreferenced) shader branches from your build unless configured otherwise.

        Harald
        Thank you for your reply!
        But I think that this workaround should one day be substituted by the (in some sort) correct method, which is modifying the properties of material used.
        As long as I don’t have any further need or the solution of making Unity not to strip these shader properties is out, I’ll stay with this workaround.

        Still, huge thanks!

          The workaround now is to make two materials, and when user toggles it, it will initialize the object with the other material which contains another properties.

          Like:
          Material A: Straight Alpha ON
          Material B: Straight Alpha OFF

          jay36065 But I think that this workaround should one day be substituted by the (in some sort) correct method, which is modifying the properties of material used.

          @jay36065 From your reply I guess you don't quite understand how shader variants work. There are no material properties like a floating point parameter that can be set at the material. A boolean compile flag like Straight Alpha Texture switches from one compiled shader to a different compiled shader. To be more precise: a shader variant where certain #ifdef preprocessor defines are enabled, which in turn enables certain code branches in the shader code. So during the build and shader variant stripping, any unused shader variants (e.g. 126 of 128 variants for a shader with 7 variant property branches) are removed from the build. If you don't want that, either disable shader variant stripping in your build settings, or ensure that the needed variants are included.

          See the docs here for more info:
          https://docs.unity3d.com/6000.1/Documentation/Manual/shader-variant-stripping.html