How to mix spine skins?

How to mix spine skins?
0

Hello,

I’m looking for a way to mix spine skins.

An ideal way for me is to use Skin API documented here: http://ja.esotericsoftware.com/spine-api-reference#Skin

a sample code is like this. (this sample code is used in a lua project)

    local skinNames = ["headA", "bodyB", "legC"]
    local mixedSkin = Spine.Skin.new("mixed")
    for _, skinName in pairs(skinNames) do
        local skin = self.skeleton.data:findSkin(skinName)
        local attachmentsA = skin.attachments
        for slotIndex, attachments in pairs(attachmentsA) do
            for name, attachment in pairs(attachments) do
                mixedSkin:addAttachment(slotIndex, name, attachment)
            end
        end
    end
    self.skeleton:setSkinByReference(mixedSkin)

but it seems that cocos creator’s API does not provides Skin class nor Skeleton.setSkin(skin) according to this page: https://docs.cocos2d-x.org/creator/api/en/classes/Skeleton.html

Is there a way to mix skins?

Thanks.

Maybe there’s an elegant way to import the engine module, then get the Skin prototype from spine.Skin.

cocos-creator/engine/blob/master/extensions/spine/lib/spine.js

However, at least here’s a hacky way to get the Skin prototype through the default skin object.

    // Get spine component.
    var skeletonComp = this.getComponent('sp.Skeleton');
    // Find default skin.
    var _skeleton = skeletonComp._skeleton;
    var defaultSkin = _skeleton.data.findSkin('default');
    // Get skin prototype (you can cache it for later use).
    let SkinProto = Object.getPrototypeOf(defaultSkin);

Then create a new skin object with prototype.

    let mixedSkin = Object.create(SkinProto);
    mixedSkin.name = 'mixed';
    mixedSkin.attachments = [];

For the rest follow the lua script you shared.