你好,我在Unity中使用SkeletonGraphic组件时遇到了这样的问题,
Hello,I have a trouble when using SkeletonGraphic component in Unity,
它的Spine模型在UnityEditor中显示正常,而在Build后运行时出现了贴图丢失的问题(PC平台),如下图:
the skeleton cannot displaying correctly after build and run,but it works right in UnityEditor.
UnityEditor中:
Running in UnityEditor:
Build后运行时:
Runing after build:
调用代码为:
The coding is:
public static void SetSpineAvatar(UnitSpineData sdata)
{
var obj = CreateSpineAvatarPrefab(); // Instantiate perfab
var sg = obj.GetComponent<SkeletonGraphic>();
SkeletonDataAsset sda = CreateSkeletonData(sdata.ModelName); // Create SkeletonDataAsset
sg.skeletonDataAsset = sda;
sg.Initialize(true);
var dic = ChangeSlotAttachments(sdata, sg.Skeleton); // Change attachments
//Repack skin
RepackSkeletonSkin(sg.Skeleton, sg.skeletonDataAsset, out Material runtimeMaterial, out Texture2D runtimeAtlas);
sg.material = runtimeMaterial;
sg.material.shader = Shader.Find("Spine/SkeletonGraphic");
sg.OverrideTexture = runtimeAtlas;
sg.AnimationState.SetAnimation(0, "idle", true);
}
private static void RepackSkeletonSkin(Skeleton skeleton, SkeletonDataAsset dataAsset, out Material runtimeMaterial, out Texture2D runtimeAtlas)
{
var repackedSkin = new Skin("repacked skin");
repackedSkin.AddAttachments(skeleton.Data.DefaultSkin);
repackedSkin = repackedSkin.GetRepackedSkin("repacked skin", dataAsset.atlasAssets[0].PrimaryMaterial, out runtimeMaterial, out runtimeAtlas);
skeleton.SetSkin(repackedSkin);
skeleton.SetSlotsToSetupPose();
}
我发现在UnityEditor中重新打包后的皮肤材质贴图如下:
I found the runtime material and altas of repacked skin is a image like this in UnityEditor:
但在Build后运行的程序中变成了这样:
But it cannot work correctly after the project build:
如果我移除重新打包皮肤的代码,SkeletonGraphic就可以加载贴图,但是你懂的……
If I remove the RepackSkeletonSkin code,SkeletonGraphic component can load texture instead of white square,but you know SkeletonGraphic component supports only a single atlas texture...
在UnityEditor中显示的SkeletonGraphic和Shader的参数如下:
The properties of SkeletonGraphic component and its shader (in UnityEditor):
同样的Spine模型在使用SkeletonAnimation组件时无论是UnityEditor中或是Build后都显示正常。
The same skeleton can displaying correctly when using SkeletonAnimation component after build,but I need use it in UI with SkeletonGraphic component.
已经检查过Unity打包时引入的Shader设置,但问题没有得到解决。
I have checked the "Always included shaders" setting in "Built-in shader settings",but the problem was not solved.
我的Unity版本是2019.3.14f1,Spine版本是spine-unity-3.8-2019-08-20.unitypackage
My unity version is 2019.3.14f1, spine runtime version is spine-unity-3.8-2019-08-20.unitypackage.
好的吧,我很高兴地发现它只是出现了配置问题,在打包时需要把所有用到的材质设置为可读写的。
unity的一些编辑器和实际打包运行之间的没有提示的小细节差异挺搞人心态……