• Unity
  • Change skin of spine cause FPS drop

Related Discussions
...

FPS will drop when change 3 more spine per second.
I print the program running time to find that GetRemappedClone() and SetAttachment() is used the most time.
How to optimize?

Thanks for your help!

Attach My Code:

public void InitSkeletonSkin (List<string> changeKeyList, List<Sprite> changeSpriteList) {
Skin customSkin = new Skin("customSkin");

for (int i = 0; i < changeKeyList.Count; ++i)
{
    var changeKey = changeKeyList[i];
    int changeSlotIndex = skeleton.FindSlotIndex(changeKey);

    Attachment baseAttachment = baseSkin.GetAttachment(changeSlotIndex, changeKey);
    var changeSprite = changeSpriteList[i];
    Attachment newAttachment = baseAttachment.GetRemappedClone(changeSprite, sourceMaterial);
    if (newAttachment != null)
    customSkin.SetAttachment(changeSlotIndex, changeKey, newAttachment);
}

repackedSkin = new Skin("repackedSkin");
repackedSkin.AddAttachments(skeleton.Data.DefaultSkin);
repackedSkin.AddAttachments(customSkin);
if (runtimeMaterial)
    Destroy(runtimeMaterial);
if (runtimeAtlas)
    Destroy(runtimeAtlas);
repackedSkin = repackedSkin.GetRepackedSkin("repackedSkin", sourceMaterial, out runtimeMaterial, out runtimeAtlas);
skeleton.SetSkin(repackedSkin);

skeleton.SetToSetupPose();
skeletonGraphic.Update(0);
skeletonGraphic.OverrideTexture = runtimeAtlas;

}

Regarding formatting: please use the three backticks

```

before and after your pasted code, or use the code tags:

[code]
code goes here

[/code] or the code button in the Full Editor to format your pasted code, otherwise your code indentation is messed up and unreadable.

joeplayer wrote

FPS will drop when change 3 more spine per second.

I assume you mean that when changing skins at 3 or more Spine skeletons, then your FPS will drop, is this correct?

In general, please note that GetRepackedSkin() is a very costly operation, it packs images to a new texture and uploads it to your graphics card's VRAM. In general you should perform these repack operations in non-critical times like when loading a level where possible. Where this is not possible, to avoid too many costly repack operation you could:

  • a) Avoid repacking and only assign the new Skin normally. This will most likely increase your draw calls and is a tradeoff.
  • b) First assign the skin normally and then distribute multiple repacking operations across multiple frames. So only repack one skin per frame, in the next frame repack the next skin, etc.
  • c) Rearrange your atlas images (check your draw order and check how often it would switch from one atlas to another) so that you have only few draw calls and don't need to repack at all.
12 days later