using UnityEngine; using UnityEditor; using System; using System.IO; using System.Linq; using System.Reflection; namespace FlashTools.Internal { public class SwfAnimationAssetPostprocessor : AssetPostprocessor { static void OnPostprocessAllAssets( string[] imported_assets, string[] deleted_assets, string[] moved_assets, string[] moved_from_asset_paths) { var asset_paths = imported_assets .Where(p => Path.GetExtension(p).ToLower().Equals(".asset")); foreach ( var asset_path in asset_paths ) { var asset = AssetDatabase.LoadAssetAtPath(asset_path); if ( asset ) { AssetProcess(asset_path, asset); } } } static void AssetProcess(string asset_path, SwfAnimationAsset asset) { try { var atlas_asset = LoadAtlasAsset(asset_path); if ( atlas_asset != asset.Atlas ) { asset.Atlas = atlas_asset; ConfigureAtlas(asset_path, asset); EditorUtility.SetDirty(asset); AssetDatabase.SaveAssets(); } } catch ( Exception e ) { Debug.LogErrorFormat( "Postprocess swf animation asset error: {0}", e.Message); } } static Texture2D LoadAtlasAsset(string asset_path) { return AssetDatabase.LoadAssetAtPath( GetAtlasPath(asset_path)); } static string GetAtlasPath(string asset_path) { return Path.ChangeExtension(asset_path, ".png"); } static void ConfigureAtlas(string asset_path, SwfAnimationAsset asset) { var atlas_importer = GetBitmapsAtlasImporter(asset_path); var atlas_importer_size = GetSizeFromTextureImporter(atlas_importer); atlas_importer.spritesheet = asset.Data.Bitmaps .Select(bitmap => new SpriteMetaData{ name = bitmap.Id.ToString(), rect = new Rect( bitmap.SourceRect.xMin * atlas_importer_size.x, bitmap.SourceRect.yMin * atlas_importer_size.y, bitmap.SourceRect.width * atlas_importer_size.x, bitmap.SourceRect.height * atlas_importer_size.y)}) .ToArray(); atlas_importer.textureType = TextureImporterType.Sprite; atlas_importer.spriteImportMode = SpriteImportMode.Multiple; atlas_importer.spritePixelsPerUnit = asset.Settings.PixelsPerUnit; atlas_importer.mipmapEnabled = asset.Settings.GenerateMipMaps; atlas_importer.filterMode = SwfAtlasFilterToImporterFilter(asset.Settings.AtlasTextureFilter); atlas_importer.textureFormat = SwfAtlasFormatToImporterFormat(asset.Settings.AtlasTextureFormat); AssetDatabase.ImportAsset( GetAtlasPath(asset_path), ImportAssetOptions.ForceUpdate); } static TextureImporter GetBitmapsAtlasImporter(string asset_path) { var atlas_path = GetAtlasPath(asset_path); var atlas_importer = AssetImporter.GetAtPath(atlas_path) as TextureImporter; if ( !atlas_importer ) { throw new UnityException(string.Format( "atlas texture importer not found ({0})", atlas_path)); } return atlas_importer; } static Vector2 GetSizeFromTextureImporter(TextureImporter importer) { var method_args = new object[2]{0,0}; typeof(TextureImporter) .GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance) .Invoke(importer, method_args); return new Vector2((int)method_args[0], (int)method_args[1]); } static FilterMode SwfAtlasFilterToImporterFilter( SwfConverterSettings.SwfAtlasFilter filter) { switch ( filter ) { case SwfConverterSettings.SwfAtlasFilter.Point: return FilterMode.Point; case SwfConverterSettings.SwfAtlasFilter.Bilinear: return FilterMode.Bilinear; case SwfConverterSettings.SwfAtlasFilter.Trilinear: return FilterMode.Trilinear; default: throw new UnityException(string.Format( "incorrect swf atlas filter ({0})", filter)); } } static TextureImporterFormat SwfAtlasFormatToImporterFormat( SwfConverterSettings.SwfAtlasFormat format) { switch ( format ) { case SwfConverterSettings.SwfAtlasFormat.AutomaticCompressed: return TextureImporterFormat.AutomaticCompressed; case SwfConverterSettings.SwfAtlasFormat.Automatic16bit: return TextureImporterFormat.Automatic16bit; case SwfConverterSettings.SwfAtlasFormat.AutomaticTruecolor: return TextureImporterFormat.AutomaticTruecolor; case SwfConverterSettings.SwfAtlasFormat.AutomaticCrunched: return TextureImporterFormat.AutomaticCrunched; default: throw new UnityException(string.Format( "incorrect swf atlas format ({0})", format)); } } } }