using UnityEditor; using UnityEngine; using XGame.Editor.Asset.Tools; namespace FL.Editor { /// /// 纹理资源属性预设 /// public partial class FLAssetPostprocessor : AssetPostprocessor { /// /// Assets相对路径前缀 /// private const string ResRelative = "Assets/Res/"; private const string i18nRelative = "Assets/i18n/"; private const string Readable = "readable"; private const string Mipmap = "mipmap"; private const string UIAtlas = "/Atlas/"; private const string Sprites = "/Sprites/"; private const string Textures = "/Textures/"; private const string FGUITextures = "/Addressable/FGUI/"; private const string SpineTextures = "/Spines/"; private const int AtlasTextureMaxSize = 512; private const int TextureMaxSize = 2048; private const TextureImporterFormat HaveAlphaSettingFormat = TextureImporterFormat.ASTC_6x6; private const TextureImporterFormat AndroidNoAlphaSettingFormat = TextureImporterFormat.ETC2_RGB4; private const TextureImporterFormat IosNoAlphaSettingFormat = TextureImporterFormat.ASTC_6x6; private void OnPreprocessTexture() { if (!assetPath.StartsWith(ResRelative)) return; // CheckTextureAlpha(assetPath); var textureImporter = (TextureImporter)assetImporter; textureImporter.isReadable = assetPath.ToLower().Contains(Readable); textureImporter.anisoLevel = 1; var haveAlpha = textureImporter.DoesSourceTextureHaveAlpha(); #region Atlas if (assetPath.Contains(UIAtlas)) { textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.mipmapEnabled = false; var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings(); defaultSetting.format = TextureImporterFormat.Automatic; defaultSetting.textureCompression = TextureImporterCompression.Uncompressed; defaultSetting.maxTextureSize = AtlasTextureMaxSize; textureImporter.SetPlatformTextureSettings(defaultSetting); return; } #endregion #region Sprite if (assetPath.Contains(Sprites)) { textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.mipmapEnabled = false; textureImporter.npotScale = TextureImporterNPOTScale.None; var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings(); defaultSetting.format = TextureImporterFormat.Automatic; defaultSetting.textureCompression = TextureImporterCompression.Compressed; defaultSetting.maxTextureSize = TextureMaxSize; textureImporter.SetPlatformTextureSettings(defaultSetting); //ios var iosSetting = textureImporter.GetPlatformTextureSettings("iOS"); iosSetting.overridden = true; iosSetting.format = haveAlpha ? HaveAlphaSettingFormat : IosNoAlphaSettingFormat; iosSetting.textureCompression = TextureImporterCompression.Compressed; iosSetting.maxTextureSize = TextureMaxSize; textureImporter.SetPlatformTextureSettings(iosSetting); //Android var androidSetting = textureImporter.GetPlatformTextureSettings("Android"); androidSetting.overridden = true; androidSetting.format = haveAlpha ? HaveAlphaSettingFormat : AndroidNoAlphaSettingFormat; androidSetting.textureCompression = TextureImporterCompression.Compressed; androidSetting.maxTextureSize = TextureMaxSize; textureImporter.SetPlatformTextureSettings(androidSetting); return; } #endregion #region Texture if (assetPath.Contains(Textures) || assetPath.Contains(FGUITextures)) { textureImporter.textureType = TextureImporterType.Default; // Unity导入贴图默认使用的sprite,切换为Default时会开启mipmapEnabled,因此需要在设置textureType后再设置mipmapEnabled textureImporter.mipmapEnabled = assetPath.ToLower().Contains(Mipmap); var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings(); defaultSetting.format = TextureImporterFormat.Automatic; defaultSetting.textureCompression = TextureImporterCompression.Compressed; defaultSetting.maxTextureSize = TextureMaxSize; textureImporter.SetPlatformTextureSettings(defaultSetting); //ios var iosSetting = textureImporter.GetPlatformTextureSettings("iOS"); iosSetting.overridden = true; iosSetting.format = haveAlpha ? HaveAlphaSettingFormat : IosNoAlphaSettingFormat; iosSetting.textureCompression = TextureImporterCompression.Compressed; iosSetting.maxTextureSize = TextureMaxSize; textureImporter.SetPlatformTextureSettings(iosSetting); //Android var androidSetting = textureImporter.GetPlatformTextureSettings("Android"); androidSetting.overridden = true; androidSetting.format = haveAlpha ? HaveAlphaSettingFormat : AndroidNoAlphaSettingFormat; androidSetting.textureCompression = TextureImporterCompression.Compressed; androidSetting.maxTextureSize = TextureMaxSize; textureImporter.SetPlatformTextureSettings(androidSetting); return; } #endregion #region Spine return; if (assetPath.Contains(SpineTextures)) { textureImporter.textureType = TextureImporterType.Default; // Unity导入贴图默认使用的sprite,切换为Default时会开启mipmapEnabled,因此需要在设置textureType后再设置mipmapEnabled textureImporter.mipmapEnabled = assetPath.ToLower().Contains(Mipmap); textureImporter.npotScale = TextureImporterNPOTScale.None; var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings(); defaultSetting.format = TextureImporterFormat.Automatic; defaultSetting.textureCompression = TextureImporterCompression.CompressedHQ; defaultSetting.maxTextureSize = TextureMaxSize; textureImporter.SetPlatformTextureSettings(defaultSetting); //ios var iosSetting = textureImporter.GetPlatformTextureSettings("iOS"); iosSetting.overridden = true; iosSetting.format = haveAlpha ? HaveAlphaSettingFormat : IosNoAlphaSettingFormat; iosSetting.textureCompression = TextureImporterCompression.CompressedHQ; iosSetting.maxTextureSize = TextureMaxSize; textureImporter.SetPlatformTextureSettings(iosSetting); //Android var androidSetting = textureImporter.GetPlatformTextureSettings("Android"); androidSetting.overridden = true; androidSetting.format = haveAlpha ? HaveAlphaSettingFormat : AndroidNoAlphaSettingFormat; androidSetting.compressionQuality = 100; //这个参数是百分比的意思,100的时候才能保持贴图精度 androidSetting.textureCompression = TextureImporterCompression.CompressedHQ; androidSetting.maxTextureSize = TextureMaxSize; textureImporter.SetPlatformTextureSettings(androidSetting); } #endregion } private void OnPostprocessTexture(Texture2D texture) { //检测texture var task = new TextureVerifyTask(); task.VerifyTextureByPath(assetPath); //Packages 文件夹下不判断 //if (assetPath.Contains("Packages")) // return; //if (assetPath.ToLower().Contains("atlas")) // return; //if (EditorUtility.DisplayDialog(title, message, confirm)) //{ // AssetDatabase.DeleteAsset(assetPath); // AssetDatabase.Refresh(); //} } } }