Unity 部分平台不支持dds图片格式

多选材质球,执行DDSToPNG.DdsToPng()
在这里插入图片描述

using System.IO;
using UnityEditor;
using UnityEngine;

public class DDSToPNG : Editor
{
    [MenuItem("Tools/DdsToPng", false, 100)]
    public static void DdsToPng()
    {
        foreach (var obj in Selection.objects)
        {
            if (obj.GetType() == typeof(Material))
            {
                Material _mat = (Material)obj;
                Shader shader = _mat.shader;
                // 遍历Shader的所有属性
                for (int i = 0; i < ShaderUtil.GetPropertyCount(shader); ++i)
                {
                    // 只对Texture类型属性执行
                    if (ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv)
                    {
                        // 获取属性名
                        string propertyName = ShaderUtil.GetPropertyName(shader, i);
                        // 获取属性对应的贴图
                        Texture tex = _mat.GetTexture(propertyName);
                        if (tex == null)
                            continue;
                        string texPath = AssetDatabase.GetAssetPath(tex.GetInstanceID());
                        if (texPath.EndsWith(".dds"))
                        {
                            Debug.Log(texPath);
                            string pngTex = texPath.Replace("dds", "png");
                            // 保存PNG图片
                            File.WriteAllBytes(System.Environment.CurrentDirectory + "/" + pngTex, DeCompress(tex).EncodeToPNG());
                            // 导入PNG资源
                            AssetDatabase.ImportAsset(pngTex);
                            // 重新给材质球设置贴图
                            _mat.SetTexture(propertyName, AssetDatabase.LoadAssetAtPath<Texture>(pngTex));
                        }
                    }
                }
            }
        }
        AssetDatabase.Refresh();
    }
    /// <summary>
    /// 将Texture解压到Texture2D
    /// 解决部分Texture2D转换错误
    /// </summary>
    /// <param name="source"></param>
    /// <returns>byte[]</returns>
    public static Texture2D DeCompress(Texture source)
    {
        if (source == null)
        {
            return null;
        }
        RenderTexture renderTex = RenderTexture.GetTemporary(
                    source.width,
                    source.height,
                    0,
                    RenderTextureFormat.Default,
                    RenderTextureReadWrite.Linear);
        Graphics.Blit(source, renderTex);
        RenderTexture previous = RenderTexture.active;
        RenderTexture.active = renderTex;
        Texture2D readableText = new Texture2D(source.width, source.height);
        readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
        readableText.Apply();
        RenderTexture.active = previous;
        RenderTexture.ReleaseTemporary(renderTex);
        return readableText;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值