请教大佬一个问题:1.我通过单独更换插槽附件的图片纹理,例如我想更换一把武器,武器是换成功了,Materials上也会增加该图片的材质,但是会多增加一个原始的材质,每换一个插槽附件都会在原来的基础上多增加一个;2.然后我试着用不同皮肤的方式来替换武器,将两个皮肤打包成一个图集,更换是没有问题的。但如果我将皮肤打包成多个图集之后,就会出现1的这种情况,有多个重复的材质。请问这种多个材质的问题要怎么解决?

    Related Discussions
    ...

    要解决在Unity中使用Spine换装时出现的多个材质问题,可以参考以下几种方法:

    1. 调整绘制顺序和分组附件

    2. 运行时重新打包皮肤

      • 使用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
    3. 启用Multiple CanvasRenderers

      • 如果使用SkeletonGraphic组件,并且使用了多个材质(图集纹理),需要启用Multiple CanvasRenderers。可以在spine-unity documentation中找到相关说明。
    4. 示例场景和脚本

      • 可以查看示例场景Spine Examples/Other Examples/Mix and Match SkinsSpine Examples/Other Examples/Mix and Match Equip,以及使用的MixAndMatch.cs示例脚本,了解如何在运行时打包多个纹理到一个纹理中。

    通过以上方法,可以有效减少材质的重复和切换,提高渲染性能。

    • x_bin hat auf diesen Beitrag geantwortet.

      x_bin 我用的是spine3.8版本的,我看官方的MixAndMatchEquip教程也会出现多个重复材质的情况

      Spinebot 我看官方的MixAndMatchEquip教程也会出现多个重复材质的情况

      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 hat auf diesen Beitrag geantwortet.

        Misaki
        那这里增加的重复材质是不是可以不用理会,不会影响性能?

        • Misaki hat auf diesen Beitrag geantwortet.

          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:

          • x_bin hat auf diesen Beitrag geantwortet.

            Misaki 明白什么意思了,换装完之后需要执行Done操作才能把多个材质打包成一个材质,谢谢!