In Unity, what's more "lightweight" between manually changing an attachment graphic in code, vs changing a skin in code?

I have a spell where I want 1 of the images in the spine animation to change randomly on spawning. Now imagine 100s of these spells frequently spawning. I can do the random graphic change by changing the attachment in runtime, or changing the skin in runtime.

I know it's probably a bit vague and maybe not much difference in it, but I just wondered if there was an actual, sizeable difference between them?
  • Posts: 12


I suggest digging into the code. It doesn't have to be treated as a black box, it is very straightforward and often you can see what operations are more expensive. To set an attachment you could set Slot attachment. The code for that is here:
spine-runtimes/Slot.cs at 3.8
As you can see, it does very, very little. You need to get the attachment to set, so first you'd use SkeletonData findSkin:
spine-runtimes/SkeletonData.cs at 3.8
You can see it compares each name, so you may want to do it at start up and store the skin(s). Once you have the skin with your attachment (which may be the "default" skin, which contains all attachments not in a skin in the Spine editor), you can use Skin getAttachment:
spine-runtimes/Skin.cs at 3.8
The TryGetValue there is a dictionary lookup, which isn't terribly expensive but you could get out the attachments you need at startup. For example, maybe you put them in a list, then randomly choose one during the game.

Alternatively you can use Skeleton setAttachment:
spine-runtimes/Skeleton.cs at 3.8
This is for convenience and it uses the methods linked above to find the slot, get the attachment from the skin, and set it on the slot. Even doing all that isn't terribly expensive, especially if you just do it once to configure a skeleton and not every frame.

Changing a skin is Skeleton setSkin:
spine-runtimes/Skeleton.cs at 3.8
It does some logic to set attachments in the new skin if they were set in the old skin, so it's probably slightly more work but still very small. It's less flexible too, since you would need to set up the skins with all the combinations of images you need, while setting individual attachments gives you fine grained control.

TL;DR: don't be shy, read the runtime code. Both ways are fast, so speed is not the reason to choose. Skins are better when setting many attachments in predetermined combinations, otherwise setting attachments is probably better.
User avatar

  • Posts: 9491

Return to Unity