Spine是一款专门为软件和游戏开发设计,量身打造的2D动画软件。动画师,原画师和程序共同为您的游戏赋予生命。
http://zh.esotericsoftware.com/spine-using-runtimes
这次教大家如何在Unity里使用图片进行Spine换装。
Spine本身提供了2个方法来换装,分别是换全套和换某部分。
这两个方法都是使用了Spine自身的AtlasRegion,这种情况下没有美工输出AtlasRegion就不能随意使用任意图片进行换装。
现在先搞清楚Spine的设置关系,首先是有了AtlasRegion,里面有许多组成一个个体(角色)的各部分的Slot,然后Slot里包含Attachment,里面又存放了图片信息。
所以我们的换装思路是:使用Texture2D自建Spine的AtlasRegion,然后获取要换掉的Slot,最后对Slot的Attachment进行换图,并重新计算图片显示。
①
private AtlasRegion CreateRegion(Texture2D texture)
{
Spine.AtlasRegion region = new AtlasRegion();
region.width = texture.width;
region.height = texture.height;
region.originalWidth = texture.width;
region.originalHeight = texture.height;
region.rotate = false;
region.page = new AtlasPage();
region.page.name = texture.name;
region.page.width = texture.width;
region.page.height = texture.height;
region.page.uWrap = TextureWrap.ClampToEdge;
region.page.vWrap = TextureWrap.ClampToEdge;
return region;
}
②
Material CreateRegionAttachmentByTexture(Slot slot, Texture2D texture)
{
if (slot == null) { return null; }
if (texture == null) { return null; }
RegionAttachment attachment = slot.Attachment as RegionAttachment;
if (attachment == null) { return null; }
attachment.RendererObject = CreateRegion(texture);
attachment.SetUVs(0f, 1f, 1f, 0f, false);
Material mat = new Material(Shader.Find("Sprites/Default"));
mat.mainTexture = texture;
(attachment.RendererObject as AtlasRegion).page.rendererObject = mat;
slot.Attachment = attachment;
return mat;
}
Material CreateMeshAttachmentByTexture(Spine.Slot slot, Texture2D texture)
{
if (slot == null) return null;
MeshAttachment oldAtt = slot.Attachment as MeshAttachment;
if (oldAtt == null || texture == null) return null;
MeshAttachment att = new MeshAttachment(oldAtt.Name);
att.RendererObject = CreateRegion(texture);
att.Path = oldAtt.Path;
att.Bones = oldAtt.Bones;
att.Edges = oldAtt.Edges;
att.Triangles = oldAtt.triangles;
att.Vertices = oldAtt.Vertices;
att.WorldVerticesLength = oldAtt.WorldVerticesLength;
att.HullLength = oldAtt.HullLength;
att.RegionRotate = false;
att.RegionU = 0f;
att.RegionV = 1f;
att.RegionU2 = 1f;
att.RegionV2 = 0f;
att.RegionUVs = oldAtt.RegionUVs;
att.UpdateUVs();
Material mat = new Material(Shader.Find("Sprites/Default"));
mat.mainTexture = texture;
(att.RendererObject as Spine.AtlasRegion).page.rendererObject = mat;
slot.Attachment = att;
return mat;
}
③
Material m;
string slot;
Texture2D texture;
void SetSkin()
{
_skeletonAnimation = GetComponent<SkeletonAnimation>();
m = CreateTextureSizeAttachmentByTexture(_skeletonAnimation.skeleton.FindSlot(slot), texture);
}
但是这样不会根据图片大小换装 所以我们要重新计算Attachment
attachment.UpdateOffsetByTexture2D(attachment, texture);
以上就是unity里Spine图片换装的思路和解决方法,欢迎交流~
End .