Пример #1
0
        static void ShowSpriteFrame(SpriteFrame frame, Material mat, DragonBoneData.SkinSlotDisplayData displayData, Transform slot, DragonBoneData.SlotData slotData)
        {
            SpriteFrame newFrame = (SpriteFrame)GameObject.Instantiate(frame);

            newFrame.atlasMat = mat;
            newFrame.CreateQuad();
            newFrame.frameName        = displayData.textureName;
            newFrame.name             = displayData.textureName;
            newFrame.pivot            = displayData.pivot;
            newFrame.transform.parent = slot;

            Vector3 localPos = Vector3.zero;

            if (!float.IsNaN(displayData.transform.x))
            {
                localPos.x = displayData.transform.x;
            }
            if (!float.IsNaN(displayData.transform.y))
            {
                localPos.y = displayData.transform.y;
            }
            newFrame.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            if (!float.IsNaN(displayData.transform.scx))
            {
                localSc.x = displayData.transform.scx;
            }
            if (!float.IsNaN(displayData.transform.scy))
            {
                localSc.y = displayData.transform.scy;
            }
            newFrame.transform.localScale = localSc;

            Color c = Color.white;

            if (slotData.color != null)
            {
                c.a            = slotData.color.aM + slotData.color.a0;
                c.r            = slotData.color.rM + slotData.color.r0;
                c.g            = slotData.color.gM + slotData.color.g0;
                c.b            = slotData.color.bM + slotData.color.b0;
                newFrame.color = c;
            }

            if (!float.IsNaN(displayData.transform.rotate))
            {
                newFrame.transform.localRotation = Quaternion.Euler(0, 0, displayData.transform.rotate);
            }
        }
Пример #2
0
        static SpriteRenderer ShowUnitySpriteSingle(Sprite sprite, DragonBoneData.SkinSlotDisplayData displayData, Transform slot, DragonBoneData.SlotData slotData)
        {
            GameObject     go       = new GameObject(displayData.textureName);
            SpriteRenderer renderer = go.AddComponent <SpriteRenderer>();

            renderer.sprite     = sprite;
            go.transform.parent = slot;

            Vector3 localPos = Vector3.zero;

            if (!float.IsNaN(displayData.transform.x))
            {
                localPos.x = displayData.transform.x;
            }
            if (!float.IsNaN(displayData.transform.y))
            {
                localPos.y = displayData.transform.y;
            }
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            if (!float.IsNaN(displayData.transform.scx))
            {
                localSc.x = displayData.transform.scx;
            }
            if (!float.IsNaN(displayData.transform.scy))
            {
                localSc.y = displayData.transform.scy;
            }
            go.transform.localScale = localSc;

            Color c = Color.white;

            if (slotData.color != null)
            {
                c.a            = slotData.color.aM + slotData.color.a0;
                c.r            = slotData.color.rM + slotData.color.r0;
                c.g            = slotData.color.gM + slotData.color.g0;
                c.b            = slotData.color.bM + slotData.color.b0;
                renderer.color = c;
            }

            if (!float.IsNaN(displayData.transform.rotate))
            {
                go.transform.localRotation = Quaternion.Euler(0, 0, displayData.transform.rotate);
            }
            return(renderer);
        }
Пример #3
0
        static void ShowCustomCollider(DragonBoneData.SkinSlotDisplayData displayData, Transform slot, ArmatureEditor armatureEditor, DragonBoneData.SlotData slotData)
        {
            GameObject go = new GameObject(displayData.textureName);

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

            if (!float.IsNaN(displayData.transform.x))
            {
                localPos.x = displayData.transform.x;
            }
            if (!float.IsNaN(displayData.transform.y))
            {
                localPos.y = displayData.transform.y;
            }
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            if (!float.IsNaN(displayData.transform.scx))
            {
                localSc.x = displayData.transform.scx;
            }
            if (!float.IsNaN(displayData.transform.scy))
            {
                localSc.y = displayData.transform.scy;
            }
            go.transform.localScale = localSc;

            if (!float.IsNaN(displayData.transform.rotate))
            {
                go.transform.localRotation = Quaternion.Euler(0, 0, displayData.transform.rotate);
            }

            if (armatureEditor.genCustomCollider)
            {
                PolygonCollider2D collider = go.AddComponent <PolygonCollider2D>();
                Vector2[]         points   = new Vector2[displayData.vertices.Length];
                int len = points.Length;
                for (int i = 0; i < len; ++i)
                {
                    points[i] = (Vector2)displayData.vertices[i];
                }
                collider.points = points;
            }
        }
Пример #4
0
        static void ShowSpriteFrame(SpriteFrame frame, Material mat, DragonBoneData.SkinSlotDisplayData displayData, Transform slot, DragonBoneData.SlotData slotData)
        {
            SpriteFrame newFrame = (SpriteFrame)GameObject.Instantiate(frame);

            newFrame.atlasMat = mat;
            newFrame.CreateQuad();
            newFrame.frameName        = displayData.textureName;
            newFrame.name             = displayData.textureName;
            newFrame.pivot            = displayData.pivot;
            newFrame.transform.parent = slot;

            Vector3 localPos = Vector3.zero;

            if (!float.IsNaN(displayData.transform.x))
            {
                localPos.x = displayData.transform.x;
            }
            if (!float.IsNaN(displayData.transform.y))
            {
                localPos.y = displayData.transform.y;
            }
            newFrame.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            if (!float.IsNaN(displayData.transform.scx))
            {
                localSc.x = displayData.transform.scx;
            }
            if (!float.IsNaN(displayData.transform.scy))
            {
                localSc.y = displayData.transform.scy;
            }
            newFrame.transform.localScale = localSc;

            newFrame.color = slot.GetComponent <Slot>().color;

            if (!float.IsNaN(displayData.transform.rotate))
            {
                newFrame.transform.localRotation = Quaternion.Euler(0, 0, displayData.transform.rotate);
            }
        }
Пример #5
0
        static void ShowSpriteMesh(TextureFrame frame, Material mat, DragonBoneData.SkinSlotDisplayData displayData, Transform slot, ArmatureEditor armatureEditor, DragonBoneData.SlotData slotData)
        {
            armatureEditor.spriteMeshUsedMatKV[mat] = true;
            GameObject go = new GameObject(displayData.textureName);
            SpriteMesh sm = go.AddComponent <SpriteMesh>();

            sm.atlasMat  = mat;
            sm.vertices  = displayData.vertices;
            sm.frame     = frame;
            sm.uvs       = displayData.uvs;
            sm.triangles = displayData.triangles;
            sm.colors    = new Color[sm.vertices.Length];
            for (int i = 0; i < sm.colors.Length; ++i)
            {
                sm.colors[i] = Color.white;
            }
            if (displayData.weights != null && displayData.weights.Length > 0)
            {
                sm.CreateMesh(true);
                if (armatureEditor.ffdKV.ContainsKey(displayData.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;
                    }
                }
            }
            else
            {
                sm.CreateMesh(false);
                if (displayData.bonePose == null)
                {
                    //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;
                    }
                }
            }
            sm.transform.parent = slot;

            if (displayData.bonePose != null)
            {
                SkinnedMeshRenderer skinnedMesh = sm.GetComponent <SkinnedMeshRenderer>();
                if (skinnedMesh)
                {
                    skinnedMesh.quality = SkinQuality.Bone4;
                    if (displayData.weights != null && displayData.weights.Length > 0)
                    {
                        Transform[] bones = new Transform[displayData.bonePose.Length / 7];
                        for (int i = 0; i < displayData.bonePose.Length; i += 7)
                        {
                            int index     = i / 7;
                            int boneIndex = (int)displayData.bonePose[i];
                            bones[index] = armatureEditor.bones[boneIndex];
                        }

                        List <BoneWeight> boneWeights = new List <BoneWeight>();
                        for (int i = 0; i < displayData.weights.Length; ++i)
                        {
                            int boneCount = (int)displayData.weights[i];                            //骨骼数量

                            List <KeyValuePair <int, float> > boneWeightList = new List <KeyValuePair <int, float> >();
                            for (int j = 0; j < boneCount * 2; j += 2)
                            {
                                int   boneIdx = (int)displayData.weights[i + j + 1];
                                float weight  = displayData.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);
                            });
                            BoneWeight bw = new BoneWeight();
                            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;
                                }
                            }
                            boneWeights.Add(bw);
                            i += boneCount * 2;
                        }
                        Matrix4x4[] matrixArray = new Matrix4x4[bones.Length];
                        for (int i = 0; i < matrixArray.Length; ++i)
                        {
                            Transform  bone       = bones[i];
                            Vector3    bonePos    = bone.localPosition;
                            Quaternion boneRotate = bone.localRotation;

                            Matrix2D m2d = armatureEditor.bonePoseKV[displayData.textureName + bone.name];
                            bone.position = new Vector3(m2d.tx * 0.01f, -m2d.ty * 0.01f, bone.position.z);
                            bone.rotation = Quaternion.Euler(0f, 0f, -m2d.GetAngle());

                            matrixArray[i]  = bone.worldToLocalMatrix * armatureEditor.armature.localToWorldMatrix;
                            matrixArray[i] *= Matrix4x4.TRS(slot.localPosition, slot.localRotation, slot.localScale);

                            bone.localPosition = bonePos;
                            bone.localRotation = boneRotate;
                        }
                        skinnedMesh.bones = bones;
                        skinnedMesh.sharedMesh.boneWeights = boneWeights.ToArray();
                        skinnedMesh.sharedMesh.bindposes   = matrixArray;
                        skinnedMesh.rootBone = slot;
                        sm.bindposes         = matrixArray;
                        SpriteMesh.BoneWeightClass[] bwcs = new SpriteMesh.BoneWeightClass[boneWeights.Count];
                        for (int i = 0; i < boneWeights.Count; ++i)
                        {
                            SpriteMesh.BoneWeightClass bwc = new SpriteMesh.BoneWeightClass();
                            BoneWeight bw = boneWeights[i];
                            bwc.boneIndex0 = bw.boneIndex0;
                            bwc.boneIndex1 = bw.boneIndex1;
                            bwc.boneIndex2 = bw.boneIndex2;
                            bwc.boneIndex3 = bw.boneIndex3;
                            bwc.weight0    = bw.weight0;
                            bwc.weight1    = bw.weight1;
                            bwc.weight2    = bw.weight2;
                            bwc.weight3    = bw.weight3;
                            bwcs[i]        = bwc;
                        }
                        sm.weights = bwcs;
                    }
                }
            }
            DragonBoneData.TransformData tranform = displayData.transform;
            Vector3 localPos = Vector3.zero;

            if (!float.IsNaN(tranform.x))
            {
                localPos.x = tranform.x;
            }
            if (!float.IsNaN(tranform.y))
            {
                localPos.y = tranform.y;
            }
            sm.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            if (!float.IsNaN(tranform.scx))
            {
                localSc.x = tranform.scx;
            }
            if (!float.IsNaN(tranform.scy))
            {
                localSc.y = tranform.scy;
            }
            sm.transform.localScale = localSc;

            sm.color = slot.GetComponent <Slot>().color;
            sm.transform.localRotation = Quaternion.Euler(0, 0, tranform.rotate);
        }
Пример #6
0
        static SpriteRenderer ShowUnitySprite(Material mat, DragonBoneData.SkinSlotDisplayData displayData, Transform slot, SpriteMetaData metaData, DragonBoneData.SlotData slotData)
        {
            Sprite sprite = Sprite.Create((Texture2D)mat.mainTexture, metaData.rect, metaData.pivot, 100f, 0, SpriteMeshType.Tight);

            return(ShowUnitySpriteSingle(sprite, displayData, slot, slotData));
        }
Пример #7
0
        public static void ShowSkins(ArmatureEditor armatureEditor)
        {
            if (armatureEditor.armatureData.skinDatas != null && armatureEditor.armatureData.skinDatas.Length > 0)
            {
                DragonBoneArmature armature            = armatureEditor.armature.GetComponent <DragonBoneArmature>();
                Dictionary <Texture2D, Material> matKV = new Dictionary <Texture2D, Material>();
                List <Material> mats = new List <Material>();
                //创建贴图集的材质
                Material atlasMat = null;
                if (armatureEditor.altasTexture)
                {
                    Material mat  = null;
                    string   path = AssetDatabase.GetAssetPath(armatureEditor.altasTexture);
                    path = path.Substring(0, path.LastIndexOf('.')) + "_Mat.mat";
                    mat  = AssetDatabase.LoadAssetAtPath <Material>(path);
                    if (!mat)
                    {
                        mat = new Material(Shader.Find("DragonBone/DragonBone Simple"));
                        AssetDatabase.CreateAsset(mat, path);
                    }
                    mat.mainTexture = armatureEditor.altasTexture;
                    matKV[armatureEditor.altasTexture] = mat;
                    atlasMat = mat;
                    mats.Add(mat);
                }

                if (armatureEditor.otherTextures != null && armatureEditor.otherTextures.Length > 0)
                {
                    for (int r = 0; r < armatureEditor.otherTextures.Length; ++r)
                    {
                        ArmatureEditor.Atlas atlas = armatureEditor.otherTextures[r];
                        Material             mat   = null;

                        string path = AssetDatabase.GetAssetPath(atlas.texture);
                        path = path.Substring(0, path.LastIndexOf('.')) + "_Mat.mat";
                        mat  = AssetDatabase.LoadAssetAtPath <Material>(path);
                        if (!mat)
                        {
                            mat = new Material(Shader.Find("DragonBone/DragonBone Simple"));
                            AssetDatabase.CreateAsset(mat, path);
                        }
                        mat.mainTexture      = atlas.texture;
                        matKV[atlas.texture] = mat;
                        mats.Add(mat);
                    }
                }

                //create Frames
                Dictionary <Texture2D, SpriteFrame> frameKV = new Dictionary <Texture2D, SpriteFrame>();
                List <TextureFrame> tfs = new List <TextureFrame>();

                SpriteFrame frame = null;
                if (armatureEditor.altasTextAsset != null)
                {
                    GameObject  go = new GameObject();
                    SpriteFrame sf = go.AddComponent <SpriteFrame>();
                    sf.atlasMat  = atlasMat;
                    sf.atlasText = armatureEditor.altasTextAsset;
                    sf.ParseAtlasText();
                    sf.CreateQuad();
                    frameKV[armatureEditor.altasTexture] = sf;
                    frame = sf;
                    tfs.AddRange(frame.frames);
                }
                if (armatureEditor.otherTextures != null && armatureEditor.otherTextures.Length > 0)
                {
                    for (int r = 0; r < armatureEditor.otherTextures.Length; ++r)
                    {
                        ArmatureEditor.Atlas atlas = armatureEditor.otherTextures[r];
                        GameObject           go    = new GameObject();
                        SpriteFrame          sf    = go.AddComponent <SpriteFrame>();
                        sf.atlasMat  = matKV[atlas.texture];
                        sf.atlasText = atlas.atlasText;
                        sf.ParseAtlasText();
                        sf.CreateQuad();
                        frameKV[atlas.texture] = sf;
                        tfs.AddRange(sf.frames);
                    }
                }

                List <SpriteMetaData> metaDatas = new List <SpriteMetaData>();
                List <SpriteRenderer> sprites   = new List <SpriteRenderer>();

                int meshSpriteCount = 0;
                int len             = armatureEditor.armatureData.skinDatas.Length;
                for (int i = 0; i < len; ++i)
                {
                    DragonBoneData.SkinData skinData = armatureEditor.armatureData.skinDatas[i];
                    for (int j = 0; j < skinData.slots.Length; ++j)
                    {
                        DragonBoneData.SkinSlotData skinSlotData = skinData.slots[j];
                        Transform slot = armatureEditor.slotsKV[skinSlotData.slotName];
                        DragonBoneData.SlotData slotData = armatureEditor.slotsDataKV[skinSlotData.slotName];
                        if (slot && skinSlotData.displays != null && skinSlotData.displays.Length > 0)
                        {
                            for (int k = 0; k < skinSlotData.displays.Length; ++k)
                            {
                                DragonBoneData.SkinSlotDisplayData displayData = skinSlotData.displays[k];
                                if (displayData.type != "image" && displayData.type != "mesh" && displayData.type != "boundingBox")
                                {
                                    continue;
                                }

                                if (displayData.type.Equals("boundingBox"))
                                {
                                    if (displayData.subType == "polygon")
                                    {
                                        ShowCustomCollider(displayData, slot, armatureEditor, slotData);
                                    }
                                    continue;
                                }

                                ArmatureEditor.Atlas atlas = armatureEditor.GetAtlasByTextureName(displayData.texturePath);
                                if (!armatureEditor.isSingleSprite)
                                {
                                    atlasMat = matKV[atlas.texture];
                                    frame    = frameKV[atlas.texture];
                                }

                                if (displayData.type == "image")
                                {
                                    if (armatureEditor.useUnitySprite)
                                    {
                                        if (armatureEditor.isSingleSprite)
                                        {
                                            Sprite sprite     = armatureEditor.spriteKV[displayData.textureName];
                                            string spritePath = AssetDatabase.GetAssetPath(sprite);
                                            if (displayData.pivot.x != 0 || displayData.pivot.y != 0)
                                            {
                                                TextureImporter textureImporter = AssetImporter.GetAtPath(spritePath) as TextureImporter;
                                                textureImporter.textureType         = TextureImporterType.Sprite;
                                                textureImporter.spriteImportMode    = SpriteImportMode.Single;
                                                textureImporter.spritePixelsPerUnit = 100;
                                                textureImporter.spritePivot         = new Vector2((displayData.pivot.x + sprite.rect.width / 2) / sprite.rect.width, (displayData.pivot.y + sprite.rect.height / 2) / sprite.rect.height);
                                                AssetDatabase.ImportAsset(spritePath, ImportAssetOptions.ForceUpdate);
                                            }
                                            sprites.Add(ShowUnitySpriteSingle(sprite, displayData, slot, slotData));
                                        }
                                        else
                                        {
                                            SpriteMetaData metaData = new SpriteMetaData();
                                            metaData.name   = displayData.textureName;
                                            metaData.rect   = frame.GetFrameByName(displayData.texturePath).rect;
                                            metaData.rect.y = armatureEditor.altasTexture.height - metaData.rect.y - metaData.rect.height;
                                            if (displayData.pivot.x != 0 || displayData.pivot.y != 0)
                                            {
                                                metaData.alignment = (int)SpriteAlignment.Custom;
                                                metaData.pivot     = new Vector2((displayData.pivot.x + metaData.rect.width / 2) / metaData.rect.width, (displayData.pivot.y + metaData.rect.height / 2) / metaData.rect.height);
                                            }
                                            metaDatas.Add(metaData);
                                            sprites.Add(ShowUnitySprite(atlasMat, displayData, slot, metaData, slotData));
                                        }
                                    }
                                    else
                                    {
                                        ShowSpriteFrame(frame, atlasMat, displayData, slot, slotData);
                                    }
                                }
                                else if (displayData.type == "mesh")
                                {
                                    TextureFrame textureFrame = new TextureFrame();
                                    if (armatureEditor.isSingleSprite)
                                    {
                                        Sprite sprite = armatureEditor.spriteKV[displayData.textureName];
                                        textureFrame.name             = displayData.textureName;
                                        textureFrame.frameSize        = sprite.rect;
                                        textureFrame.rect             = sprite.rect;
                                        textureFrame.atlasTextureSize = new Vector2(sprite.rect.width, sprite.rect.height);

                                        string path           = AssetDatabase.GetAssetPath(sprite);
                                        string materialFolder = path.Substring(0, path.LastIndexOf("/"));
                                        materialFolder = materialFolder.Substring(0, materialFolder.LastIndexOf("/")) + "/Materials/";
                                        if (!Directory.Exists(materialFolder))
                                        {
                                            Directory.CreateDirectory(materialFolder);
                                        }
                                        string matPath = materialFolder + sprite.name + "_Mat.mat";
                                        atlasMat = AssetDatabase.LoadAssetAtPath <Material>(matPath);
                                        if (!atlasMat)
                                        {
                                            atlasMat = new Material(Shader.Find("DragonBone/DragonBone Simple"));
                                            AssetDatabase.CreateAsset(atlasMat, matPath);
                                        }
                                        atlasMat.mainTexture = AssetDatabase.LoadAssetAtPath <Texture>(path);

                                        mats.Add(atlasMat);
                                        textureFrame.material = atlasMat;
                                        textureFrame.texture  = atlasMat.mainTexture;
                                        tfs.Add(textureFrame);
                                    }
                                    else
                                    {
                                        foreach (TextureFrame st in frame.frames)
                                        {
                                            if (st.name.Equals(displayData.textureName))
                                            {
                                                textureFrame = st;
                                                break;
                                            }
                                        }
                                    }
                                    if (textureFrame.rect.width > 0 && textureFrame.rect.height > 0 && atlasMat && atlasMat.mainTexture)
                                    {
                                        ShowSpriteMesh(textureFrame, atlasMat, displayData, slot, armatureEditor, slotData);
                                        ++meshSpriteCount;
                                    }
                                }
                            }
                            slot.GetComponent <Slot>().displayIndex = slotData.displayIndex;
                        }
                    }
                }
                armature.materials     = mats.ToArray();
                armature.textureFrames = tfs.ToArray();

                foreach (SpriteFrame sf in frameKV.Values)
                {
                    GameObject.DestroyImmediate(sf.gameObject);
                }

                if (armatureEditor.useUnitySprite)
                {
                    if (!armatureEditor.isSingleSprite)
                    {
                        if (metaDatas.Count > 0)
                        {
                            string          textureAtlasPath = AssetDatabase.GetAssetPath(armatureEditor.altasTexture);
                            TextureImporter textureImporter  = AssetImporter.GetAtPath(textureAtlasPath) as TextureImporter;
                            textureImporter.maxTextureSize      = 2048;
                            textureImporter.spritesheet         = metaDatas.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;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (atlasMat != null)
                    {
                        if (meshSpriteCount == 0)
                        {
                            //can delete safely
                            foreach (Material mat in matKV.Values)
                            {
                                if (!armatureEditor.spriteMeshUsedMatKV.ContainsKey(mat))
                                {
                                    AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(mat));
                                }
                            }
                        }
                        else
                        {
                            foreach (SpriteRenderer sprite in sprites)
                            {
                                sprite.material = atlasMat;
                            }
                        }
                    }
                }
            }
        }
Пример #8
0
        public static void ParseArmtureData(ArmatureEditor armatureEditor, SimpleJSON.JSONClass armtureObj)
        {
            //parse bone data
            if (armtureObj.ContainKey("bone"))
            {
                SimpleJSON.JSONArray      bones     = armtureObj["bone"].AsArray;
                DragonBoneData.BoneData[] boneDatas = new DragonBoneData.BoneData[bones.Count];
                for (int i = 0; i < bones.Count; ++i)
                {
                    SimpleJSON.JSONClass    boneObj  = bones[i].AsObject;
                    DragonBoneData.BoneData boneData = new DragonBoneData.BoneData();
                    if (boneObj.ContainKey("length"))
                    {
                        boneData.length = boneObj["length"].AsFloat;
                    }
                    if (boneObj.ContainKey("name"))
                    {
                        boneData.name = boneObj["name"].ToString().Trim();
                    }
                    if (boneObj.ContainKey("parent"))
                    {
                        boneData.parent = boneObj["parent"].ToString();
                    }
                    if (boneObj.ContainKey("inheritRotation"))
                    {
                        boneData.inheritRotation = boneObj["inheritRotation"].AsInt == 1?true:false;
                    }
                    if (boneObj.ContainKey("inheritScale"))
                    {
                        boneData.inheritScale = boneObj["inheritScale"].AsInt == 1?true:false;
                    }
                    if (boneObj.ContainKey("transform"))
                    {
                        SimpleJSON.JSONClass         transformObj = boneObj["transform"].AsObject;
                        DragonBoneData.TransformData transData    = new DragonBoneData.TransformData();
                        if (transformObj.ContainKey("x"))
                        {
                            transData.x = transformObj["x"].AsFloat * 0.01f;
                        }
                        if (transformObj.ContainKey("y"))
                        {
                            transData.y = -transformObj["y"].AsFloat * 0.01f;
                        }
                        if (transformObj.ContainKey("skX"))
                        {
                            transData.rotate = -transformObj["skX"].AsFloat;
                        }
                        if (transformObj.ContainKey("scX"))
                        {
                            transData.scx = transformObj["scX"].AsFloat;
                        }
                        if (transformObj.ContainKey("scY"))
                        {
                            transData.scy = transformObj["scY"].AsFloat;
                        }
                        boneData.transform = transData;
                    }
                    boneDatas[i] = boneData;
                    armatureEditor.bonesDataKV[boneData.name] = boneData;
                }
                armatureEditor.armatureData.boneDatas = boneDatas;
            }

            //parse slot data
            if (armtureObj.ContainKey("slot"))
            {
                SimpleJSON.JSONArray      slots     = armtureObj["slot"].AsArray;
                DragonBoneData.SlotData[] slotDatas = new DragonBoneData.SlotData[slots.Count];
                bool isMC = armatureEditor.armatureData.type.Equals("MovieClip");
                for (int i = 0; i < slots.Count; ++i)
                {
                    SimpleJSON.JSONClass    slotObj  = slots[i].AsObject;
                    DragonBoneData.SlotData slotData = new DragonBoneData.SlotData();
                    if (slotObj.ContainKey("name"))
                    {
                        slotData.name = slotObj["name"].ToString();
                    }
                    if (slotObj.ContainKey("parent"))
                    {
                        slotData.parent = slotObj["parent"].ToString();
                    }
                    if (slotObj.ContainKey("z"))
                    {
                        slotData.z = -slotObj["z"].AsFloat * armatureEditor.zoffset;
                    }
                    if (!isMC)
                    {
                        if (slotObj.ContainKey("displayIndex"))
                        {
                            slotData.displayIndex = slotObj["displayIndex"].AsInt;
                        }
                    }
                    if (slotObj.ContainKey("scale"))
                    {
                        slotData.scale = slotObj["scale"].AsFloat;
                    }
                    if (slotObj.ContainKey("blendMode"))
                    {
                        slotData.blendMode = slotObj["blendMode"].ToString();
                    }
                    if (slotObj.ContainKey("color"))
                    {
                        SimpleJSON.JSONClass     colorObj  = slotObj["color"].AsObject;
                        DragonBoneData.ColorData colorData = new DragonBoneData.ColorData();
                        if (colorObj.ContainKey("aM"))
                        {
                            colorData.aM = colorObj["aM"].AsFloat * 0.01f;
                        }
                        if (colorObj.ContainKey("a0"))
                        {
                            colorData.aM += colorObj["a0"].AsFloat / 255f;
                        }
                        if (colorObj.ContainKey("rM"))
                        {
                            colorData.rM = colorObj["rM"].AsFloat * 0.01f;
                        }
                        if (colorObj.ContainKey("r0"))
                        {
                            colorData.rM += colorObj["r0"].AsFloat / 255f;
                        }
                        if (colorObj.ContainKey("gM"))
                        {
                            colorData.gM = colorObj["gM"].AsFloat * 0.01f;
                        }
                        if (colorObj.ContainKey("g0"))
                        {
                            colorData.gM += colorObj["g0"].AsFloat / 255f;
                        }
                        if (colorObj.ContainKey("bM"))
                        {
                            colorData.bM = colorObj["bM"].AsFloat * 0.01f;
                        }
                        if (colorObj.ContainKey("b0"))
                        {
                            colorData.bM += colorObj["b0"].AsFloat / 255f;
                        }
                        slotData.color = colorData;
                    }

                    slotDatas[i] = slotData;
                    armatureEditor.slotsDataKV[slotData.name] = slotData;
                }
                armatureEditor.armatureData.slotDatas = slotDatas;
            }

            //parse IK data
            if (armtureObj.ContainKey("ik"))
            {
                SimpleJSON.JSONArray    iks     = armtureObj["ik"].AsArray;
                DragonBoneData.IKData[] ikDatas = new DragonBoneData.IKData[iks.Count];
                for (int i = 0; i < iks.Count; ++i)
                {
                    SimpleJSON.JSONClass  ikObj  = iks[i].AsObject;
                    DragonBoneData.IKData ikData = new DragonBoneData.IKData();
                    if (ikObj.ContainKey("name"))
                    {
                        ikData.name = ikObj["name"].ToString();
                    }
                    if (ikObj.ContainKey("bone"))
                    {
                        ikData.bone = ikObj["bone"].ToString();
                    }
                    if (ikObj.ContainKey("target"))
                    {
                        ikData.target = ikObj["target"].ToString();
                    }
                    if (ikObj.ContainKey("bendPositive"))
                    {
                        ikData.bendPositive = ikObj["bendPositive"].AsBool;
                    }
                    if (ikObj.ContainKey("chain"))
                    {
                        ikData.chain = ikObj["chain"].AsInt;
                    }
                    if (ikObj.ContainKey("weight"))
                    {
                        ikData.weight = ikObj["weight"].AsFloat;
                    }
                    ikDatas[i] = ikData;
                }
                armatureEditor.armatureData.ikDatas = ikDatas;
            }

            //parse animation file
            if (armtureObj.ContainKey("animation"))
            {
                SimpleJSON.JSONArray           anims     = armtureObj["animation"].AsArray;
                DragonBoneData.AnimationData[] animDatas = new DragonBoneData.AnimationData[anims.Count];
                for (int i = 0; i < anims.Count; ++i)
                {
                    SimpleJSON.JSONClass         animObj  = anims[i].AsObject;
                    DragonBoneData.AnimationData animData = new DragonBoneData.AnimationData();
                    if (animObj.ContainKey("name"))
                    {
                        animData.name = animObj["name"].ToString().Trim();
                    }
                    if (animObj.ContainKey("playTimes"))
                    {
                        animData.playTimes = animObj["playTimes"].AsInt;
                    }
                    if (animObj.ContainKey("duration"))
                    {
                        animData.duration = animObj["duration"].AsInt;
                    }
                    if (animData.duration == 0)
                    {
                        animData.duration = 1;
                    }
                    if (animObj.ContainKey("frame"))
                    {
                        ParseAnimFrames(animObj["frame"].AsArray, animData);
                    }
                    if (animObj.ContainKey("bone"))
                    {
                        SimpleJSON.JSONArray bones = animObj["bone"].AsArray;
                        animData.boneDatas = new DragonBoneData.AnimSubData[bones.Count];
                        ParsetAnimBoneSlot(armatureEditor, bones, animData.boneDatas);
                    }
                    if (animObj.ContainKey("slot"))
                    {
                        SimpleJSON.JSONArray slots = animObj["slot"].AsArray;
                        animData.slotDatas = new DragonBoneData.AnimSubData[slots.Count];
                        ParsetAnimBoneSlot(armatureEditor, slots, animData.slotDatas);
                    }
                    //ffd
                    if (animObj.ContainKey("ffd"))
                    {
                        SimpleJSON.JSONArray ffds = animObj["ffd"].AsArray;
                        animData.ffdDatas = new DragonBoneData.AnimSubData[ffds.Count];
                        ParsetAnimBoneSlot(armatureEditor, ffds, animData.ffdDatas);
                    }
                    //zOrder
                    if (animObj.ContainKey("zOrder"))
                    {
                        SimpleJSON.JSONClass zOrders = animObj["zOrder"].AsObject;
                        ParseAnimSortOrder(armatureEditor, zOrders, animData);
                    }
                    animDatas[i] = animData;
                }
                armatureEditor.armatureData.animDatas = animDatas;
            }

            //parse skin data
            if (armtureObj.ContainKey("skin"))
            {
                SimpleJSON.JSONArray      skins     = armtureObj["skin"].AsArray;
                DragonBoneData.SkinData[] skinDatas = new DragonBoneData.SkinData[skins.Count];
                for (int i = 0; i < skins.Count; ++i)
                {
                    DragonBoneData.SkinData skinData = new DragonBoneData.SkinData();
                    skinDatas[i] = skinData;
                    SimpleJSON.JSONClass skinObj = skins[i].AsObject;
                    string skinName = skinObj["name"].ToString();
                    skinData.skinName = skinName;
                    if (skinObj.ContainKey("slot"))
                    {
                        SimpleJSON.JSONArray slots = skinObj["slot"].AsArray;
                        skinData.slots = new DragonBoneData.SkinSlotData[slots.Count];
                        for (int j = 0; j < slots.Count; ++j)
                        {
                            DragonBoneData.SkinSlotData skinSlotData = new DragonBoneData.SkinSlotData();
                            SimpleJSON.JSONClass        slot         = slots[j].AsObject;
                            skinData.slots[j] = skinSlotData;
                            if (slot.ContainKey("name"))
                            {
                                skinSlotData.slotName = slot["name"].ToString();
                            }
                            if (slot.ContainKey("display"))
                            {
                                SimpleJSON.JSONArray display = slot["display"].AsArray;
                                skinSlotData.displays = new DragonBoneData.SkinSlotDisplayData[display.Count];
                                for (int k = 0; k < display.Count; ++k)
                                {
                                    DragonBoneData.SkinSlotDisplayData displayData = new DragonBoneData.SkinSlotDisplayData();
                                    skinSlotData.displays[k] = displayData;
                                    SimpleJSON.JSONClass displayObj = display[k].AsObject;
                                    if (displayObj.ContainKey("name"))
                                    {
                                        displayData.textureName = displayObj["name"].ToString().Replace('/', '_');
                                    }
                                    if (displayObj.ContainKey("path"))
                                    {
                                        displayData.texturePath = displayObj["path"].ToString();
                                    }
                                    else
                                    {
                                        displayData.texturePath = displayData.textureName;
                                    }
                                    if (displayObj.ContainKey("type"))
                                    {
                                        displayData.type = displayObj["type"].ToString();
                                    }
                                    if (displayObj.ContainKey("subType"))
                                    {
                                        displayData.subType = displayObj["subType"].ToString();
                                    }
                                    if (displayObj.ContainKey("pivot"))
                                    {
                                        displayData.pivot = new Vector2(displayObj["pivot"].AsObject["x"].AsFloat, displayObj["pivot"].AsObject["y"].AsFloat);
                                    }
                                    if (displayObj.ContainKey("transform"))
                                    {
                                        SimpleJSON.JSONClass         transformObj = displayObj["transform"].AsObject;
                                        DragonBoneData.TransformData transData    = new DragonBoneData.TransformData();
                                        if (transformObj.ContainKey("x"))
                                        {
                                            transData.x = transformObj["x"].AsFloat * 0.01f;
                                        }
                                        if (transformObj.ContainKey("y"))
                                        {
                                            transData.y = -transformObj["y"].AsFloat * 0.01f;
                                        }
                                        if (transformObj.ContainKey("skX"))
                                        {
                                            transData.rotate = -transformObj["skX"].AsFloat;
                                        }
                                        if (transformObj.ContainKey("scX"))
                                        {
                                            transData.scx = transformObj["scX"].AsFloat;
                                        }
                                        if (transformObj.ContainKey("scY"))
                                        {
                                            transData.scy = transformObj["scY"].AsFloat;
                                        }
                                        displayData.transform = transData;
                                    }
                                    //uv
                                    if (displayObj.ContainKey("uvs"))
                                    {
                                        SimpleJSON.JSONArray uvsObj = displayObj["uvs"].AsArray;
                                        int index = 0;
                                        displayData.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);
                                            displayData.uvs[index] = uv;
                                            ++index;
                                        }
                                    }


                                    //weight
                                    if (displayObj.ContainKey("weights"))
                                    {
                                        SimpleJSON.JSONArray weightsObj = displayObj["weights"].AsArray;
                                        displayData.weights = new float[weightsObj.Count];
                                        for (int z = 0; z < weightsObj.Count; ++z)
                                        {
                                            displayData.weights[z] = weightsObj[z].AsFloat;
                                        }
                                    }
                                    //bonepose
                                    if (displayObj.ContainKey("bonePose"))
                                    {
                                        SimpleJSON.JSONArray bonePoseObj = displayObj["bonePose"].AsArray;
                                        displayData.bonePose = new float[bonePoseObj.Count];
                                        for (int z = 0; z < bonePoseObj.Count; z += 7)
                                        {
                                            displayData.bonePose[z]     = bonePoseObj[z].AsFloat;
                                            displayData.bonePose[z + 1] = bonePoseObj[z + 1].AsFloat;                                        //a
                                            displayData.bonePose[z + 2] = bonePoseObj[z + 2].AsFloat;                                        //b
                                            displayData.bonePose[z + 3] = bonePoseObj[z + 3].AsFloat;                                        //c
                                            displayData.bonePose[z + 4] = bonePoseObj[z + 4].AsFloat;                                        //d
                                            displayData.bonePose[z + 5] = bonePoseObj[z + 5].AsFloat;                                        //tx
                                            displayData.bonePose[z + 6] = bonePoseObj[z + 6].AsFloat;                                        //ty

                                            Matrix2D m = new Matrix2D(displayData.bonePose[z + 1], displayData.bonePose[z + 2],
                                                                      displayData.bonePose[z + 3], displayData.bonePose[z + 4], displayData.bonePose[z + 5], displayData.bonePose[z + 6]);
                                            armatureEditor.bonePoseKV[displayData.textureName + armatureEditor.armatureData.boneDatas[(int)displayData.bonePose[z]].name] = m;
                                        }
                                    }

                                    Matrix2D slotPoseMat = null;
                                    //slotpose
                                    if (displayObj.ContainKey("slotPose"))
                                    {
                                        SimpleJSON.JSONArray slotPoseObj = displayObj["slotPose"].AsArray;
                                        slotPoseMat = new Matrix2D(slotPoseObj[0].AsFloat, slotPoseObj[1].AsFloat, slotPoseObj[2].AsFloat,
                                                                   slotPoseObj[3].AsFloat, slotPoseObj[4].AsFloat, slotPoseObj[5].AsFloat);
                                    }

                                    //vertex
                                    if (displayObj.ContainKey("vertices"))
                                    {
                                        SimpleJSON.JSONArray verticesObj = displayObj["vertices"].AsArray;
                                        displayData.vertices = new Vector3[verticesObj.Count / 2];

                                        for (int z = 0; z < verticesObj.Count; z += 2)
                                        {
                                            int     vertexIndex = z / 2;
                                            Vector3 vertex      = new Vector3(verticesObj[z].AsFloat, verticesObj[z + 1].AsFloat, 0f);
                                            if (slotPoseMat != null)
                                            {
                                                //slotPose转换
                                                vertex = (Vector3)slotPoseMat.TransformPoint(vertex.x, vertex.y);
                                            }
                                            vertex.x *= 0.01f;
                                            vertex.y *= -0.01f;
                                            displayData.vertices[vertexIndex] = vertex;
                                        }
                                    }
                                    //triangles
                                    if (displayObj.ContainKey("triangles"))
                                    {
                                        SimpleJSON.JSONArray trianglesObj = displayObj["triangles"].AsArray;
                                        displayData.triangles = new int[trianglesObj.Count];
                                        for (int z = 0; z < trianglesObj.Count; z++)
                                        {
                                            displayData.triangles[z] = trianglesObj[z].AsInt;
                                        }
                                        //dragonBone和unity的z相反
                                        for (int z = 0; z < displayData.triangles.Length; z += 3)
                                        {
                                            int f1 = displayData.triangles[z];
                                            int f3 = displayData.triangles[z + 2];
                                            displayData.triangles[z]     = f3;
                                            displayData.triangles[z + 2] = f1;
                                        }
                                    }
                                    //edges

                                    //userdeges
                                }
                            }
                        }
                    }
                }
                armatureEditor.armatureData.skinDatas = skinDatas;
            }
        }