UI图片导入时自动调整贴图参数

 UI图片在PC, Android, iOS平台上所用的格式设置为推荐的值。

#if UNITY_EDITOR

using System.IO;
using UnityEditor;
using UnityEngine;

public class UITextureConfig : AssetPostprocessor
{

    private bool IsIgnoreAsset()
    {
        if (!assetImporter.assetPath.Contains("Assets/UI/"))
            return true;

        var ext = Path.GetExtension(assetImporter.assetPath);
        switch (ext.ToLower())
        {
            case ".png":
            case ".jpg":
            case ".jpeg":
                break;
            default:
                Debug.LogError($"unsupport ui image: {assetImporter.assetPath}");
                return true;
        }
        return false;
    }

    /// 纹理导入之前调用
    void OnPreprocessTexture()
    {
    }

    /// 纹理导入之后调用
    public void OnPostprocessTexture(Texture2D texture)
    {
        if (IsIgnoreAsset())
            return;

        var texImporter = (TextureImporter)assetImporter;

        SetSettings(texImporter);

        var hasAlphaPixel = TextureConfigUtil.HasTransparentPixel(assetImporter.assetPath, texture);
        texImporter.alphaIsTransparency = hasAlphaPixel;

        SetPlatformSettings(texImporter, texImporter.GetDefaultPlatformTextureSettings(), hasAlphaPixel, false);
        SetPlatformSettings(texImporter, texImporter.GetPlatformTextureSettings("Standalone"), hasAlphaPixel, false);
        SetPlatformSettings(texImporter, texImporter.GetPlatformTextureSettings("Android"), hasAlphaPixel, false);
        SetPlatformSettings(texImporter, texImporter.GetPlatformTextureSettings("iPhone"), hasAlphaPixel, false);
    }

    public static void SetSettings(TextureImporter texImporter)
    {
        texImporter.textureType = TextureImporterType.Sprite;

        var setting = new TextureImporterSettings();
        texImporter.ReadTextureSettings(setting);
        setting.spritePixelsPerUnit = 100;
        setting.spriteGenerateFallbackPhysicsShape = false;
        texImporter.SetTextureSettings(setting);

        texImporter.isReadable = false;
        texImporter.mipmapEnabled = false;

        texImporter.maxTextureSize = 2048;
    }

    private static void SetPlatformSettings(TextureImporter texImporter, TextureImporterPlatformSettings settings, bool isTransparent, bool isCrunch)
    {
        bool isApplySettings = true;
        switch (settings.name)
        {
        case "DefaultTexturePlatform":
            settings.maxTextureSize = 2048;
            settings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
            settings.format = TextureImporterFormat.Automatic;
            settings.textureCompression = TextureImporterCompression.Compressed;
            settings.crunchedCompression = isCrunch;
            settings.compressionQuality = 100;
            break;
        case "Standalone":
            settings.overridden = true;
            settings.maxTextureSize = 2048;
            settings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
            if (isCrunch)
            {
                if (isTransparent)
                    settings.format = TextureImporterFormat.DXT5Crunched;
                else
                    settings.format = TextureImporterFormat.DXT1Crunched;
            }
            else
            {
                if (isTransparent)
                    settings.format = TextureImporterFormat.DXT5;
                else
                    settings.format = TextureImporterFormat.DXT1;
            }
            settings.compressionQuality = 100;
            break;
        case "Android":
            settings.overridden = true;
            settings.maxTextureSize = 2048;
            settings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
            if (isCrunch)
            {
                if (isTransparent)
                    settings.format = TextureImporterFormat.ETC2_RGBA8Crunched;
                else
                    settings.format = TextureImporterFormat.ETC2_RGB4;
            }
            else
            {
                if (isTransparent)
                    settings.format = TextureImporterFormat.ETC2_RGBA8;
                else
                    settings.format = TextureImporterFormat.ETC2_RGB4;
            }
            settings.compressionQuality = 100;
            //settings.allowsAlphaSplitting = isTransparent;
            settings.androidETC2FallbackOverride = AndroidETC2FallbackOverride.UseBuildSettings;
            break;
        case "iPhone":
            settings.overridden = true;
            settings.maxTextureSize = 2048;
            settings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
            if (isTransparent)
                settings.format = TextureImporterFormat.ASTC_RGBA_4x4;
            else
                settings.format = TextureImporterFormat.ASTC_RGB_6x6;
            settings.compressionQuality = 100;
            break;
        default:
            isApplySettings = false;
            return;
        }

        if (isApplySettings)
        {
            texImporter.SetPlatformTextureSettings(settings);
        }
    }

}

#endif

 

#if UNITY_EDITOR

using System;
using System.IO;
using UnityEditor;
using UnityEngine;

public static class TextureConfigUtil
{

    public static bool HasTransparentPixel(string texPath, Texture2D tex)
    {
        var fileName = Path.GetFileNameWithoutExtension(texPath);
        //alpha贴图
        if (fileName.EndsWith("_alpha", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith("_a", StringComparison.OrdinalIgnoreCase))
            return false;

        TextureImporter texImporter = (TextureImporter)AssetImporter.GetAtPath(texPath);
        if (!texImporter.DoesSourceTextureHaveAlpha()) //没有透明像素的图片也能开启alpha通道的, 没开alpha通道肯定没透明像素
            return false;

        var isReadableBak = tex.isReadable;
        if (!tex.isReadable)
        {
            texImporter.isReadable = true;
            texImporter.SaveAndReimport();
        }

        try
        {
            for (var x = 0; x < tex.width; x++)
            {
                for (var y = 0; y < tex.height; ++y)
                {
                    var c = tex.GetPixel(x, y);
                    if (c.a < 1.0f)
                    {
                        Debug.Log($"({x}, {y}) pixel transparent");
                        return true;
                    }
                }
            }
        }
        finally
        {
            if (!isReadableBak)
            {
                texImporter.isReadable = false;
                texImporter.SaveAndReimport();
            }
        }

        return false;
    }

}

#endif

 

代码和Inspector面板的对应关系

 

参考

Unity 检测资源文件导入_unity 监听资源导入_[奋斗不止]的博客-CSDN博客



posted @ 2023-03-17 23:09  yanghui01  阅读(97)  评论(0编辑  收藏  举报