Esempio n. 1
0
        private void GenerateSheet(AseImportContextWorker aseWorker, string sheetAssetPath, Dictionary <string, AnimationOptions> animationOptionsDictionary)
        {
            var sheetImporter = GetAtPath(sheetAssetPath) as TextureImporter;
            var texture       = AssetDatabase.LoadAssetAtPath <Texture2D>(sheetAssetPath);

            sheetImporter.wrapMode            = textureCreationOptions.textureWrapMode;
            sheetImporter.filterMode          = textureCreationOptions.filterMode;
            sheetImporter.alphaIsTransparency = textureCreationOptions.alphaIsTransparency;
            sheetImporter.spriteImportMode    = SpriteImportMode.Multiple;


            var sprites = AseImportContextWorker.EnumerateSprites(texture, sheetData, spriteImportOptions).ToArray();

            sheetImporter.spritesheet = sprites.Select(s => new SpriteMetaData
            {
                rect = s.rect,
                name = s.name,
            }).ToArray();

            sheetImporter.SaveAndReimport();


            foreach (var metaFrameTag in sheetData.meta.frameTags)
            {
                var anim = aseWorker.GenerateAnimation(metaFrameTag, sheetData.frames, sprites,
                                                       animationOptionsDictionary?.ContainsKey(metaFrameTag.name) == true
                ? animationOptionsDictionary[metaFrameTag.name]
                : AseImportContextWorker.GetDefaultAnimationOptions);


                if (anim != null)
                {
                    AssetDatabase.CreateAsset(anim, Path.GetDirectoryName(sheetAssetPath) + "/anim_" + metaFrameTag.name + ".anim");
                }
            }
        }
Esempio n. 2
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var aseWorker = new AseImportContextWorker(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();
        }