• RuntimesUnity
  • Replacing Sprite sometimes not working in SkeletonGraphic

I need to dynamically change sprite in an attachment. It alywas shows wrong results in SkeletonGraphic, meanwhile the result in SkeletonAnimation is correct.
As the image shows, the left is SkeletonAnimation and the right is SkeletonGraphic.

I have another Spine file which works well in both SkeletonGraphic and SkeletonAnimation. So I'm confusing what's the difference between different Spines?

    Fish Does that skeleton data have multiple atlas pages? If so, that wrong results would occur because SkeletonGraphic is limited to a single texture by default. If you want to display skeleton data that has multiple atlas pages using the SkeletonGraphic component, you need to enable Advanced - Multiple CanvasRenderers at the SkeletonGraphic component Inspector. However, this is not recommended for performance reasons. This means Skeletons used in UI shall be packed as a single-texture (single-page) atlas, rather than multi-page atlases. See section "Advanced - Single Atlas Texture Export and SkeletonGraphic" on how to pack attachments to a single atlas page texture:
    http://esotericsoftware.com/spine-unity#Advanced---Single-Texture-Atlas-Export-and-SkeletonGraphic

    For your information, this limitation is also explained in the following video (from 05:56 to 07:36):

    • Responderam a isso Fish.

      Misaki No. This skeleton data does not have multiple atlas pages. I can upload the files.

      archive.zip
      1MB

      This is my code:

              Skeleton skeleton = m_Spine.Skeleton;
              SkeletonData skeletonData = skeleton.Data;
      
              Material sourceMaterial = m_Spine.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial;
      
              // 当前的skin作为模板
              Skin templateSkin = skeletonData.FindSkin("1");
              Skin currentEquipsSkin = new Skin("my custom skin");
              currentEquipsSkin.AddSkin(templateSkin);
      
              Slot slot = skeleton.FindSlot("pic");
              int slotIndex = skeleton.Slots.IndexOf(slot);
              Attachment attachment = templateSkin.GetAttachment(slotIndex, "pic");
              Attachment newAttachment = attachment.GetRemappedClone(m_CardSprite, sourceMaterial);
              currentEquipsSkin.SetAttachment(slotIndex, "pic", newAttachment);
      
              // 重新设置图片(Graphic下必须)
              currentEquipsSkin = currentEquipsSkin.GetRepackedSkin("repacked skin", sourceMaterial,
                  out Material runtimeMaterial, out Texture2D runtimeAtlas);
              // 储存skin
              skeleton.SetSkin(currentEquipsSkin);
              skeleton.SetSlotsToSetupPose();
              m_Spine.Update(0);

        Fish Thank you for sharing your skeleton files and code! I have checked them and it appears that you forgot to add the attachments in the default skin to "currentEquipsSkin". This is causing the images used in the default skin to be lacking in the repacked texture so the result is misaligned.

        You probably created that code in reference to MixAndMatchGraphic.cs, and in MixAndMatchGraphic.cs, this is done at line 122:
        repackedSkin.AddAttachments(skeleton.Data.DefaultSkin);
        https://github.com/EsotericSoftware/spine-runtimes/blob/d33c10f85634d01efbe4a3ab31dabaeaca41230c/spine-unity/Assets/Spine%20Examples/Scripts/MixAndMatchGraphic.cs#L122

        I hope this will help you.

        • Responderam a isso Fish.

          Fish I'm glad to hear that it works! Thank you for getting back to us 🙂

          • Responderam a isso Fish.
            2 meses depois

            Misaki I'm sorry to disturb again. It works in Editor, but appears strange in iOS device.
            The result shows as below:

            I guess it is the problem in currentEquipsSkin.GetRepackedSkin Because when I set texture's format to RGBA Compressed ETC2, it shows the same result.

            The version of runtime code is 'spine-unity-3.8-2021-11-10.unitypackage'.

              Fish It appears to be the same problem as described in the "Why does my repacked skin display in the Editor but shows white polygons in the built executable?" section of the FAQ: https://esotericsoftware.com/spine-unity#Visual

              As described there, I guess you are missing something in the "Important Note" in the "Runtime Repacking" section of the documentation. Could you please confirm it first?
              If the problem is not resolved after checking that, we would appreciate it if you could send us via email a minimal repro project: contact@esotericsoftware.com

              @Fish Thanks for sending the reproduction project.

              It seems that you've set up the Texture import settings of the Sprite's Texture Assets/颜色分类.png as required for repacking, but have not done the same for the second texture that contributes attachments, the Assets/CardSpine/skeleton.png texture. If at the skeleton.png texture you also enable Read/Write Enabled and set the format to a non-compressed format like RGBA 32 bit, it worked on our end.

              Please let us know if this resolves the issue on your end as well.

              • Responderam a isso Fish.

                Harald After I apply the same setting to both skeleton.png and replaced sprite, it worked.

                Thanks.

                @Fish Glad to hear, thanks for getting back to us.