Пример #1
0
        private static bool ImportImageAndMetaInfo(AnimationImportJob job)
        {
            _latestData = null;

            var zipFilePath = GetFileSystemPath(job.assetDirectory + "/" + job.fileName);

            var files = GetContentsFromZipFile(zipFilePath);

            if (files.ContainsKey("docData.json"))
            {
                string jsonData = System.Text.Encoding.UTF8.GetString(files["docData.json"]);

                PyxelEditData pyxelEditData = ReadJson(jsonData);

                List <Layer> allLayers = new List <Layer>();

                foreach (var item in pyxelEditData.canvas.layers)
                {
                    Layer  layer     = item.Value;
                    string layerName = "layer" + item.Key.ToString() + ".png";
                    layer.texture = LoadTexture(files[layerName]);
                    allLayers.Add(layer);
                }

                Texture2D image = CreateBlankTexture(new Color(0f, 0f, 0f, 0), pyxelEditData.canvas.width, pyxelEditData.canvas.height);
                for (int i = allLayers.Count - 1; i >= 0; i--)
                {
                    Layer layer = allLayers[i];

                    if (!layer.hidden)
                    {
                        float maxAlpha = layer.alpha / 255f;
                        image = CombineTextures(image, layer.texture, maxAlpha);
                    }
                }

                if (!Directory.Exists(job.directoryPathForSprites))
                {
                    Directory.CreateDirectory(job.directoryPathForSprites);
                }

                SaveTextureToAssetPath(image, job.imageAssetFilename);

                _latestData = pyxelEditData;

                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #2
0
        private static PyxelEditData ReadJson(string jsonData)
        {
            PyxelEditData data = new PyxelEditData();

            JSONObject obj = JSONObject.Parse(jsonData);

            if (obj.ContainsKey("name"))
            {
                data.name = obj["name"].Str;
            }
            if (obj.ContainsKey("tileset"))
            {
                data.tileset.tileWidth  = (int)obj["tileset"].Obj["tileWidth"].Number;
                data.tileset.tileHeight = (int)obj["tileset"].Obj["tileHeight"].Number;
                data.tileset.tilesWide  = (int)obj["tileset"].Obj["tilesWide"].Number;
                data.tileset.fixedWidth = obj["tileset"].Obj["fixedWidth"].Boolean;
                data.tileset.numTiles   = (int)obj["tileset"].Obj["numTiles"].Number;
            }
            if (obj.ContainsKey("animations"))
            {
                foreach (var item in obj["animations"].Obj)
                {
                    data.animations[int.Parse(item.Key)] = new Animation(item.Value.Obj);
                }
            }
            if (obj.ContainsKey("canvas"))
            {
                data.canvas.width      = (int)obj["canvas"].Obj["width"].Number;
                data.canvas.height     = (int)obj["canvas"].Obj["height"].Number;
                data.canvas.tileWidth  = (int)obj["canvas"].Obj["tileWidth"].Number;
                data.canvas.tileHeight = (int)obj["canvas"].Obj["tileHeight"].Number;
                data.canvas.numLayers  = (int)obj["canvas"].Obj["numLayers"].Number;
                foreach (var item in obj["canvas"].Obj["layers"].Obj)
                {
                    data.canvas.layers[int.Parse(item.Key)] = new Layer(item.Value.Obj);
                }
            }

            return(data);
        }
Пример #3
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);
        }