Example #1
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            try
            {
                var aseWorker = new AseImportContextWorkerPro(ctx);

                //Check if this is a ase file. If it's not, we have no business here
                if (!aseWorker.ContextFileIsAseFile())
                {
                    return;
                }

                var ase = new AsepriteCli(AsepriteAutoImporterSettings.GetSettings().pathToAsepriteExecutable,
                                          importOptions);

                var exportDir = GetTempFolder();

                var sheetFile     = $"{exportDir}/{aseWorker.AseFileNoExt}_sheet.png";
                var sheetDataFile = $"{exportDir}/{aseWorker.AseFileNoExt}_data.json";

                ase.ExportSpriteSheet(aseWorker.AseFile, sheetFile, sheetDataFile);

                aseWorker.TextureCreationOptions = textureCreationOptions;
                aseWorker.SpriteImportOptions    = spriteImportOptions;

                aseWorker.AddMainTextureToContext(sheetFile);

                sheetData = AseSheetData.Create(sheetDataFile, aseWorker.AseFile);

                RefreshAnimationOptions();

                aseWorker.AddIndividualSpritesToContext(sheetData);
                aseWorker.AddAnimationsToContext(sheetData,
                                                 animationOptions.ToDictionary(e => e.tagName, e => e.animationOptions));

                Directory.Delete(exportDir, true);
                AssetDatabase.SaveAssets();
            }
            catch (Exception e)
            {
                ctx.LogImportError($"AsepriteAutoImporter: Error while importing file '{ctx.assetPath}'. {e.Message}");
            }
        }
Example #2
0
        public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx)
        {
            var aseWorker = new AseImportContextWorkerPro(ctx)
            {
                TextureCreationOptions = textureCreationOptions,
                SpriteImportOptions    = spriteImportOptions
            };

            //Check if this is a ase file. If it's not, we have no business here
            if (!aseWorker.ContextFileIsAseFile())
            {
                return;
            }

            var ase = new AsepriteCli(AsepriteAutoImporterSettings.GetSettings().pathToAsepriteExecutable, importOptions);

            var tempDir       = GetTempFolder();
            var sheetDataFile = $"{tempDir}/{aseWorker.AseFileNoExt}_data.json".Replace(" ", "");

            var animationOptionsDictionary = animationOptions.ToDictionary(e => e.tagName, e => e.animationOptions);

            if (colabOptions.exportToFile)
            {
                var exportDir = GetExportDir(aseWorker.AseFileNoExt);

                var sheetFile = $"{exportDir}/{aseWorker.AseFileNoExt}_sheet.png";

                ase.ExportSpriteSheet(aseWorker.AseFile, sheetFile, sheetDataFile);

                sheetData = AseSheetData.Create(sheetDataFile, aseWorker.AseFile);

                RefreshAnimationOptions();

                var sheetAssetPath = "Assets/" + sheetFile.Split(new[] { "Assets/" }, StringSplitOptions.RemoveEmptyEntries)[1];

                AssetDatabase.ImportAsset(sheetAssetPath, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
                AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);

                aseWorker.MainTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(sheetAssetPath);

                if (!Directory.Exists(Path.GetDirectoryName(sheetAssetPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(sheetAssetPath));
                }

                void GenerateSheetLocal()
                {
                    GenerateSheet(aseWorker, sheetAssetPath, animationOptionsDictionary);
                    EditorApplication.delayCall -= GenerateSheetLocal;
                }

                EditorApplication.delayCall += GenerateSheetLocal;
            }
            else
            {
                var sheetFile = $"{tempDir}/{aseWorker.AseFileNoExt}_sheet.png";

                ase.ExportSpriteSheet(aseWorker.AseFile, sheetFile, sheetDataFile);
                sheetData = AseSheetData.Create(sheetDataFile, aseWorker.AseFile);

                aseWorker.AddMainTextureToContext(sheetFile);

                RefreshAnimationOptions();

                aseWorker.AddIndividualSpritesToContext(sheetData);
                aseWorker.AddAnimationsToContext(sheetData, animationOptionsDictionary);
            }

            Directory.Delete(tempDir, true);

            AssetDatabase.SaveAssets();
        }