using System.IO; using System.Reflection; using UnityEditor; using UnityEngine; public class ImportPicture : AssetPostprocessor { void OnPreprocessTexture() { TextureImporter importer = assetImporter as TextureImporter; if (importer != null) { if (IsFirstImport(importer)) { importer.textureType = TextureImporterType.Sprite; TextureImporterPlatformSettings settings = importer.GetPlatformTextureSettings("iPhone"); bool isPowerOfTwo = IsPowerOfTwo(importer); TextureImporterFormat defaultAlpha = isPowerOfTwo ? TextureImporterFormat.PVRTC_RGBA4 : TextureImporterFormat.ASTC_RGBA_4x4; TextureImporterFormat defaultNotAlpha = isPowerOfTwo ? TextureImporterFormat.PVRTC_RGB4 : TextureImporterFormat.ASTC_RGB_6x6; settings.overridden = true; settings.format = importer.DoesSourceTextureHaveAlpha() ? defaultAlpha : defaultNotAlpha; importer.SetPlatformTextureSettings(settings); settings = importer.GetPlatformTextureSettings("Android"); settings.overridden = true; settings.allowsAlphaSplitting = false; bool divisible4 = IsDivisibleOf4(importer); defaultAlpha = divisible4 ? TextureImporterFormat.ETC2_RGBA8Crunched : TextureImporterFormat.ASTC_RGBA_4x4; defaultNotAlpha = divisible4 ? TextureImporterFormat.ETC_RGB4Crunched : TextureImporterFormat.ASTC_RGB_6x6; settings.format = importer.DoesSourceTextureHaveAlpha() ? defaultAlpha : defaultNotAlpha; importer.SetPlatformTextureSettings(settings); } } } //Divide by 4 bool IsDivisibleOf4(TextureImporter importer) { (int width, int height) = GetTextureImporterSize(importer); return (width % 4 == 0 && height % 4 == 0); } //Integer power of 2 bool IsPowerOfTwo(TextureImporter importer) { (int width, int height) = GetTextureImporterSize(importer); return (width == height) && (width > 0) && ((width & (width - 1)) == 0); } //The map does not exist, the meta file does not exist, and the picture size has been modified. You need to import it again bool IsFirstImport(TextureImporter importer) { (int width, int height) = GetTextureImporterSize(importer); Texture tex = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath); bool hasMeta = File.Exists(AssetDatabase.GetAssetPathFromTextMetaFilePath(assetPath)); return tex == null || !hasMeta || (tex.width != width && tex.height != height); } //Gets the width and height of the imported picture (int, int) GetTextureImporterSize(TextureImporter importer) { if (importer != null) { object[] args = new object[2]; MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke(importer, args); return ((int)args[0], (int)args[1]); } return (0, 0); } }
Here, we will first judge whether the DoesSourceTextureHaveAlpha image has a transparent channel and select the corresponding optimal solution. Another problem here is that generally, we need to put the large image of the UI in the specified directory, that is, the above code should only process the images in the specified directory. You can judge the path, but another problem will arise. First, put the picture into the unit, and then move the picture to the UI directory just specified. If this is the case, OnPreprocessTexture in the above code will not be executed. Therefore, we need to listen to the success event of resource import. If it is moved to the UI directory, we need to re import the asset.
static bool forceImport = false; static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (string str in movedAssets) { if (str.Contains("UI Save path")) { forceImport = true; //reimport and force IsFirstImport AssetDatabase.ImportAsset(str); // If you are moving to the specified UI directory, you need to Import again } } }
Article source: https://www.yxkfw.com/thread-69943-1-1.html