Example #1
0
        public static void ParseTextureAtlas(ArmatureEditor armatureEditor, Texture2D texture, TextAsset atlasText)
        {
            SimpleJSON.JSONClass obj = SimpleJSON.JSON.Parse(atlasText.text).AsObject;
            SimpleJSON.JSONArray arr = obj["SubTexture"].AsArray;
            for (int i = 0; i < arr.Count; ++i)
            {
                SimpleJSON.JSONClass frameObj = arr[i].AsObject;
                string textureName            = frameObj["name"].ToString();
                textureName = textureName.Replace('/', '_');

                ArmatureEditor.Atlas atlas = new ArmatureEditor.Atlas();
                atlas.texture   = texture;
                atlas.atlasText = atlasText;
                armatureEditor.atlasKV[textureName] = atlas;
            }
        }
Example #2
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;
                            }
                        }
                    }
                }
            }
        }