FLAssetPostprocessor.Texture.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using UnityEditor;
  2. using UnityEngine;
  3. using XGame.Editor.Asset;
  4. namespace FL.Editor
  5. {
  6. /// <summary>
  7. /// 纹理资源属性预设
  8. /// </summary>
  9. public partial class FLAssetPostprocessor : AssetPostprocessor
  10. {
  11. /// <summary>
  12. /// Assets相对路径前缀
  13. /// </summary>
  14. private const string ResRelative = "Assets/Res/";
  15. private const string i18nRelative = "Assets/i18n/";
  16. private const string Readable = "readable";
  17. private const string Mipmap = "mipmap";
  18. private const string UIAtlas = "/Atlas/";
  19. private const string Sprites = "/Sprites/";
  20. private const string Textures = "/Textures/";
  21. private const string FGUITextures = "/Addressable/FGUI/";
  22. private const string SpineTextures = "/Spines/";
  23. private const int AtlasTextureMaxSize = 512;
  24. private const int TextureMaxSize = 2048;
  25. private const TextureImporterFormat HaveAlphaSettingFormat = TextureImporterFormat.ASTC_6x6;
  26. private const TextureImporterFormat AndroidNoAlphaSettingFormat = TextureImporterFormat.ETC2_RGB4;
  27. private const TextureImporterFormat IosNoAlphaSettingFormat = TextureImporterFormat.ASTC_6x6;
  28. private void OnPreprocessTexture()
  29. {
  30. if (!assetPath.StartsWith(ResRelative))
  31. return;
  32. // CheckTextureAlpha(assetPath);
  33. var textureImporter = (TextureImporter)assetImporter;
  34. textureImporter.isReadable = assetPath.ToLower().Contains(Readable);
  35. textureImporter.anisoLevel = 1;
  36. var haveAlpha = textureImporter.DoesSourceTextureHaveAlpha();
  37. #region Atlas
  38. if (assetPath.Contains(UIAtlas))
  39. {
  40. textureImporter.textureType = TextureImporterType.Sprite;
  41. textureImporter.spriteImportMode = SpriteImportMode.Single;
  42. textureImporter.mipmapEnabled = false;
  43. var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings();
  44. defaultSetting.format = TextureImporterFormat.Automatic;
  45. defaultSetting.textureCompression = TextureImporterCompression.Uncompressed;
  46. defaultSetting.maxTextureSize = AtlasTextureMaxSize;
  47. textureImporter.SetPlatformTextureSettings(defaultSetting);
  48. return;
  49. }
  50. #endregion
  51. #region Sprite
  52. if (assetPath.Contains(Sprites))
  53. {
  54. textureImporter.textureType = TextureImporterType.Sprite;
  55. textureImporter.spriteImportMode = SpriteImportMode.Single;
  56. textureImporter.mipmapEnabled = false;
  57. textureImporter.npotScale = TextureImporterNPOTScale.None;
  58. var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings();
  59. defaultSetting.format = TextureImporterFormat.Automatic;
  60. defaultSetting.textureCompression = TextureImporterCompression.Compressed;
  61. defaultSetting.maxTextureSize = TextureMaxSize;
  62. textureImporter.SetPlatformTextureSettings(defaultSetting);
  63. //ios
  64. var iosSetting = textureImporter.GetPlatformTextureSettings("iOS");
  65. iosSetting.overridden = true;
  66. iosSetting.format = haveAlpha ? HaveAlphaSettingFormat : IosNoAlphaSettingFormat;
  67. iosSetting.textureCompression = TextureImporterCompression.Compressed;
  68. iosSetting.maxTextureSize = TextureMaxSize;
  69. textureImporter.SetPlatformTextureSettings(iosSetting);
  70. //Android
  71. var androidSetting = textureImporter.GetPlatformTextureSettings("Android");
  72. androidSetting.overridden = true;
  73. androidSetting.format = haveAlpha ? HaveAlphaSettingFormat : AndroidNoAlphaSettingFormat;
  74. androidSetting.textureCompression = TextureImporterCompression.Compressed;
  75. androidSetting.maxTextureSize = TextureMaxSize;
  76. textureImporter.SetPlatformTextureSettings(androidSetting);
  77. return;
  78. }
  79. #endregion
  80. #region Texture
  81. if (assetPath.Contains(Textures) || assetPath.Contains(FGUITextures))
  82. {
  83. textureImporter.textureType = TextureImporterType.Default;
  84. // Unity导入贴图默认使用的sprite,切换为Default时会开启mipmapEnabled,因此需要在设置textureType后再设置mipmapEnabled
  85. textureImporter.mipmapEnabled = assetPath.ToLower().Contains(Mipmap);
  86. var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings();
  87. defaultSetting.format = TextureImporterFormat.Automatic;
  88. defaultSetting.textureCompression = TextureImporterCompression.Compressed;
  89. defaultSetting.maxTextureSize = TextureMaxSize;
  90. textureImporter.SetPlatformTextureSettings(defaultSetting);
  91. //ios
  92. var iosSetting = textureImporter.GetPlatformTextureSettings("iOS");
  93. iosSetting.overridden = true;
  94. iosSetting.format = haveAlpha ? HaveAlphaSettingFormat : IosNoAlphaSettingFormat;
  95. iosSetting.textureCompression = TextureImporterCompression.Compressed;
  96. iosSetting.maxTextureSize = TextureMaxSize;
  97. textureImporter.SetPlatformTextureSettings(iosSetting);
  98. //Android
  99. var androidSetting = textureImporter.GetPlatformTextureSettings("Android");
  100. androidSetting.overridden = true;
  101. androidSetting.format = haveAlpha ? HaveAlphaSettingFormat : AndroidNoAlphaSettingFormat;
  102. androidSetting.textureCompression = TextureImporterCompression.Compressed;
  103. androidSetting.maxTextureSize = TextureMaxSize;
  104. textureImporter.SetPlatformTextureSettings(androidSetting);
  105. return;
  106. }
  107. #endregion
  108. #region Spine
  109. return;
  110. if (assetPath.Contains(SpineTextures))
  111. {
  112. textureImporter.textureType = TextureImporterType.Default;
  113. // Unity导入贴图默认使用的sprite,切换为Default时会开启mipmapEnabled,因此需要在设置textureType后再设置mipmapEnabled
  114. textureImporter.mipmapEnabled = assetPath.ToLower().Contains(Mipmap);
  115. textureImporter.npotScale = TextureImporterNPOTScale.None;
  116. var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings();
  117. defaultSetting.format = TextureImporterFormat.Automatic;
  118. defaultSetting.textureCompression = TextureImporterCompression.CompressedHQ;
  119. defaultSetting.maxTextureSize = TextureMaxSize;
  120. textureImporter.SetPlatformTextureSettings(defaultSetting);
  121. //ios
  122. var iosSetting = textureImporter.GetPlatformTextureSettings("iOS");
  123. iosSetting.overridden = true;
  124. iosSetting.format = haveAlpha ? HaveAlphaSettingFormat : IosNoAlphaSettingFormat;
  125. iosSetting.textureCompression = TextureImporterCompression.CompressedHQ;
  126. iosSetting.maxTextureSize = TextureMaxSize;
  127. textureImporter.SetPlatformTextureSettings(iosSetting);
  128. //Android
  129. var androidSetting = textureImporter.GetPlatformTextureSettings("Android");
  130. androidSetting.overridden = true;
  131. androidSetting.format = haveAlpha ? HaveAlphaSettingFormat : AndroidNoAlphaSettingFormat;
  132. androidSetting.compressionQuality = 100; //这个参数是百分比的意思,100的时候才能保持贴图精度
  133. androidSetting.textureCompression = TextureImporterCompression.CompressedHQ;
  134. androidSetting.maxTextureSize = TextureMaxSize;
  135. textureImporter.SetPlatformTextureSettings(androidSetting);
  136. }
  137. #endregion
  138. }
  139. private void OnPostprocessTexture(Texture2D texture)
  140. {
  141. //检测texture
  142. var task = new TextureVerifyTask();
  143. task.VerifyTextureByPath(assetPath);
  144. //Packages 文件夹下不判断
  145. //if (assetPath.Contains("Packages"))
  146. // return;
  147. //if (assetPath.ToLower().Contains("atlas"))
  148. // return;
  149. //if (EditorUtility.DisplayDialog(title, message, confirm))
  150. //{
  151. // AssetDatabase.DeleteAsset(assetPath);
  152. // AssetDatabase.Refresh();
  153. //}
  154. }
  155. }
  156. }