1. 程式人生 > >Unity3D圖片換裝方案

Unity3D圖片換裝方案

-------------------------------------

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<SkeletonAnimation>();
      m = CreateTextureSizeAttachmentByTexture(_skeletonAnimation.skeleton.FindSlot(slot), texture);                       
}

image.png

但是這樣不會根據圖片大小換裝 所以我們要重新計算Attachment

      attachment.UpdateOffsetByTexture2D(attachment, texture); 

以上就是unity裡Spine圖片換裝的思路和解決方法,歡迎交流~
End .