static public bool UpdateAtlasSpriteInfo(string pathOutput, List <DataAnimAnalytics> listAnim, float scale)
        {
            //Debug.LogError(scale);
            if (listAnim.Count < 1)
            {
                return(false);
            }

            Dictionary <string, EAPInfoAttachment> dicPivotCache = new Dictionary <string, EAPInfoAttachment>();

            for (int i = 0; i < listAnim.Count; i++)
            {
                DataAnimAnalytics dataAnalytic = listAnim[i];
                //Debug.LogError(Pathfinding.Serialization.JsonFx.JsonWriter.Serialize(dataAnalytic.jsonFinal));
                foreach (KeyValuePair <string, EAPInfoAttachment> pair in dataAnalytic.jsonFinal.dicPivot)
                {
                    dicPivotCache[pair.Value.spriteName] = pair.Value;
                }
            }

            Dictionary <string, List <EAPInfoAttachment> > dicPivot = new Dictionary <string, List <EAPInfoAttachment> >();

            for (int i = 0; i < listAnim.Count; i++)
            {
                DataAnimAnalytics dataAnalytic = listAnim[i];
                foreach (KeyValuePair <string, EAPInfoAttachment> pair in dataAnalytic.jsonFinal.dicPivot)
                {
                    List <EAPInfoAttachment> list = null;
                    dicPivot.TryGetValue(pair.Value.spriteName, out list);
                    if (list == null)
                    {
                        list = new List <EAPInfoAttachment>();
                    }
                    bool haveExist = false;
                    for (int x = 0; x < list.Count; x++)
                    {
                        if (list[x].spriteName == pair.Key)
                        {
                            haveExist = true;
                            break;
                        }
                    }
                    if (!haveExist)
                    {
                        list.Add(pair.Value);
                    }
                    dicPivot[pair.Value.spriteName] = list;
                }
            }

            TextureImporter         ti       = AssetImporter.GetAtPath(pathOutput) as TextureImporter;
            TextureImporterSettings settings = new TextureImporterSettings();

            ti.ReadTextureSettings(settings);
            SpriteMetaData[] lstMetaSprite = ti.spritesheet;
            Dictionary <string, SpriteMetaData> dicSpriteMeta = new Dictionary <string, SpriteMetaData>();
            bool haveNew = false;

            for (int i = 0; i < lstMetaSprite.Length; i++)
            {
                SpriteMetaData spriteMetaData = lstMetaSprite[i];
                if (Mathf.Abs(scale - 1f) > Mathf.Epsilon)
                {
                    Rect rect = spriteMetaData.rect;
                    rect.x              = rect.x * scale;
                    rect.y              = rect.y * scale;
                    rect.width          = rect.width * scale;
                    rect.height         = rect.height * scale;
                    spriteMetaData.rect = rect;
                    haveNew             = true;
                }
                dicSpriteMeta[lstMetaSprite[i].name] = spriteMetaData;
            }
            foreach (KeyValuePair <string, List <EAPInfoAttachment> > pair in dicPivot)
            {
                List <EAPInfoAttachment> list = pair.Value;
                for (int i = 0; i < list.Count; i++)
                {
                    if (!dicSpriteMeta.ContainsKey(list[i].spriteName))                   // sprite new
                    {
                        if (dicSpriteMeta.ContainsKey(list[i].spriteName))
                        {
                            SpriteMetaData currentMeta = dicSpriteMeta[list[i].spriteName];
                            SpriteMetaData metaSprite  = new SpriteMetaData();
                            metaSprite.name      = list[i].spriteName;
                            metaSprite.rect      = currentMeta.rect;
                            metaSprite.alignment = currentMeta.alignment;

                            EAPInfoAttachment pivotCache = null;
                            dicPivotCache.TryGetValue(list[i].spriteName, out pivotCache);
                            if (pivotCache == null)
                            {
                                pivotCache = list[i];
                            }
                            //Debug.LogError(pivotCache.name+","+list[i].name+","+pivotCache.name+","+pivotCache.isOptimze);

                            if (!pivotCache.isOptimze)
                            {
                                metaSprite.pivot = new Vector2(list[i].x, list[i].y);                             //currentMeta.pivot;
                            }
                            else
                            {
                                float pivotX  = list[i].x * pivotCache.originalRect.width * scale;
                                float pivotY  = list[i].y * pivotCache.originalRect.height * scale;
                                float oWidth  = pivotCache.optimizeRect.width * scale;
                                float oHeight = pivotCache.optimizeRect.height * scale;
                                if (oWidth < 1)
                                {
                                    oWidth = 1;
                                }
                                if (oHeight < 1)
                                {
                                    oHeight = 1;
                                }
                                pivotX           = pivotX - pivotCache.startX * scale;
                                pivotY           = pivotY - pivotCache.startY * scale;
                                pivotX           = pivotX / oWidth;
                                pivotY           = pivotY / oHeight;
                                metaSprite.pivot = new Vector2(pivotX, pivotY);
                            }
                            dicSpriteMeta[metaSprite.name] = metaSprite;
                            haveNew = true;
                        }
                    }
                }
            }
            if (haveNew)
            {
                Texture2D mainTexture = AssetDatabase.LoadAssetAtPath(pathOutput, typeof(Texture2D)) as Texture2D;
                lstMetaSprite = new SpriteMetaData[dicSpriteMeta.Count];
                int count = 0;
                foreach (KeyValuePair <string, SpriteMetaData> pair in dicSpriteMeta)
                {
                    lstMetaSprite[count] = pair.Value;
                    count++;
                }
                ti.isReadable                = true;
                ti.mipmapEnabled             = false;
                ti.spritesheet               = lstMetaSprite;
                ti.textureType               = TextureImporterType.Sprite;
                ti.spriteImportMode          = SpriteImportMode.Multiple;
                ti.spritePixelsPerUnit       = 100;
                settings.textureFormat       = TextureImporterFormat.ARGB32;
                settings.npotScale           = TextureImporterNPOTScale.None;
                settings.alphaIsTransparency = true;
                ti.SetTextureSettings(settings);
                ti.maxTextureSize   = 4096;
                ti.mipmapEnabled    = false;
                ti.spriteImportMode = SpriteImportMode.Multiple;
                AssetDatabase.ImportAsset(pathOutput);
                EditorUtility.SetDirty(mainTexture);
                AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
                AssetDatabase.ImportAsset(pathOutput);
            }
            return(true);
        }
Beispiel #2
0
        public AEPJsonFinal(RawAEPJson raw)
        {
            #region Generate Bones Info
            this.bones = raw.bones;
            dicBones   = new Dictionary <string, BoneElement>();
            for (int i = 0; i < bones.Count; i++)
            {
                //bones[i].index=i;
                dicBones[bones[i].name] = bones[i];
            }
            #endregion

            #region attactment
            Dictionary <string, bool> dicAttact = new Dictionary <string, bool> ();
            if (raw.animations != null && raw.animations.animation != null)
            {
                foreach (KeyValuePair <string, object> pair1 in raw.animations.animation)
                {
                    if (pair1.Key == "bones")                 // tam thoi chi su ly cho bone
                    {
                        Dictionary <string, object> temp1 = (Dictionary <string, object>)pair1.Value;

                        foreach (KeyValuePair <string, object> pair2 in temp1)
                        {
                            //Debug.LogError(Pathfinding.Serialization.JsonFx.JsonWriter.Serialize(pair2.Value));
                            AEPBoneAnimationElement full = Pathfinding.Serialization.JsonFx.JsonReader.Deserialize(
                                Pathfinding.Serialization.JsonFx.JsonWriter.Serialize(pair2.Value), typeof(AEPBoneAnimationElement)) as AEPBoneAnimationElement;
                            full.name = pair2.Key;
                            dicAnimation[full.name] = full;
                        }
                        //Debug.LogError(Pathfinding.Serialization.JsonFx.JsonWriter.Serialize(dicAnimation));
                    }
                    else if (pair1.Key == "slots")                 // attachment object
                    {
                        Dictionary <string, object> temp1 = (Dictionary <string, object>)pair1.Value;

                        foreach (KeyValuePair <string, object> pair2 in temp1)
                        {
                            AEPSlotAnimationElement slotAnim = Pathfinding.Serialization.JsonFx.JsonReader.Deserialize(
                                Pathfinding.Serialization.JsonFx.JsonWriter.Serialize(pair2.Value), typeof(AEPSlotAnimationElement)) as AEPSlotAnimationElement;
                            slotAnim.name = pair2.Key;
                            if (slotAnim.attachment != null)
                            {
                                for (int yy = 0; yy < slotAnim.attachment.Count; yy++)
                                {
                                    string finalName = slotAnim.attachment[yy].name;
                                    if (!string.IsNullOrEmpty(finalName))
                                    {
                                        int index = finalName.LastIndexOf("/");
                                        finalName = finalName.Substring(index + 1, finalName.Length - index - 1);
                                    }
                                    slotAnim.attachment[yy].name = finalName;
                                }
                                dicAttact[slotAnim.name] = true;
                            }
                            dicSlotAttactment[slotAnim.name] = slotAnim;
                        }
                        //Debug.LogError(Pathfinding.Serialization.JsonFx.JsonWriter.Serialize(dicSlotAttactment));
                    }
                }
            }
            #endregion

            #region generate Slots Info
            this.slots = raw.slots;
            if (this.slots == null)
            {
                this.slots = new List <SlotElelement>();
            }
            dicSlots = new Dictionary <string, SlotElelement>();
            for (int i = 0; i < slots.Count; i++)
            {
                string slotAttachmentName = slots[i].attachment;
                if (slotAttachmentName == null)
                {
                    if (dicAttact.ContainsKey(slots[i].name))
                    {
                        slotAttachmentName = "";
                    }
                }
                if (slotAttachmentName != null)
                {
                    //Debug.LogError(slotAttachmentName);
                    int index = slotAttachmentName.LastIndexOf("/");
                    slotAttachmentName           = slotAttachmentName.Substring(index + 1, slotAttachmentName.Length - index - 1);
                    slots[i].attachment          = slotAttachmentName;
                    slots[i].index               = slots.Count - i;
                    this.dicSlots[slots[i].name] = slots[i];

                    BoneElement boneTemp = null;
                    dicBones.TryGetValue(slots[i].bone, out boneTemp);
                    if (boneTemp != null)
                    {
                        boneTemp.index = slots[i].index;
                    }
                }
            }
            #endregion

            #region skin
            Dictionary <string, object> skins = raw.skins;
            foreach (KeyValuePair <string, object> pair1 in skins)
            {
                Dictionary <string, object> temp1 = (Dictionary <string, object>)pair1.Value;
                int count = 0;
                foreach (KeyValuePair <string, object> pair2 in temp1)
                {
                    string        slotsName = pair2.Key;
                    SlotElelement slot      = null;
                    dicSlots.TryGetValue(slotsName, out slot);


                    Dictionary <string, object> temp2 = (Dictionary <string, object>)pair2.Value;
                    foreach (KeyValuePair <string, object> pair3 in temp2)
                    {
                        int depth = -count;
                        //Debug.LogError(pair3.Key);
                        string attachmentName = pair3.Key;
                        int    index          = attachmentName.LastIndexOf("/");
                        attachmentName = attachmentName.Substring(index + 1, attachmentName.Length - index - 1);
                        float x = 0;
                        float y = 0;

                        RawAEPJsonAttachment rawPivot = Pathfinding.Serialization.JsonFx.JsonReader.Deserialize(
                            Pathfinding.Serialization.JsonFx.JsonWriter.Serialize(pair3.Value), typeof(RawAEPJsonAttachment)) as RawAEPJsonAttachment;
                        x = (rawPivot.width / 2 - rawPivot.x) / rawPivot.width;
                        y = (rawPivot.height / 2 - rawPivot.y) / rawPivot.height;
                        if (slot != null)
                        {
                            depth = -slot.index;
                        }
                        if (string.IsNullOrEmpty(rawPivot.name))
                        {
                            rawPivot.name = attachmentName;
                        }
                        //Debug.LogError(Pathfinding.Serialization.JsonFx.JsonWriter.Serialize(rawPivot));
                        EAPInfoAttachment pivot = new EAPInfoAttachment(attachmentName, slotsName, depth, x, y, rawPivot.x, rawPivot.y);

                        count++;
                        //Debug.LogError(pivot.name);
                        pivot.spriteName           = pivot.spriteName.Trim();
                        dicPivot[pivot.spriteName] = pivot;
                        if (slot != null && !slot.listAcceptAttachment.Contains(pivot.spriteName))
                        {
                            slot.listAcceptAttachment.Add(pivot.spriteName);
                            slot.listAcceptObj.Add(pivot);
                        }
                    }
                }
            }
            #endregion
        }