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