Spine是一款专门为软件和游戏开发设计,量身打造的2D动画软件。动画师,原画师和程序共同为您的游戏赋予生命。
官网介绍:http://zh.esotericsoftware.com/spine-using-runtimes
-------------------------------------
Spine本身提供了2个方法来换装,分别是换全套和换某部分。
这两个方法都是使用了Spine自身的AtlasRegion,这种情况下没有美工输出AtlasRegion就不能随意使用任意图片进行换装。
-------------------------------------
所以这次教大家如何在Unity里使用图片进行Spine换装。
现在先搞清楚Spine的设置关系,一个Spine动画由AtlasRegion(图集)、png、json/byte(图集配置信息)组成。
首先是有了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();
m = CreateTextureSizeAttachmentByTexture(_skeletonAnimation.skeleton.FindSlot(slot), texture);
}
image.png
但是这样不会根据图片大小换装 所以我们要重新计算Attachment
attachment.UpdateOffsetByTexture2D(attachment, texture);
以上就是unity里Spine图片换装的思路和解决方法,欢迎交流~
End .