spine 换装接口 setAttachment
首先你在制作骨骼的时候,需要给一个 slot 设置多个 attachment,spine在一个时刻只会显示其中的一个 attachment,动态切换 attachment 调用 SkeletonRenderer::setAttachment
接口。这个接口自动绑定并未提供,社区版手动绑定提供给开发者使用。
接口示例:(注:只有第一个参数时,表示去掉slot的Attachment,不显示图片)
local hero = sp.SkeletonAnimation:create("build_yellowlightfinished.json", "build_yellowlightfinished.atlas")
hero:setAttachment("changegun", "pc_gungirl_crossbow3")
c++:
SkeletonRenderer.cpp
bool SkeletonRenderer::setAttachment (const std::string& slotName, const std::string& attachmentName) {
return spSkeleton_setAttachment(_skeleton, slotName.c_str(), attachmentName.empty() ? 0 : attachmentName.c_str()) ? true : false;
}
Skeleton.c
int spSkeleton_setAttachment (spSkeleton* self, const char* slotName, const char* attachmentName) {
int i;
for (i = 0; i < self->slotsCount; ++i) {
spSlot *slot = self->slots[i];
if (strcmp(slot->data->name, slotName) == 0) {
if (!attachmentName)
spSlot_setAttachment(slot, 0);
else {
spAttachment* attachment = spSkeleton_getAttachmentForSlotIndex(self, i, attachmentName);
if (!attachment) return 0;
spSlot_setAttachment(slot, attachment);
}
return 1;
}
}
return 0;
}
void spSlot_setAttachment (spSlot* self, spAttachment* attachment) {
if (attachment == self->attachment) return;
CONST_CAST(spAttachment*, self->attachment) = attachment;
SUB_CAST(_spSlot, self)->attachmentTime = self->bone->skeleton->time;
self->attachmentVerticesCount = 0;
}
总结:
制作spine时 一个插槽slot上需要预制多个attachment附件,需要切换显示时 调用setAttachment方法,第一个参数slotName,第二个参数 attachmentName。
最终是spSlot对象 调用了spSlot_setAttachment方法。