首页 > 其他分享 >UI图片导入时自动调整贴图参数

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

时间:2023-03-17 23:24:03浏览次数:56  
标签:贴图 TextureImporterFormat false settings format 导入 UI texImporter return

 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博客



标签:贴图,TextureImporterFormat,false,settings,format,导入,UI,texImporter,return
From: https://www.cnblogs.com/sailJs/p/17227168.html

相关文章

  • 关于AWS-ACM-自己导入型Imported-证书的更新替换
    在AWS中,ACM即AmazonCertificateManager的意思,是用来存储管理证书的一个服务ACM的证书有两种类型、一种是由AWS颁发的证书,另一种还可以将自己的证书导入进来,进行管理......
  • ElementUI中对el-table的某一列的时间进行格式化
    场景前端使用el-table查询到后台的数据的某一列时间列默认是标准UTC格式,如果想按照自己想显示的格式获取年月日和时分秒进行显示怎么办。注:关注公众号霸道的程序猿获取编......
  • String、StringBuffer、StringBuilder区别与联系
      1.String类是不可变类,即一旦一个String对象被创建后,包含在这个对象中的字符序列是不可改变的,直至这个对象销毁。2.StringBuffer类则代表一个字符序列可变的字符串......
  • String、StringBuffer、StringBuilder区别与联系
      1.String类是不可变类,即一旦一个String对象被创建后,包含在这个对象中的字符序列是不可改变的,直至这个对象销毁。2.StringBuffer类则代表一个字符序列可变的字符串......
  • Tree结构UI优化显示
    整体UI面板绘制参照https://www.cnblogs.com/babashi9527/p/17146645.html接着UI面板的设计;实现树形结构菜单的方式有很多种,每一种优化UI显示的方式可能存在较大差异;我......
  • Splunk DB Connect 连接MySQL报错CLIENT_PLUGIN_AUTH is required
    01、问题描述使用SplunkDBConnect连接MySQL数据库读库时,报错CLIENT_PLUGIN_AUTHisrequired,如下图:02、原因分析根据报错信息,查阅相关资料,了解到报错原因:目标数据库......
  • ubuntu20.04 centos 7安装nginx 及nginx可视化工具 nginxWebUI
          nginxWebUI是一款图形化管理nginx配置的工具,可以使用网页来快速配置nginx的各项功能,包括http协议转发、tcp协议转发、反向代理、负载均衡、静态html服务器、ss......
  • springboot自动导入@Data注解无效问题
    随手敲个项目,发现@Data注解一直爆红,idea提示导入,但是我已经导入了  于是我又到pom文件里面查找问题,发现一个sql依赖导致了pom顶部标签爆红pom未修改前是这样的 ......
  • suitecrm
      SuiteCRM userbitnami 安装步骤注意:在华为云上开放对应的端口号suitecrm汉化包 汉化步骤......
  • Python pyyaml报错:TypeError load() missing 1 required positional argument ‘Loade
    直接开门见山,一个小报错。报错:ERROR-load()missing1requiredpositionalargument:'Loader'报错行:config=yaml.load(f)报错原因:......