public override void OnImportAsset(AssetImportContext ctx)
        {
            name = GetFileName(ctx.assetPath);

            AseFile aseFile    = ReadAseFile(ctx.assetPath);
            int     frameCount = aseFile.Header.Frames;

            SpriteAtlasBuilder atlasBuilder = new SpriteAtlasBuilder(textureSettings, aseFile.Header.Width, aseFile.Header.Height);

            Texture2D[] frames = null;
            if (importType != AseFileImportType.LayerToSprite)
            {
                frames = aseFile.GetFrames();
            }
            else
            {
                frames = aseFile.GetLayersAsFrames();
            }

            SpriteImportData[] spriteImportData = new SpriteImportData[0];

            //if (textureSettings.transparentMask)
            //{
            //    atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentColor, false);
            //}
            //else
            //{
            //    atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, false);

            //}

            atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentMask, false);


            atlas.filterMode          = textureSettings.filterMode;
            atlas.alphaIsTransparency = false;
            atlas.wrapMode            = TextureWrapMode.Clamp;
            atlas.name = "Texture";

            ctx.AddObjectToAsset("Texture", atlas);

            ctx.SetMainObject(atlas);

            switch (importType)
            {
            case AseFileImportType.LayerToSprite:
            case AseFileImportType.Sprite:
                ImportSprites(ctx, aseFile, spriteImportData);
                break;

            case AseFileImportType.Tileset:
                ImportTileset(ctx, atlas);
                break;
            }

            ctx.SetMainObject(atlas);
        }
        private void GenerateSeparateTexturesFromLayers(AssetImportContext ctx, AseFile aseFile)
        {
            SpriteAtlasBuilder atlasBuilder = new SpriteAtlasBuilder(textureSettings, aseFile.Header.Width, aseFile.Header.Height);

            SpriteImportData[] spriteImportData = new SpriteImportData[0];
            List <LayerChunk>  layers           = aseFile.GetChunks <LayerChunk>();

            for (int i = 0; i < layers.Count; i++)
            {
                List <Texture2D> layerFrames = aseFile.GetLayerTexture(i, layers[i]);
                Texture2D        layerAtlas;
                layerAtlas                     = atlasBuilder.GenerateAtlas(layerFrames.ToArray(), out spriteImportData, textureSettings.transparentMask, false);
                layerAtlas.filterMode          = textureSettings.filterMode;
                layerAtlas.alphaIsTransparency = false;
                layerAtlas.wrapMode            = TextureWrapMode.Clamp;
                layerAtlas.name                = layers[i].LayerName;
                //AssetDatabase.CreateAsset( layerAtlas, Path.GetDirectoryName( ctx.assetPath ) + "/" + Path.GetFileNameWithoutExtension( ctx.assetPath ) + "-" + layers[i].LayerName + ".asset" );
                ctx.AddObjectToAsset(layers[i].LayerName, layerAtlas);
                if (i == 0)
                {
                    // assign member "atlas" for ImportSprites() etc below
                    atlas = layerAtlas;
                    ctx.SetMainObject(layerAtlas);
                }
            }

            switch (importType)
            {
            case AseFileImportType.LayerToSprite:
            case AseFileImportType.Sprite:
                ImportSprites(ctx, aseFile, spriteImportData);
                break;

            case AseFileImportType.Tileset:
                ImportTileset(ctx);
                break;
            }
        }
Exemple #3
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            name = GetFileName(ctx.assetPath);

            AseFile aseFile = ReadAseFile(ctx.assetPath);

            //Debug.Log(string.Format("导入Ase动画文件:{0},帧间隔为{1}(相当于动画帧率{2})", name, aseFile.Header.Speed, Mathf.RoundToInt(1000 / aseFile.Header.Speed)));

            this.aseFile = aseFile;

            SpriteAtlasBuilder atlasBuilder = new SpriteAtlasBuilder(textureSettings, aseFile.Header.Width, aseFile.Header.Height);

            Texture2D[] frames;
            if (importType != AseFileImportType.LayerToSprite)
            {
                frames = aseFile.GetFrames();
            }
            else
            {
                frames = aseFile.GetLayersAsFrames();
            }

            SpriteImportData[] spriteImportData = new SpriteImportData[0];

            //if (textureSettings.transparentMask)
            //{
            //    atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentColor, false);
            //}
            //else
            //{
            //    atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, false);

            //}

            //翻转贴图
            if (textureSettings.flipTexture)
            {
                for (int i = 0; i < frames.Length; i++)
                {
                    frames[i] = FlipTexture(frames[i]);
                }
            }

            atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentMask, false);

            atlas.filterMode          = textureSettings.filterMode;
            atlas.alphaIsTransparency = false;
            atlas.wrapMode            = TextureWrapMode.Clamp;
            atlas.name = "Texture";

            ctx.AddObjectToAsset("Texture", atlas);

            ctx.SetMainObject(atlas);

            switch (importType)
            {
            case AseFileImportType.LayerToSprite:
            case AseFileImportType.Sprite:
                ImportSprites(ctx, aseFile, spriteImportData);
                break;

            case AseFileImportType.Tileset:
                ImportTileset(ctx, atlas);
                break;
            }

            ctx.SetMainObject(atlas);
        }
Exemple #4
0
        private void GenerateSeparateTexturesFromLayers(AssetImportContext ctx, AseFile aseFile)
        {
            SpriteAtlasBuilder atlasBuilder = new SpriteAtlasBuilder(textureSettings, aseFile.Header.Width, aseFile.Header.Height);
            List <LayerChunk>  layers       = aseFile.GetChunks <LayerChunk>();

            SpriteImportData[] spriteImportData = new SpriteImportData[0];

            {
                List <Texture2D> layerFrames = aseFile.GetLayerTexture(0, layers[0]);
                Texture2D        layerAtlas  = atlasBuilder.GenerateAtlas(layerFrames.ToArray(), out spriteImportData, textureSettings.transparentMask, false);
                layerAtlas.filterMode          = textureSettings.filterMode;
                layerAtlas.alphaIsTransparency = false;
                layerAtlas.wrapMode            = TextureWrapMode.Clamp;
                layerAtlas.name = layers[0].LayerName;
                //string assetpath = Path.GetDirectoryName( ctx.assetPath ) + "/" + Path.GetFileNameWithoutExtension( ctx.assetPath ) + "-" + layers[0].LayerName + ".asset";
                //AssetDatabase.CreateAsset( layerAtlas, assetpath );
                ctx.AddObjectToAsset(layers[0].LayerName, layerAtlas);

                switch (importType)
                {
                case AseFileImportType.LayerToSprite:
                case AseFileImportType.Sprite:
                    ImportSprites(ctx, aseFile, spriteImportData, layerAtlas);
                    break;

                case AseFileImportType.Tileset:
                    ImportTileset(ctx, layerAtlas);
                    break;
                }
            }

            for (int i = 1; i < layers.Count; i++)
            {
                List <Texture2D> layerFrames = aseFile.GetLayerTexture(i, layers[i]);
                Texture2D        layerAtlas  = atlasBuilder.GenerateAtlas(layerFrames.ToArray(), out _, textureSettings.transparentMask, false);
                layerAtlas.filterMode          = textureSettings.filterMode;
                layerAtlas.alphaIsTransparency = false;
                layerAtlas.wrapMode            = TextureWrapMode.Clamp;
                layerAtlas.name = layers[i].LayerName;

                ctx.AddObjectToAsset(layers[i].LayerName, layerAtlas);

                /*
                 * string imagepath = Path.GetDirectoryName( ctx.assetPath ) + "/" + Path.GetFileNameWithoutExtension( ctx.assetPath ) + "-" + layers[i].LayerName + ".png";
                 *
                 * Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>( imagepath );
                 * if( texture == null )
                 * {
                 * File.WriteAllBytes( imagepath, layerAtlas.EncodeToPNG() );
                 * AssetDatabase.SaveAssets();
                 * }
                 * else
                 * {
                 *
                 * //texture.SetPixelData( layerAtlas.GetRawTextureData(), 0 );
                 * //EditorUtility.SetDirty( texture );
                 * }
                 * AssetDatabase.Refresh();
                 * Texture2D asdf = AssetDatabase.LoadAssetAtPath<Texture2D>( imagepath );
                 *
                 * //AssetDatabase.CreateAsset( layerAtlas, assetpath );
                 *
                 *
                 * TextureImporter textureImporter = GetAtPath( imagepath ) as TextureImporter;
                 * if( textureImporter != null )
                 * {
                 * SecondarySpriteTexture[] sst = new SecondarySpriteTexture[1];
                 * sst[0].name = "_NormalMap";
                 * sst[0].texture = layerAtlas;
                 * textureImporter.secondarySpriteTextures = sst;
                 *
                 * EditorUtility.SetDirty( textureImporter );
                 * textureImporter.SaveAndReimport();
                 *
                 * }
                 */
            }
        }
Exemple #5
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            name = GetFileName(ctx.assetPath);

            AseFile aseFile    = ReadAseFile(ctx.assetPath);
            int     frameCount = aseFile.Header.Frames;

            SpriteAtlasBuilder atlasBuilder = new SpriteAtlasBuilder(textureSettings, aseFile.Header.Width, aseFile.Header.Height);

            Texture2D[] frames = null;
            if (importType != AseFileImportType.LayerToSprite)
            {
                frames = aseFile.GetFrames(textureSettings.ignoreBackground);
            }
            else
            {
                frames = aseFile.GetLayersAsFrames();
            }

            SpriteImportData[] spriteImportData;

            //if (textureSettings.transparentMask)
            //{
            //    atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentColor, false);
            //}
            //else
            //{
            //    atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, false);

            //}

            atlas = atlasBuilder.GenerateAtlas(frames, name, out spriteImportData, textureSettings.transparencyMode, false);

            atlas.filterMode          = textureSettings.filterMode;
            atlas.alphaIsTransparency = textureSettings.transparencyMode == TransparencyMode.Alpha;
            atlas.wrapMode            = textureSettings.wrapMode;
            atlas.name = "Texture";

            MetaData[] metadatas = aseFile.GetMetaData(textureSettings.spritePivot, textureSettings.pixelsPerUnit);

            List <SecondarySpriteTexture> secondarySpriteTextures = new List <SecondarySpriteTexture>();

            // Find any metadata with a secondary texture
            foreach (var metadata in metadatas)
            {
                if (metadata.Type == MetaDataType.SECONDARY_TEXTURE)
                {
                    var textureName = metadata.Args[0];

                    var       secondaryTextureFrames = aseFile.GetLayerTexture(metadata.LayerIndex, metadata.Layer);
                    Texture2D secondaryTexture       = atlasBuilder.GenerateAtlas(secondaryTextureFrames.ToArray(), name, out _,
                                                                                  textureSettings.transparencyMode, false);

                    secondaryTexture.alphaIsTransparency = textureSettings.transparencyMode == TransparencyMode.Alpha;;
                    secondaryTexture.wrapMode            = textureSettings.wrapMode;
                    secondaryTexture.filterMode          = textureSettings.filterMode;
                    secondaryTexture.name = textureName;

                    secondarySpriteTextures.Add(new SecondarySpriteTexture()
                    {
                        name = textureName, texture = secondaryTexture
                    });
                }
            }

            TextureGenerationSettings generationSettings = new TextureGenerationSettings();

            generationSettings.platformSettings = new TextureImporterPlatformSettings();

            generationSettings.spriteImportData         = spriteImportData;
            generationSettings.secondarySpriteTextures  = secondarySpriteTextures.ToArray();
            generationSettings.sourceTextureInformation = new SourceTextureInformation()
            {
                width         = atlas.width,
                height        = atlas.height,
                containsAlpha = true,
            };

            generationSettings.textureImporterSettings = new TextureImporterSettings
            {
                textureType         = TextureImporterType.Sprite,
                textureShape        = TextureImporterShape.Texture2D,
                alphaIsTransparency = textureSettings.transparencyMode == TransparencyMode.Alpha,
                alphaSource         = TextureImporterAlphaSource.FromInput,
                spritePixelsPerUnit = textureSettings.pixelsPerUnit,
                spritePivot         = textureSettings.spritePivot,
                spriteMeshType      = SpriteMeshType.Tight,
                convertToNormalMap  = false,
                mipmapEnabled       = false,
                sRGBTexture         = true,
                readable            = false,
                fadeOut             = false,
                wrapMode            = TextureWrapMode.Clamp,
                wrapModeU           = TextureWrapMode.Clamp,
                wrapModeV           = TextureWrapMode.Clamp,
                wrapModeW           = TextureWrapMode.Clamp
            };

            var imageBuffer     = atlas.GetRawTextureData <Color32>();
            var generatedOutput = TextureGenerator.GenerateTexture(generationSettings, imageBuffer);

            generatedOutput.texture.name = "Texture";

            // Main texture atlas
            ctx.AddObjectToAsset("Texture", generatedOutput.texture);
            ctx.SetMainObject(generatedOutput.texture);

            // Add secondary textures
            foreach (var secondarySpriteTexture in secondarySpriteTextures)
            {
                ctx.AddObjectToAsset("SecondaryTex_" + secondarySpriteTexture.name, secondarySpriteTexture.texture);
            }

            // Add sprites
            foreach (var sprite in generatedOutput.sprites)
            {
                ctx.AddObjectToAsset(sprite.name, sprite);
            }

            // Add animations
            if (importType == AseFileImportType.Sprite)
            {
                GenerateAnimations(ctx, aseFile, generatedOutput.sprites, metadatas);
            }

            switch (importType)
            {
            case AseFileImportType.LayerToSprite:
            case AseFileImportType.Sprite:
                // ImportSprites(ctx, aseFile, spriteImportData, metadatas);
                break;

            case AseFileImportType.Tileset:
                ImportTileset(ctx, atlas);
                break;
            }
        }