Spine是一款專門為軟件和游戲開發(fā)設(shè)計,量身打造的2D動畫軟件牍戚。動畫師,原畫師和程序共同為您的游戲賦予生命侮繁。
http://zh.esotericsoftware.com/spine-using-runtimes
這次教大家如何在Unity里使用圖片進(jìn)行Spine換裝。
Spine本身提供了2個方法來換裝如孝,分別是換全套和換某部分宪哩。
這兩個方法都是使用了Spine自身的AtlasRegion,這種情況下沒有美工輸出AtlasRegion就不能隨意使用任意圖片進(jìn)行換裝第晰。
現(xiàn)在先搞清楚Spine的設(shè)置關(guān)系锁孟,首先是有了AtlasRegion,里面有許多組成一個個體(角色)的各部分的Slot茁瘦,然后Slot里包含Attachment品抽,里面又存放了圖片信息。
所以我們的換裝思路是:使用Texture2D自建Spine的AtlasRegion甜熔,然后獲取要換掉的Slot圆恤,最后對Slot的Attachment進(jìn)行換圖,并重新計算圖片顯示腔稀。
①
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);
}
但是這樣不會根據(jù)圖片大小換裝 所以我們要重新計算Attachment
attachment.UpdateOffsetByTexture2D(attachment, texture);
以上就是unity里Spine圖片換裝的思路和解決方法,歡迎交流~
End .