Beispiel #1
0
        private static void ShowCustomCollider(SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor)
        {
            GameObject go = new GameObject(attachmentData.name);

            go.transform.parent = skinParent;
            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;
            go.transform.localScale = localSc;

            go.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation);

            if (armatureEditor.genCustomCollider)
            {
                PolygonCollider2D collider = go.AddComponent <PolygonCollider2D>();
                Vector2[]         points   = new Vector2[attachmentData.vertices.Length];
                int len = points.Length;
                for (int i = 0; i < len; ++i)
                {
                    points[i] = (Vector2)attachmentData.vertices[i];
                }
                collider.SetPath(0, points);
            }
        }
Beispiel #2
0
        private static Image ShowUIImageSingle(Sprite sprite, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, TextureFrame frame)
        {
            GameObject go       = new GameObject(attachmentData.name);
            Image      renderer = go.AddComponent <Image>();

            renderer.sprite        = sprite;
            renderer.raycastTarget = false;
            renderer.SetNativeSize();
            renderer.material = frame.uiMaterial;
            go.transform.SetParent(skinParent);
            BoxCollider2D col = go.GetComponent <BoxCollider2D>();

            if (col)
            {
                col.size = renderer.rectTransform.sizeDelta;
            }

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;

            if (frame.isRotated)
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.height;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.width;
                }
            }
            else
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.width;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.height;
                }
            }
            go.transform.localScale    = localSc;
            renderer.color             = attachmentData.color;
            go.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation + (frame.isRotated?-90f:0f));

            return(renderer);
        }
Beispiel #3
0
        private static SpriteRenderer ShowUnitySpriteSingle(Sprite sprite, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, TextureFrame frame)
        {
            GameObject     go       = new GameObject(attachmentData.name);
            SpriteRenderer renderer = go.AddComponent <SpriteRenderer>();

            renderer.material   = frame.material;
            renderer.sprite     = sprite;
            go.transform.parent = skinParent;

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;

            if (frame.isRotated)
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.height;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.width;
                }
            }
            else
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.width;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.height;
                }
            }
            go.transform.localScale = localSc;

            renderer.color = attachmentData.color;

            go.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation + (frame.isRotated?-90f:0f));

            return(renderer);
        }
Beispiel #4
0
        static void ShowSpriteMesh(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor)
        {
            GameObject go = new GameObject(attachmentData.name);
            SpriteMesh sm = go.AddComponent <SpriteMesh>();

            sm.transform.parent = skinParent;

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;
            go.transform.localScale = localSc;

            go.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation);

            Transform[] bones = SetMeshVertex <SpriteMesh>(sm, attachmentData, armatureEditor);
            sm.uvs       = attachmentData.uvs;
            sm.triangles = attachmentData.triangles;
            sm.colors    = new Color[sm.vertices.Length];
            for (int i = 0; i < sm.colors.Length; ++i)
            {
                sm.colors[i] = Color.white;
            }
            if (armatureEditor.genMeshCollider && attachmentData.edges != null)
            {
                sm.edges = attachmentData.edges;
            }
            if (attachmentData.weights != null && attachmentData.weights.Count > 0)
            {
                sm.CreateMesh();
                if (armatureEditor.ffdKV.ContainsKey(attachmentData.textureName))
                {
                    //Vertex controller
                    sm.vertControlTrans = new Transform[sm.vertices.Length];
                    for (int i = 0; i < sm.vertices.Length; ++i)
                    {
                        GameObject gov = new GameObject(go.name + "_v" + i);
                        gov.transform.parent        = go.transform;
                        gov.transform.localPosition = sm.vertices[i];
                        gov.transform.localScale    = Vector3.zero;
                        sm.vertControlTrans[i]      = gov.transform;
                        gov.SetActive(false);
                    }
                }
            }
            else
            {
                sm.CreateMesh();
                //Vertex controller
                sm.vertControlTrans = new Transform[sm.vertices.Length];
                for (int i = 0; i < sm.vertices.Length; ++i)
                {
                    GameObject gov = new GameObject(go.name + "_v" + i);
                    gov.transform.parent        = go.transform;
                    gov.transform.localPosition = sm.vertices[i];
                    gov.transform.localScale    = Vector3.zero;
                    sm.vertControlTrans[i]      = gov.transform;
                    gov.SetActive(false);
                }
            }

            if (attachmentData.weights != null && attachmentData.weights.Count > 0)
            {
                List <Armature.BoneWeightClass> boneWeights = new List <Armature.BoneWeightClass>();
                for (int i = 0; i < attachmentData.weights.Count; ++i)
                {
                    int boneCount = (int)attachmentData.weights[i];                    //骨骼数量
                    List <KeyValuePair <int, float> > boneWeightList = new List <KeyValuePair <int, float> >();
                    for (int j = 0; j < boneCount * 2; j += 2)
                    {
                        int   boneIdx = (int)attachmentData.weights[i + j + 1];
                        float weight  = attachmentData.weights[i + j + 2];
                        boneWeightList.Add(new KeyValuePair <int, float>(boneIdx, weight));
                    }
                    //sort boneWeightList,desc
                    boneWeightList.Sort(delegate(KeyValuePair <int, float> x, KeyValuePair <int, float> y) {
                        if (x.Value == y.Value)
                        {
                            return(0);
                        }
                        return(x.Value < y.Value? 1: -1);
                    });
                    Armature.BoneWeightClass bw = new Armature.BoneWeightClass();
                    for (int j = 0; j < boneWeightList.Count; ++j)
                    {
                        if (j == 0)
                        {
                            bw.boneIndex0 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight0    = boneWeightList[j].Value;
                        }
                        else if (j == 1)
                        {
                            bw.boneIndex1 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight1    = boneWeightList[j].Value;
                        }
                        else if (j == 2)
                        {
                            bw.boneIndex2 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight2    = boneWeightList[j].Value;
                        }
                        else if (j == 3)
                        {
                            bw.boneIndex3 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight3    = boneWeightList[j].Value;
                        }
                        else if (j == 4)
                        {
                            bw.boneIndex4 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight4    = boneWeightList[j].Value;
                            break;
                        }
                    }
                    boneWeights.Add(bw);
                    i += boneCount * 2;
                }
                Matrix4x4[] matrixArray = new Matrix4x4[bones.Length];
                for (int i = 0; i < matrixArray.Length; ++i)
                {
                    Transform bone = bones[i];
                    matrixArray[i]  = bone.worldToLocalMatrix * armatureEditor.armature.localToWorldMatrix;
                    matrixArray[i] *= Matrix4x4.TRS(slot.localPosition, slot.localRotation, slot.localScale);
                }
                sm.bones     = bones;
                sm.bindposes = matrixArray;
                sm.weights   = boneWeights.ToArray();
            }
            sm.color = attachmentData.color;
            sm.UpdateMesh();
            sm.UpdateVertexColor();
            sm.frame = frame;
        }
Beispiel #5
0
 //return current mesh bones
 static Transform[] SetMeshVertex <T>(T sm, SpineData.SkinAttachment attachmentData, SpineArmatureEditor armatureEditor)
 {
     if (attachmentData.weights != null && attachmentData.weights.Count > 0)
     {
         List <Vector3>         verticesList = new List <Vector3>();
         List <float>           weights      = new List <float>();
         List <Transform>       bones        = new List <Transform>();
         Dictionary <int, bool> bonesKV      = new Dictionary <int, bool>();
         for (int i = 0; i < attachmentData.weights.Count; ++i)
         {
             int boneCount = (int)attachmentData.weights[i];
             weights.Add(boneCount);
             Vector3 v = Vector3.zero;
             for (int j = 0; j < boneCount * 4; j += 4)
             {
                 int   boneIdx = (int)attachmentData.weights[i + j + 1];
                 float vx      = attachmentData.weights[i + j + 2];
                 float vy      = attachmentData.weights[i + j + 3];
                 float weight  = attachmentData.weights[i + j + 4];
                 weights.Add(boneIdx);
                 weights.Add(weight);
                 //convert vertex
                 Vector3   tempP = new Vector3(vx, vy, 0f);
                 Transform bone  = armatureEditor.bones[boneIdx];
                 tempP = bone.TransformPoint(tempP);
                 v.x  += tempP.x * weight;
                 v.y  += tempP.y * weight;
                 if (!bonesKV.ContainsKey(boneIdx))
                 {
                     bones.Add(armatureEditor.bones[boneIdx]);
                     bonesKV[boneIdx] = true;
                 }
             }
             verticesList.Add(v);
             i += boneCount * 4;
         }
         attachmentData.vertices = verticesList.ToArray();
         if (sm is SpriteMesh)
         {
             (sm as SpriteMesh).vertices = attachmentData.vertices;
         }
         else if (sm is UIMesh)
         {
             (sm as UIMesh).vertices = attachmentData.vertices;
         }
         attachmentData.weights = weights;
         return(bones.ToArray());
     }
     else
     {
         if (sm is SpriteMesh)
         {
             (sm as SpriteMesh).vertices = attachmentData.vertices;
         }
         else if (sm is UIMesh)
         {
             (sm as UIMesh).vertices = attachmentData.vertices;
         }
     }
     return(null);
 }
Beispiel #6
0
        private static SpriteRenderer ShowUnitySprite(SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpriteMetaData metaData, TextureFrame frame)
        {
            Sprite sprite = Sprite.Create((Texture2D)frame.texture, metaData.rect, metaData.pivot, 100f, 0, SpriteMeshType.Tight);

            return(ShowUnitySpriteSingle(sprite, attachmentData, slot, skinParent, frame));
        }
Beispiel #7
0
        static void ShowUIFrame(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor, SpineData.SlotData slotData)
        {
            GameObject go       = new GameObject();
            UIFrame    newFrame = go.AddComponent <UIFrame>();

            newFrame.raycastTarget = false;
            newFrame.GetComponent <Graphic>().raycastTarget = false;
            newFrame.CreateQuad();
            newFrame.frame = frame;
            newFrame.name  = attachmentData.textureName;
            newFrame.transform.SetParent(skinParent);

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;

            if (newFrame.frame.isRotated)
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.height;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.width;
                }
            }
            else
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.width;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.height;
                }
            }
            newFrame.transform.localScale = localSc;
            newFrame.color = attachmentData.color;
            newFrame.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation);

            if (armatureEditor.genImgCollider)
            {
                BoxCollider2D collider = newFrame.gameObject.AddComponent <BoxCollider2D>();
                if (newFrame.frame.isRotated)
                {
                    collider.size = new Vector2(newFrame.frame.rect.size.y, newFrame.frame.rect.size.x) * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
                else
                {
                    collider.size = newFrame.frame.rect.size * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
            }
            newFrame.UpdateAll();
        }
Beispiel #8
0
        private static void ShowSpriteFrame(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor)
        {
            GameObject  go       = new GameObject();
            SpriteFrame newFrame = go.AddComponent <SpriteFrame>();

            newFrame.CreateQuad();
            newFrame.textureFrames    = armatureEditor.m_TextureFrames;
            newFrame.frame            = frame;
            newFrame.name             = attachmentData.name;
            newFrame.pivot            = Vector2.one * 0.5f;
            newFrame.transform.parent = skinParent;

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            newFrame.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;

            if (newFrame.frame.isRotated)
            {
                if (attachmentData.width > 0 && frame.rect.height > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.height;
                }
                if (attachmentData.height > 0 && frame.rect.width > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.width;
                }
            }
            else
            {
                if (attachmentData.width > 0 && frame.rect.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.width;
                }
                if (attachmentData.height > 0 && frame.rect.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.height;
                }
            }

            newFrame.transform.localScale = localSc;
            newFrame.color = attachmentData.color;
            newFrame.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation);

            if (armatureEditor.genImgCollider)
            {
                BoxCollider2D collider = newFrame.gameObject.AddComponent <BoxCollider2D>();
                if (newFrame.frame.isRotated)
                {
                    collider.size = new Vector2(newFrame.frame.rect.size.y, newFrame.frame.rect.size.x) * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
                else
                {
                    collider.size = newFrame.frame.rect.size * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
            }
            newFrame.UpdateVertexColor();
        }
Beispiel #9
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 SpineData.AnimationDeformData ParseDeformAnimTimeline(SpineArmatureEditor armatureEditor, string skinName, string slotname, string attchmentname, Bones2D.JSONArray deformAnimObj)
        {
            SpineData.AnimationDeformData animDeformDatas = new SpineData.AnimationDeformData();
            animDeformDatas.slotName  = slotname;
            animDeformDatas.skinName  = skinName;
            animDeformDatas.timelines = new SpineData.DeformTimeline[deformAnimObj.Count];

            SpineData.SkinAttachment skinAtt = GetSkinAttachment(armatureEditor, skinName, slotname, attchmentname);
            bool haveWeight = (skinAtt == null || skinAtt.weights == null || skinAtt.weights.Count == 0) ? false : true;

            for (int i = 0; i < deformAnimObj.Count; ++i)
            {
                SpineData.DeformTimeline timeline = new SpineData.DeformTimeline();
                animDeformDatas.timelines[i] = timeline;
                timeline.attachment          = attchmentname;
                Bones2D.JSONClass animObj = deformAnimObj[i].AsObject;

                if (animObj.ContainKey("time"))
                {
                    timeline.time = animObj["time"].AsFloat;
                }
                if (animObj.ContainKey("curve"))
                {
                    if (animObj["curve"] == "stepped")
                    {
                        timeline.tweenEasing = "stepped";
                    }
                    else if (animObj["curve"] == "linear")
                    {
                        //default
                    }
                    else
                    {
                        timeline.curve = ConvertJsonArrayToFloatArr(animObj["curve"].AsArray);
                    }
                }

                if (animObj.ContainKey("offset"))
                {
                    timeline.offset = animObj["offset"].AsInt / 2;
                }
                if (animObj.ContainKey("vertices"))
                {
                    Bones2D.JSONArray verticesObj = animObj["vertices"].AsArray;

                    int index = 0;
                    int k     = 0;
                    timeline.vertices = new Vector3[verticesObj.Count / 2];
                    for (; k < verticesObj.Count && k + 1 < verticesObj.Count; k += 2)
                    {
                        timeline.vertices[index] = new Vector3(verticesObj[k].AsFloat * armatureEditor.unit, verticesObj[k + 1].AsFloat * armatureEditor.unit, 0f);
                        ++index;
                    }
                    armatureEditor.ffdKV [attchmentname] = true;

                    if (haveWeight)
                    {
                        CreateBonePose(armatureEditor);
                        BoneMatrix2D matrix      = new BoneMatrix2D();
                        int          vertexIndex = 0;
                        int          offset      = timeline.offset;
                        int          newOffset   = 0;
                        for (int j = 0; j < skinAtt.weights.Count; ++j)
                        {
                            int boneCount = (int)skinAtt.weights[j];
                            if (offset <= 0)
                            {
                                Vector3 v      = timeline.vertices [vertexIndex];
                                Vector3 result = new Vector3();
                                for (int w = 0; w < boneCount * 4; w += 4)
                                {
                                    int boneIndex = (int)skinAtt.weights [j + w + 1];
                                    SpineData.BoneData boneData = armatureEditor.armatureData.bones [boneIndex];
                                    float weight = skinAtt.weights [j + w + 4];

                                    BoneMatrix2D boneMatrix = armatureEditor.bonePoseKV [boneData.name];
                                    matrix.Identity();
                                    matrix.a = boneMatrix.a;
                                    matrix.b = boneMatrix.b;
                                    matrix.c = boneMatrix.c;
                                    matrix.d = boneMatrix.d;
                                    matrix.Invert();                                     //to local

                                    Vector2 p = matrix.TransformPoint(v.x, v.y);
                                    result.x += p.x * weight;
                                    result.y += p.y * weight;
                                }
                                timeline.vertices [vertexIndex] = result;
                                ++vertexIndex;
                                if (vertexIndex >= timeline.vertices.Length)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                ++newOffset;
                            }
                            offset -= boneCount;
                            j      += boneCount * 4;
                        }
                        timeline.offset = newOffset;
                    }
                }
            }
            return(animDeformDatas);
        }
 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;
     }
 }