Exemplo n.º 1
0
        private static ImportedAnimationSheet GetAnimationInfo(JsonObject root)
        {
            if (root == null)
            {
                Debug.LogWarning("Error importing JSON animation info: JSONObject is NULL");
                return(null);
            }

            ImportedAnimationSheet animationSheet = new ImportedAnimationSheet();

            animationSheet.UsePivot = false;

            // Import Frame Size
            if (GetFrameSizeFromJson(animationSheet, root) == false)
            {
                return(null);
            }

            // Import all informations from JSON
            if (!root.ContainsKey("meta"))
            {
                Debug.LogWarning("Error importing JSON animation info: no 'meta' object");
                return(null);
            }
            var meta = root["meta"].Obj;

            // Import meta data
            GetMetaInfosFromJson(animationSheet, meta);

            // Import Animation meta data
            if (GetAnimationsFromJson(animationSheet, meta) == false)
            {
                return(null);
            }

            // Import Frames
            if (GetFramesFromJson(animationSheet, root) == false)
            {
                return(null);
            }

            animationSheet.ApplyGlobalFramesToAnimationFrames();

            return(animationSheet);
        }
Exemplo n.º 2
0
        private static bool GetFrameSizeFromJson(ImportedAnimationSheet animationSheet, JsonObject root)
        {
            var list = root["frames"].Array;

            if (list == null)
            {
                Debug.LogWarning("No 'frames' array found in JSON created by Aseprite.");
                IssueVersionWarning();
                return(false);
            }

            foreach (var item in list)
            {
                var sourceSizeValues = item.Obj["sourceSize"].Obj;
                animationSheet.SourceSize = new Vector2Int((int)sourceSizeValues["w"].Number, (int)sourceSizeValues["h"].Number);
                return(true);
            }

            return(false);
        }
Exemplo n.º 3
0
        private static bool GetFramesFromJSON(ImportedAnimationSheet animationSheet, JSONObject root)
        {
            var list = root["frames"].Array;

            if (list == null)
            {
                Debug.LogWarning("No 'frames' array found in JSON created by Aseprite.");
                IssueVersionWarning();
                return(false);
            }

            foreach (var item in list)
            {
                ImportedAnimationFrame frame = new ImportedAnimationFrame();

                var frameRect = item.Obj["frame"].Obj;
                frame.rect.width  = (int)frameRect["w"].Number;
                frame.rect.height = (int)frameRect["h"].Number;
                frame.rect.x      = (int)frameRect["x"].Number;
                frame.rect.y      = animationSheet.height - (int)frameRect["y"].Number - frame.rect.height;            // unity has a different coord system

                frame.trimmed = item.Obj["trimmed"].Boolean;

                var sourceSize = item.Obj["sourceSize"].Obj;
                frame.sourceSize.x = (int)sourceSize["w"].Number;
                frame.sourceSize.y = (int)sourceSize["h"].Number;

                var spriteSourceRect = item.Obj["spriteSourceSize"].Obj;
                frame.spriteSourceRect.width  = (int)spriteSourceRect["w"].Number;
                frame.spriteSourceRect.height = (int)spriteSourceRect["h"].Number;
                frame.spriteSourceRect.x      = (int)spriteSourceRect["x"].Number;
                frame.spriteSourceRect.y      = frame.sourceSize.y - (int)spriteSourceRect["y"].Number - frame.spriteSourceRect.height; // unity has a different coord system

                frame.duration = (int)item.Obj["duration"].Number;

                animationSheet.frames.Add(frame);
            }

            return(true);
        }
        private static bool GetAnimationsFromJSON(ImportedAnimationSheet animationSheet, JSONObject meta)
        {
            if (!meta.ContainsKey("frameTags"))
            {
                Debug.LogWarning("No 'frameTags' found in JSON created by Aseprite.");
                IssueVersionWarning();
                return(false);
            }

            var frameTags = meta["frameTags"].Array;

            foreach (var item in frameTags)
            {
                JSONObject        frameTag = item.Obj;
                ImportedAnimation anim     = new ImportedAnimation();
                anim.name             = frameTag["name"].Str;
                anim.firstSpriteIndex = (int)(frameTag["from"].Number);
                anim.lastSpriteIndex  = (int)(frameTag["to"].Number);

                switch (frameTag["direction"].Str)
                {
                default:
                    anim.direction = PlaybackDirection.Forward;
                    break;

                case "reverse":
                    anim.direction = PlaybackDirection.Reverse;
                    break;

                case "pingpong":
                    anim.direction = PlaybackDirection.PingPong;
                    break;
                }

                animationSheet.animations.Add(anim);
            }

            return(true);
        }
Exemplo n.º 5
0
        private static bool GetAnimationsFromJSON(ImportedAnimationSheet animationSheet, JSONObject meta)
        {
            if (!meta.ContainsKey("frameTags"))
            {
                Debug.LogWarning("No 'frameTags' found in JSON created by Aseprite.");
                IssueVersionWarning();
                return(false);
            }

            var frameTags = meta["frameTags"].Array;

            foreach (var item in frameTags)
            {
                JSONObject        frameTag = item.Obj;
                ImportedAnimation anim     = new ImportedAnimation();
                anim.name             = frameTag["name"].Str;
                anim.firstSpriteIndex = (int)(frameTag["from"].Number);
                anim.lastSpriteIndex  = (int)(frameTag["to"].Number);

                animationSheet.animations.Add(anim);
            }

            return(true);
        }
Exemplo n.º 6
0
        // ================================================================================
        //  Private methods
        // --------------------------------------------------------------------------------

        // parses a JSON file and creates the raw data for ImportedAnimationSheet from it
        private static ImportedAnimationSheet CreateAnimationSheetFromMetaData(AnimationImportJob job, AnimationImporterSharedConfig config)
        {
            string    textAssetFilename = job.DirectoryPathForSprites + "/" + job.Name + ".json";
            TextAsset textAsset         = AssetDatabase.LoadAssetAtPath <TextAsset>(textAssetFilename);

            if (textAsset != null)
            {
                JsonObject             jsonObject     = JsonObject.Parse(textAsset.ToString());
                ImportedAnimationSheet animationSheet = GetAnimationInfo(jsonObject);

                if (animationSheet == null)
                {
                    return(null);
                }

                if (!animationSheet.HasAnimations)
                {
                    Debug.LogWarning("No Animations found in Aseprite file. Use Aseprite Tags to assign names to Animations.");
                }

                animationSheet.PreviousImportSettings = job.PreviousImportSettings;

                animationSheet.SetNonLoopingAnimations(config.AnimationNamesThatDoNotLoop);

                // Delete JSON file afterwards
                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(textAsset));

                return(animationSheet);
            }
            else
            {
                Debug.LogWarning("Problem with JSON file: " + textAssetFilename);
            }

            return(null);
        }
Exemplo n.º 7
0
        private static ImportedAnimationSheet GetAnimationInfo(PyxelEditData data)
        {
            if (data == null)
            {
                return(null);
            }

            int tileWidth  = data.tileset.tileWidth;
            int tileHeight = data.tileset.tileHeight;

            int maxTileIndex = 0;

            ImportedAnimationSheet animationSheet = new ImportedAnimationSheet();

            animationSheet.width  = data.canvas.width;
            animationSheet.height = data.canvas.height;

            // animations
            animationSheet.animations = new List <ImportedAnimation>();
            for (int i = 0; i < data.animations.Count; i++)
            {
                var animationData = data.animations[i];

                ImportedAnimation importAnimation = new ImportedAnimation();

                importAnimation.name = animationData.name;

                importAnimation.firstSpriteIndex = animationData.baseTile;
                importAnimation.lastSpriteIndex  = animationData.baseTile + animationData.length - 1;

                maxTileIndex = Mathf.Max(maxTileIndex, importAnimation.lastSpriteIndex);

                ImportedAnimationFrame[] frames = new ImportedAnimationFrame[animationData.length];
                for (int frameIndex = 0; frameIndex < animationData.length; frameIndex++)
                {
                    ImportedAnimationFrame frame = new ImportedAnimationFrame();

                    frame.duration = animationData.frameDuration;
                    if (animationData.frameDurationMultipliers[i] != 100)
                    {
                        frame.duration *= (int)(animationData.frameDurationMultipliers[i] / 100f);
                    }

                    int tileIndex = animationData.baseTile + frameIndex;

                    int columnCount = data.canvas.width / tileWidth;

                    int column = tileIndex % columnCount;
                    int row    = tileIndex / columnCount;

                    frame.rect.y      = row * tileHeight;
                    frame.rect.x      = column * tileWidth;
                    frame.rect.width  = tileWidth;
                    frame.rect.height = tileHeight;

                    frames[frameIndex] = frame;
                }

                importAnimation.SetFrames(frames);

                animationSheet.animations.Add(importAnimation);
            }

            // gather all frames used by animations for the sprite sheet
            animationSheet.frames = new List <ImportedAnimationFrame>();
            foreach (var anim in animationSheet.animations)
            {
                foreach (var frame in anim.frames)
                {
                    animationSheet.frames.Add(frame);
                }
            }

            return(animationSheet);
        }