spine共享骨骼

发布时间 2023-11-28 16:32:01作者: mc宇少

项目中遇到使用多个相同spine的问题:

我们需要获取骨骼位置的时候,要拿下面这个类的信息

 spine工具给的更新方案是:每个spine在Update中每帧更新,根据当前spine更新骨骼信息。

这样比较费,比如我们项目场景中有五个角色,每个角色有五个编制,那光友方单位就是25个spine,很难蚌。

优化方案:每个角色设置一个主体spine,主体spine在Update中更新数据,剩下的4个相同spine在获取骨骼接口的时候同步一下主体spine的位置就可以(反正都是相对坐标,都是一致的)。

public void CopyMainframeSkeletonData()
	{
         //主体spine
        if (mainframeSkeletonAnimation.skeleton == null) return;
        if (skeleton == mainframeSkeletonAnimation.skeleton) return;
        for (int i = 0, j = skeleton.bones.Count; i < j; i++)
        {
            Bone thisBone = skeleton.bones[i];
            Bone sharedBone = mainframeSkeletonAnimation.skeleton.bones[i];
            thisBone.x = sharedBone.x;
            thisBone.y = sharedBone.y;
            thisBone.rotation = sharedBone.rotation;
            thisBone.rotationIK = sharedBone.rotationIK;
            thisBone.scaleX = sharedBone.scaleX;
            thisBone.scaleY = sharedBone.scaleY;
            thisBone.flipX = sharedBone.flipX;
            thisBone.flipY = sharedBone.flipY;
            thisBone.m00 = sharedBone.m00;
            thisBone.m01 = sharedBone.m01;
            thisBone.m10 = sharedBone.m10;
            thisBone.m11 = sharedBone.m11;
            thisBone.worldX = sharedBone.worldX;
            thisBone.worldY = sharedBone.worldY;
            thisBone.worldRotation = sharedBone.worldRotation;
            thisBone.worldScaleX = sharedBone.worldScaleX;
            thisBone.worldScaleY = sharedBone.worldScaleY;
            thisBone.worldFlipX = sharedBone.worldFlipX;
            thisBone.worldFlipY = sharedBone.worldFlipY;
        }
        // 复制slot信息
        for (int i = 0, j = skeleton.slots.Count; i < j; i++)
        {
            Slot thisSlot = skeleton.slots[i];
            Slot sharedSlot = mainframeSkeletonAnimation.skeleton.slots[i];
            thisSlot.Attachment = sharedSlot.Attachment == null ? null : skeleton.GetAttachment(i, sharedSlot.Attachment.Name);
        }
        // 复制order信息
        if (mainframeSkeletonAnimation.skeleton.drawOrderToSetupIndex == null)
        {
            skeleton.drawOrder.Clear();
            skeleton.drawOrder.AddRange(skeleton.slots);
        }
        else
        {
            for (int k = 0, n = mainframeSkeletonAnimation.skeleton.drawOrderToSetupIndex.Length; k < n; k++)
                skeleton.drawOrder[k] = skeleton.slots[mainframeSkeletonAnimation.skeleton.drawOrderToSetupIndex[k]];
        }
    }

  在获取骨骼位置的时候调用一下这个函数刷新就可以了。