请教大佬一个问题:1.我通过单独更换插槽附件的图片纹理,例如我想更换一把武器,武器是换成功了,Materials上也会增加该图片的材质,但是会多增加一个原始的材质,每换一个插槽附件都会在原来的基础上多增加一个;2.然后我试着用不同皮肤的方式来替换武器,将两个皮肤打包成一个图集,更换是没有问题的。但如果我将皮肤打包成多个图集之后,就会出现1的这种情况,有多个重复的材质。请问这种多个材质的问题要怎么解决?
unity+spine换装问题
要解决在Unity中使用Spine换装时出现的多个材质问题,可以参考以下几种方法:
调整绘制顺序和分组附件:
- 确保绘制顺序和图集纹理一致,减少材质切换。可以参考spine-unity Runtime Documentation: Material Switching and Draw Calls。
- 将附件按绘制顺序分组到图集页面中。
- 尽量将附件打包到尽可能少的图集页面中。
运行时重新打包皮肤:
- 使用
Skin.GetRepackedSkin()
方法在运行时将多个材质合并为一个材质。以下是示例代码:using Spine.Unity.AttachmentTools; // 创建重新打包的皮肤 Skin repackedSkin = collectedSkin.GetRepackedSkin("Repacked skin", skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial, out runtimeMaterial, out runtimeAtlas); collectedSkin.Clear(); // 使用重新打包的皮肤 skeletonAnimation.Skeleton.Skin = repackedSkin; skeletonAnimation.Skeleton.SetSlotsToSetupPose(); skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton); // skeletonMecanim.Update() for SkeletonMecanim // 可选:在多次重新打包操作后清除缓存 AtlasUtilities.ClearCache();
- 详细信息可以参考spine-unity Main Components: Runtime Repacking。
- 使用
启用Multiple CanvasRenderers:
- 如果使用
SkeletonGraphic
组件,并且使用了多个材质(图集纹理),需要启用Multiple CanvasRenderers
。可以在spine-unity documentation中找到相关说明。
- 如果使用
示例场景和脚本:
- 可以查看示例场景
Spine Examples/Other Examples/Mix and Match Skins
和Spine Examples/Other Examples/Mix and Match Equip
,以及使用的MixAndMatch.cs
示例脚本,了解如何在运行时打包多个纹理到一个纹理中。
- 可以查看示例场景
通过以上方法,可以有效减少材质的重复和切换,提高渲染性能。
x_bin As recommended by Spinebot, check out the example scene Spine Examples/Other Examples/Mix and Match Equip
.
This scene demonstrates how to attach a new image to the skeleton using Sprites as the source image instead of the original image in the texture atlas, and how to pack the original image and the added image into a single texture at runtime.
When you play the scene and press the buttons for each piece of equipment, the appearance of Spineboy's corresponding attachments will change and additional materials will be added:
Pressing the Done
button will repack the textures into a single page and combine the used materials into one:
See the example script EquipButtonExample.cs
attached to each button, the EquipSystemExample.cs
attached to the GameObject Mix and Match
, and the EquipsVisualsComponentExample.cs
attached to the GameObject World Spineboy
for how this is actually done.
For more information on the Skin.GetRepackedSkin()
method, please see the documentation: spine-unity 运行时文档 #组合皮肤
x_bin If the skeleton references a lot of material, it will lead to more draw calls and have a negative impact on performance as described in the documentation, but repacking textures into a single texture can reduce this.
Note that the texture is not repacked in this scene until the Done
button is pressed: