public void AddAnimationsToContext(AseSheetData aseSheetData,
                                           Dictionary <string, AnimationOptions> animationOptionsByTagName = null
                                           )
        {
            if (aseSheetData.meta.frameTags == null || aseSheetData.meta.frameTags.Length == 0)
            {
                return;
            }
            if (IndividualSprites == null)
            {
                GenerateIndividualSprites(aseSheetData, true);
            }

            foreach (var metaFrameTag in aseSheetData.meta.frameTags)
            {
                var anim = GenerateAnimation(metaFrameTag, aseSheetData.frames, IndividualSprites,
                                             animationOptionsByTagName?.ContainsKey(metaFrameTag.name) == true
                         ? animationOptionsByTagName[metaFrameTag.name]
                         : GetDefaultAnimationOptions);

                if (anim != null)
                {
                    AseFileContext.AddObjectToAsset("anim_" + metaFrameTag.name, anim);
                }
            }
        }
Ejemplo n.º 2
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}");
            }
        }
        public void AddAnimationsToContext(AseSheetData aseSheetData,
                                           Dictionary <string, AnimationOptions> animationOptionsByTagName = null
                                           )
        {
            if (aseSheetData.meta.frameTags == null || aseSheetData.meta.frameTags.Length == 0)
            {
                return;
            }
            if (IndividualSprites == null)
            {
                GenerateIndividualSprites(aseSheetData, true);
            }

            foreach (var metaFrameTag in aseSheetData.meta.frameTags)
            {
                GenerateAnimation(metaFrameTag, aseSheetData.frames, IndividualSprites,
                                  animationOptionsByTagName != null && animationOptionsByTagName.ContainsKey(metaFrameTag.name)
                        ? animationOptionsByTagName[metaFrameTag.name]
                        : GetDefaultAnimationOptions);
            }
        }
        public static IEnumerable <Sprite> EnumerateSprites(Texture2D texture, AseSheetData sheetData, SpriteImportOptions SpriteImportOptions)
        {
            var shouldExportSlices = sheetData.meta.slices?.Length > 0 &&
                                     (sheetData.meta.frameTags == null || sheetData.meta.frameTags.Length == 0);

            if (shouldExportSlices)
            {
                foreach (var slice in sheetData.meta.slices)
                {
                    foreach (var sliceKeyData in slice.keys)
                    {
                        var frame = sheetData.frames[sliceKeyData.frame];
                        var rect  = new Rect(frame.frame.x + sliceKeyData.bounds.x,
                                             frame.frame.h - sliceKeyData.bounds.y - sliceKeyData.bounds.h, sliceKeyData.bounds.w,
                                             sliceKeyData.bounds.h);

                        var sprite = Sprite.Create(texture, rect,
                                                   SpriteImportOptions.pivot,
                                                   SpriteImportOptions.pixelsPerUnit, SpriteImportOptions.extrude,
                                                   SpriteImportOptions.meshType,
                                                   SpriteImportOptions.border);
                        sprite.name = frame.filename + "_" + slice.name;

                        yield return(sprite);
                    }
                }
            }
            else
            {
                foreach (var sheetDataFrame in sheetData.frames)
                {
                    var sprite = Sprite.Create(texture, sheetDataFrame.frame.ToRect(), SpriteImportOptions.pivot,
                                               SpriteImportOptions.pixelsPerUnit, SpriteImportOptions.extrude, SpriteImportOptions.meshType,
                                               SpriteImportOptions.border);
                    sprite.name = sheetDataFrame.filename;
                    yield return(sprite);
                }
            }
        }
        private IEnumerable <Sprite> EnumerateSprites(AseSheetData sheetData, bool shouldExportSlices)
        {
            if (shouldExportSlices)
            {
                foreach (var slice in sheetData.meta.slices)
                {
                    foreach (var sliceKeyData in slice.keys)
                    {
                        var frame = sheetData.frames[sliceKeyData.frame];
                        var rect  = new Rect(frame.frame.x + sliceKeyData.bounds.x,
                                             frame.frame.h - sliceKeyData.bounds.y - sliceKeyData.bounds.h, sliceKeyData.bounds.w,
                                             sliceKeyData.bounds.h);

                        var sprite = Sprite.Create(MainTexture, rect,
                                                   SpriteImportOptions.pivot,
                                                   SpriteImportOptions.pixelsPerUnit, SpriteImportOptions.extrude,
                                                   SpriteImportOptions.meshType,
                                                   SpriteImportOptions.border);
                        sprite.name = frame.filename + "_" + slice.name;

                        yield return(sprite);
                    }
                }
            }
            else
            {
                foreach (var sheetDataFrame in sheetData.frames)
                {
                    var sprite = Sprite.Create(MainTexture, sheetDataFrame.frame.ToRect(), SpriteImportOptions.pivot,
                                               SpriteImportOptions.pixelsPerUnit, SpriteImportOptions.extrude, SpriteImportOptions.meshType,
                                               SpriteImportOptions.border);
                    sprite.name = sheetDataFrame.filename;
                    yield return(sprite);
                }
            }
        }
        public void GenerateIndividualSprites(AseSheetData sheetData, bool addToContext = false)
        {
            if (SpriteImportOptions == null)
            {
                SpriteImportOptions = SpriteImportOptions.Default;
            }

            if (MainTexture == null)
            {
                GenerateMainTextureFromSheetFile(Path.Combine(
                                                     Path.GetDirectoryName(sheetData.pathToSheetData) ??
                                                     throw new InvalidOperationException("Error while processing data path"),
                                                     sheetData.meta.image));
            }

            if (IndividualSprites != null)
            {
                foreach (var individualSprite in IndividualSprites)
                {
                    Object.DestroyImmediate(individualSprite);
                }
                IndividualSprites.Clear();
            }
            else
            {
                IndividualSprites = new List <Sprite>();
            }

            var addedObjects = new Dictionary <string, int>();



            if (!Config.IsLiteVersion && sheetData.meta.slices?.Length > 0 &&
                (sheetData.meta.frameTags?.Length > 0))
            {
                Debug.LogWarning(
                    "It seems that you have enabled the 'Split slices' option and have animations. This is not well supported and is usually only for static images. No slices will be exported now");
            }


            foreach (var sprite in EnumerateSprites(sheetData))
            {
                //Make sure we prevent import of two assets with the same name
                if (addedObjects.ContainsKey(sprite.name))
                {
                    var newCount = addedObjects[sprite.name] + 1;
                    addedObjects[sprite.name] = newCount;
                    sprite.name = sprite.name + "_" + newCount;
                }
                else
                {
                    addedObjects.Add(sprite.name, 1);
                }

                if (addToContext)
                {
                    AseFileContext.AddObjectToAsset(sprite.name, sprite);
                }

                IndividualSprites.Add(sprite);
            }
        }
 public List <Sprite> AddIndividualSpritesToContext(AseSheetData sheetData)
 {
     GenerateIndividualSprites(sheetData, true);
     return(IndividualSprites);
 }
 public IEnumerable <Sprite> EnumerateSprites(AseSheetData sheetData)
 {
     return(EnumerateSprites(MainTexture, sheetData, SpriteImportOptions));
 }
Ejemplo n.º 9
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();
        }