Exemplo n.º 1
0
        /// <summary>
        /// Generates a TextureAtlas from the given AnimationSheet object
        /// </summary>
        /// <param name="sheet">The AnimationSheet to generate the TextureAtlas of</param>
        /// <param name="cancellationToken">A cancelation token that is passed to the exporters and can be used to cancel the export process mid-way</param>
        /// <param name="progressHandler">Optional event handler for reporting the export progress</param>
        /// <returns>A TextureAtlas generated from the given AnimationSheet</returns>
        public async Task <TextureAtlas> GenerateAtlasFromAnimationSheet(AnimationSheet sheet, CancellationToken cancellationToken = new CancellationToken(), BundleExportProgressEventHandler progressHandler = null)
        {
            return(await GenerateAtlasFromAnimations(sheet.ExportSettings, sheet.Animations, sheet.Name, cancellationToken, args =>
            {
                progressHandler?.Invoke(args);

                _sheetProgress[sheet.ID] = (float)args.StageProgress / 100;
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Exports a given Bundle asynchronously, calling a progress handler along the way
        /// </summary>
        /// <param name="bundle">The bundle to export</param>
        /// <param name="cancellationToken">A cancelation token that is passed to the exporters and can be used to cancel the export process mid-way</param>
        /// <param name="progressHandler">Optional event handler for reporting the export progress</param>
        /// <returns>A task representing the concurrent export progress</returns>
        public async Task ExportBundleConcurrent(Bundle bundle, CancellationToken cancellationToken = new CancellationToken(), BundleExportProgressEventHandler progressHandler = null)
        {
            // Start with initial values for the progress export of every sheet
            float[] stageProgresses = new float[bundle.AnimationSheets.Count];
            var     exports         = new List <BundleSheetJson>();

            var progressAction = new Action <AnimationSheet>(sheet =>
            {
                if (progressHandler == null)
                {
                    return;
                }

                // Calculate total progress
                int total = (int)Math.Floor(stageProgresses.Sum() / stageProgresses.Length * 100);
                progressHandler(new SheetGenerationBundleExportProgressEventArgs(sheet, BundleExportStage.TextureAtlasGeneration, total, total / 2, "Generating sheets"));
            });

            var generationList = new List <Task>();

            for (int i = 0; i < bundle.AnimationSheets.Count; i++)
            {
                var sheet = bundle.AnimationSheets[i];
                var j     = i;
                generationList.Add(new Task(() =>
                {
                    var exp = ExportBundleSheet(sheet, cancellationToken, args =>
                    {
                        stageProgresses[j] = (float)args.StageProgress / 100;
                        progressAction(sheet);
                    });

                    try
                    {
                        var sheetJson = new BundleSheetJson(exp.Result, sheet.Name, Path.GetFullPath(bundle.ExportPath) + "\\" + sheet.Name);
                        exports.Add(sheetJson);
                    }
                    catch (TaskCanceledException)
                    {
                        // unused
                    }
                }));
            }

            var concurrent = new Task(() => { StartAndWaitAllThrottled(generationList, 7, cancellationToken); });

            concurrent.Start();

            await concurrent;

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            //
            // 2. Save the sheets to disk
            //
            for (int i = 0; i < exports.Count; i++)
            {
                var exp = exports[i];

                if (progressHandler != null)
                {
                    int progress = (int)((float)i / exports.Count * 100);
                    progressHandler.Invoke(new BundleExportProgressEventArgs(BundleExportStage.SavingToDisk, progress, 50 + progress / 2));
                }

                exp.BundleSheet.SaveToDisk(exp.ExportPath);
            }

            //
            // 3. Compose the main bundle .json
            //

            if (exports.Any(export => export.BundleSheet.ExportSettings.ExportJson))
            {
                var jsonSheetList = new List <Dictionary <string, object> >();

                foreach (var export in exports)
                {
                    if (!export.BundleSheet.ExportSettings.ExportJson)
                    {
                        continue;
                    }

                    var sheet = new Dictionary <string, object>();

                    // Path of final JSON file
                    var filePath = Utilities.GetRelativePath(Path.ChangeExtension(export.ExportPath, "json"),
                                                             bundle.ExportPath);

                    sheet["name"]        = export.SheetName;
                    sheet["sprite_file"] = filePath;

                    // Export animation list
                    var names = export.BundleSheet.Animations.Select(anim => anim.Name);

                    sheet["animations"] = names;

                    jsonSheetList.Add(sheet);
                }

                var json = new Dictionary <string, object>
                {
                    ["sheets"] = jsonSheetList
                };

                string finalPath = Path.ChangeExtension(Path.GetFullPath(bundle.ExportPath) + "\\" + bundle.Name, "json");
                string output    = JsonConvert.SerializeObject(json);

                File.WriteAllText(finalPath, output, Encoding.UTF8);
            }

            progressHandler?.Invoke(new BundleExportProgressEventArgs(BundleExportStage.Ended, 100, 100));
        }