Example #1
0
        public static void ShowSkin(SpineArmatureEditor armatureEditor)
        {
            if (armatureEditor.armatureData.skins != null)
            {
                Armature armature = armatureEditor.armature.GetComponent <Armature>();

                Dictionary <Texture, List <SpriteMetaData> > metaDatas = new Dictionary <Texture, List <SpriteMetaData> >();
                List <SpriteRenderer> sprites = new List <SpriteRenderer>();
                List <Image>          images  = new List <Image>();

                int len = armatureEditor.armatureData.skins.Length;
                for (int i = 0; i < len; ++i)
                {
                    SpineData.SkinData skinData = armatureEditor.armatureData.skins[i];
                    foreach (string slotName in skinData.slots.Keys)
                    {
                        Transform slot       = armatureEditor.slotsKV[slotName];
                        Transform skinParent = slot;
                        if (len > 1)
                        {
                            skinParent = slot.Find(skinData.skinName);
                            if (!skinParent)
                            {
                                GameObject skinParentGo = new GameObject(skinData.skinName);
                                skinParentGo.transform.parent        = slot;
                                skinParentGo.transform.localScale    = Vector3.one;
                                skinParentGo.transform.localPosition = Vector3.zero;
                                skinParentGo.transform.localRotation = Quaternion.identity;
                                skinParent = skinParentGo.transform;
                                skinParent.gameObject.SetActive(i == 0);
                            }
                        }
                        SpineData.SlotData slotData = armatureEditor.slotsDataKV[slotName];
                        List <SpineData.SkinAttachment> attachmentDataArr = skinData.slots[slotName];
                        for (int j = 0; j < attachmentDataArr.Count; ++j)
                        {
                            SpineData.SkinAttachment attachmentData = attachmentDataArr[j];
                            TextureFrame             frame          = armatureEditor.m_TextureFrames.GetTextureFrame(attachmentData.textureName);
                            if (attachmentData.type == "region")                         //region,mesh,linkedmesh,boundingBox,path
                            {
                                if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.Default)
                                {
                                    ShowSpriteFrame(frame, attachmentData, slot, skinParent, armatureEditor);
                                }
                                else if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.SpriteRender ||
                                         armatureEditor.displayType == Bone2DSetupEditor.DisplayType.UGUIImage)
                                {
                                    SpriteMetaData metaData = new SpriteMetaData();
                                    metaData.name      = attachmentData.textureName;
                                    metaData.rect      = frame.rect;
                                    metaData.rect.y    = frame.texture.height - metaData.rect.y - metaData.rect.height;
                                    metaData.alignment = (int)SpriteAlignment.Custom;
                                    metaData.pivot     = Vector2.one * 0.5f;
                                    if (!metaDatas.ContainsKey(frame.texture))
                                    {
                                        metaDatas[frame.texture] = new List <SpriteMetaData>();
                                    }
                                    metaDatas[frame.texture].Add(metaData);

                                    if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.SpriteRender)
                                    {
                                        SpriteRenderer sr = ShowUnitySprite(attachmentData, slot, skinParent, metaData, frame);
                                        if (armatureEditor.genMeshCollider)
                                        {
                                            sr.gameObject.AddComponent <BoxCollider2D>();
                                        }
                                        sprites.Add(sr);
                                    }
                                    else
                                    {
                                        Image img = ShowUIImage(attachmentData, slot, skinParent, metaData, frame);
                                        if (armatureEditor.genMeshCollider)
                                        {
                                            img.gameObject.AddComponent <BoxCollider2D>();
                                        }
                                        images.Add(img);
                                    }
                                }
                                else if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.UGUIDefault)
                                {
                                    ShowUIFrame(frame, attachmentData, slot, skinParent, armatureEditor, slotData);
                                }
                            }
                            else if (attachmentData.type == "mesh")
                            {
                                if (frame.rect.width > 0 && frame.rect.height > 0)
                                {
                                    if (armature.isUGUI)
                                    {
                                        ShowUIMesh(frame, attachmentData, slot, skinParent, armatureEditor);
                                    }
                                    else
                                    {
                                        ShowSpriteMesh(frame, attachmentData, slot, skinParent, armatureEditor);
                                    }
                                }
                            }
                            else if (attachmentData.type == "boundingbox")
                            {
                                ShowCustomCollider(attachmentData, slot, skinParent, armatureEditor);
                            }

                            if (string.IsNullOrEmpty(slotData.attachment))
                            {
                                slot.GetComponent <Slot>().displayIndex = -1;
                            }
                            else
                            {
                                if (armatureEditor.isUGUI)
                                {
                                    MaskableGraphic[] renders = slot.GetComponentsInChildren <MaskableGraphic>(true);
                                    for (int p = 0; p < renders.Length; ++p)
                                    {
                                        if (renders[p].name == slotData.attachment)
                                        {
                                            slot.GetComponent <Slot>().displayIndex = p;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    Renderer[] renders = slot.GetComponentsInChildren <Renderer>(true);
                                    for (int p = 0; p < renders.Length; ++p)
                                    {
                                        if (renders[p].name == slotData.attachment)
                                        {
                                            slot.GetComponent <Slot>().displayIndex = p;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.SpriteRender ||
                    armatureEditor.displayType == Bone2DSetupEditor.DisplayType.UGUIImage)
                {
                    if (metaDatas.Count > 0)
                    {
                        foreach (Texture k in metaDatas.Keys)
                        {
                            string          textureAtlasPath = AssetDatabase.GetAssetPath(k);
                            TextureImporter textureImporter  = AssetImporter.GetAtPath(textureAtlasPath) as TextureImporter;
                            textureImporter.maxTextureSize      = 2048;
                            textureImporter.spritesheet         = metaDatas[k].ToArray();
                            textureImporter.textureType         = TextureImporterType.Sprite;
                            textureImporter.spriteImportMode    = SpriteImportMode.Multiple;
                            textureImporter.spritePixelsPerUnit = 100;
                            AssetDatabase.ImportAsset(textureAtlasPath, ImportAssetOptions.ForceUpdate);
                            Object[] savedSprites = AssetDatabase.LoadAllAssetsAtPath(textureAtlasPath);
                            foreach (Object obj in savedSprites)
                            {
                                Sprite objSprite = obj as Sprite;
                                if (objSprite)
                                {
                                    len = sprites.Count;
                                    for (int i = 0; i < len; ++i)
                                    {
                                        if (sprites[i].name.Equals(objSprite.name))
                                        {
                                            sprites[i].sprite = objSprite;
                                        }
                                    }
                                    len = images.Count;
                                    for (int i = 0; i < len; ++i)
                                    {
                                        if (images[i].name.Equals(objSprite.name))
                                        {
                                            images[i].sprite = objSprite;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
 private static void ParseSkins(SpineArmatureEditor armatureEditor, Bones2D.JSONClass armtureObj)
 {
     if (armtureObj.ContainKey("skins"))
     {
         Bones2D.JSONClass    skins     = armtureObj["skins"].AsObject;
         SpineData.SkinData[] skinDatas = new SpineData.SkinData[skins.Count];
         armatureEditor.armatureData.skins = skinDatas;
         int      skinDataCount = 0;
         string[] skinNames     = new string[skins.Count];
         foreach (string skinName in skins.GetKeys())
         {
             Bones2D.JSONClass  skinSlots = skins[skinName].AsObject;
             SpineData.SkinData skinData  = new SpineData.SkinData();
             skinDatas[skinDataCount] = skinData;
             skinData.skinName        = skinName;
             skinNames[skinDataCount] = skinName;
             skinData.slots           = new Dictionary <string, List <SpineData.SkinAttachment> >();
             foreach (string slotName in skinSlots.GetKeys())
             {
                 Bones2D.JSONClass skinAttments = skinSlots[slotName].AsObject;
                 skinData.slots[slotName] = new List <SpineData.SkinAttachment>();
                 foreach (string attachmentName in skinAttments.GetKeys())
                 {
                     Bones2D.JSONClass        attachmentObj = skinAttments[attachmentName].AsObject;
                     SpineData.SkinAttachment attachment    = new SpineData.SkinAttachment();
                     attachment.name = attachmentName;
                     if (attachmentObj.ContainKey("name"))
                     {
                         attachment.realName = attachmentObj["name"].ToString();
                     }
                     else
                     {
                         attachment.realName = attachmentName;
                     }
                     if (attachmentObj.ContainKey("path"))
                     {
                         attachment.textureName = attachmentObj["path"].ToString();
                     }
                     else
                     {
                         attachment.textureName = attachment.realName;
                     }
                     if (attachmentObj.ContainKey("type"))
                     {
                         attachment.type = attachmentObj["type"].ToString();                                                             //region,mesh,linkedmesh,boundingbox,path
                     }
                     if (attachment.type == "path" || attachment.type == "linkedmesh")
                     {
                         continue;
                     }
                     skinData.slots[slotName].Add(attachment);
                     if (attachmentObj.ContainKey("x"))
                     {
                         attachment.x = attachmentObj["x"].AsFloat * armatureEditor.unit;
                     }
                     if (attachmentObj.ContainKey("y"))
                     {
                         attachment.y = attachmentObj["y"].AsFloat * armatureEditor.unit;
                     }
                     if (attachmentObj.ContainKey("scaleX"))
                     {
                         attachment.scaleX = attachmentObj["scaleX"].AsFloat;
                     }
                     if (attachmentObj.ContainKey("scaleY"))
                     {
                         attachment.scaleY = attachmentObj["scaleY"].AsFloat;
                     }
                     if (attachmentObj.ContainKey("width"))
                     {
                         attachment.width = attachmentObj["width"].AsFloat;
                     }
                     if (attachmentObj.ContainKey("height"))
                     {
                         attachment.height = attachmentObj["height"].AsFloat;
                     }
                     if (attachmentObj.ContainKey("rotation"))
                     {
                         attachment.rotation = attachmentObj["rotation"].AsFloat;
                     }
                     if (attachmentObj.ContainKey("color"))
                     {
                         attachment.color = SpineArmatureEditor.HexToColor(attachmentObj["color"].ToString());
                     }
                     if (attachmentObj.ContainKey("hull"))
                     {
                         attachment.hull = attachmentObj["hull"].AsInt;
                     }
                     if (attachmentObj.ContainKey("uvs"))
                     {
                         Bones2D.JSONArray uvsObj = attachmentObj["uvs"].AsArray;
                         attachment.uvs = new Vector2[uvsObj.Count / 2];
                         for (int z = 0; z < uvsObj.Count; z += 2)
                         {
                             Vector2 uv = new Vector2(uvsObj[z].AsFloat, 1 - uvsObj[z + 1].AsFloat);
                             attachment.uvs[z / 2] = uv;
                         }
                     }
                     //triangles
                     if (attachmentObj.ContainKey("triangles"))
                     {
                         Bones2D.JSONArray trianglesObj = attachmentObj["triangles"].AsArray;
                         attachment.triangles = new int[trianglesObj.Count];
                         for (int z = 0; z < trianglesObj.Count; z++)
                         {
                             attachment.triangles[z] = trianglesObj[z].AsInt;
                         }
                     }
                     //vertex
                     if (attachmentObj.ContainKey("vertices"))
                     {
                         Bones2D.JSONArray verticesObj = attachmentObj["vertices"].AsArray;
                         if (attachment.uvs != null && verticesObj.Count > attachment.uvs.Length * 2)
                         {
                             //have weighted
                             List <float> weights = new List <float>();
                             for (int i = 0; i < verticesObj.Count; ++i)
                             {
                                 int boneCount = verticesObj[i].AsInt;
                                 weights.Add(boneCount);
                                 for (int j = 0; j < boneCount * 4; j += 4)
                                 {
                                     weights.Add(verticesObj[i + j + 1].AsInt);                                        //bone index
                                     weights.Add(verticesObj[i + j + 2].AsFloat * armatureEditor.unit);                //relativeBoneX
                                     weights.Add(verticesObj[i + j + 3].AsFloat * armatureEditor.unit);                //relativeBoneY
                                     weights.Add(verticesObj[i + j + 4].AsFloat);                                      //weight
                                 }
                                 i += boneCount * 4;
                             }
                             attachment.weights = weights;
                         }
                         else
                         {
                             //only vertices
                             attachment.vertices = new Vector3[verticesObj.Count / 2];
                             for (int i = 0; i < verticesObj.Count; i += 2)
                             {
                                 Vector3 vertex = new Vector3(verticesObj[i].AsFloat, verticesObj[i + 1].AsFloat, 0f);
                                 vertex.x *= armatureEditor.unit;
                                 vertex.y *= armatureEditor.unit;
                                 attachment.vertices[i / 2] = vertex;
                             }
                         }
                     }
                     //edges
                     if (armatureEditor.genMeshCollider && attachmentObj.ContainKey("edges"))
                     {
                         Bones2D.JSONArray edgesObj = attachmentObj["edges"].AsArray;
                         int        len             = edgesObj.Count;
                         List <int> edges           = new List <int>();
                         for (int z = 0; z < len; ++z)
                         {
                             int value = edgesObj[z].AsInt / 2;
                             if (edges.Count > 0)
                             {
                                 if (edges[edges.Count - 1] != value)
                                 {
                                     edges.Add(value);
                                 }
                             }
                             else
                             {
                                 edges.Add(value);
                             }
                         }
                         if (edges.Count > 2 && edges[0] == edges[edges.Count - 1])
                         {
                             edges.RemoveAt(edges.Count - 1);
                         }
                         if (edges.Count > 2)
                         {
                             attachment.edges = edges.ToArray();
                         }
                     }
                 }
             }
             ++skinDataCount;
         }
         armatureEditor.armature.GetComponent <Armature>().skins = skinNames;
     }
 }