问题
有时项目里的美术资源会引用很多的DDS格式的图片,这种格式在Android上是没问题的,等到打IOS ab包的时候就会报错,因为苹果不支持DDS格式,这时就要对项目里DDS格式的资源进行处理
打包出现的报错
当有dds格式的时候,打包ios就会报下面的报错
DXT3 compressed textures are not supported when publishing to iPhone
Assets/Res/Scene/bhd/Texture/floor.DDS
解决
- 1:将floor.DDS转为其他格式(这里推荐使用Xnconvert来转换)非常方便,可以直接输出到原目录
- 2: 由于项目有的材质球已经引用了floor.DDS直接将其删除那么就会出问题,材质球就会出现丢失,显然是不能这样做的,那么此时就需要找到哪些资源引用了floor.DDS,unity提供了一个方法AssetDatabase.GetDependencies(path)该方法会返回文件应用了那些资源,显然和我们的需求是相反的,那么我们就要找到项目中所有的DDS格式的图片,然后找到哪些材质球引用了该图片,重新设置材质球的Texture,这样才可以,代码如下
- [MenuItem("Jenkins/一键替换DDS格式(需要选中Assets目录)")]
private static void ReplaceAllDDS()
{
Dictionary<string, string> guidItems = new Dictionary<string, string>();
Dictionary<string, string> guidItemPaths = new Dictionary<string, string>();
EditorUtility.DisplayProgressBar("正在获取所有DDS图片", "", 0f);
UnityEngine.Object[] m_objects = Selection.GetFiltered(typeof(Texture), SelectionMode.DeepAssets);//选择的所有对象
int allCount = m_objects.Length;
int count = 0;
foreach (UnityEngine.Object item in m_objects)
{
count = count + 1;
float progress = (float)count / (float)allCount;
string des = count.ToString() + "/" + allCount.ToString();
EditorUtility.DisplayProgressBar("正在搜集DDS图片", des, progress);
string assetPath = AssetDatabase.GetAssetPath(item);
if (Path.GetExtension(assetPath) == ".dds")
{
string guid = AssetDatabase.AssetPathToGUID(assetPath);
if (!guidItems.ContainsKey(guid))
{
guidItems[guid] = item.name;
guidItemPaths[guid] = assetPath;
}
}
}
EditorUtility.ClearProgressBar();
if (guidItems.Count > 0)
{
List<string> withoutExtensions = new List<string>() { ".mat" };
string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories).Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
for (int i = 0; i < files.Length; i++)
{
string file = files[i];
if (i % 20 == 0)
{
bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配检查中", file, (float)i / (float)files.Length);
if (isCancel)
{
break;
}
}
foreach (KeyValuePair<string, string> guidItem in guidItems)
{
if (Regex.IsMatch(File.ReadAllText(file), guidItem.Key))
{
string ddsPath = guidItemPaths[guidItem.Key];
Debug.Log(string.Format("dds:{0} 引用的资源: {1}", ddsPath, file));
string assetPath = file.Substring(Application.dataPath.Length - 6).Replace(Path.DirectorySeparatorChar, '/');
Material s_materila = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Material)) as Material;
if (s_materila)
{
string newTextruePath = ddsPath.Replace(".dds", ".png");
if (Path.GetExtension(newTextruePath) != "")//判断同目录下是否有同名.png文件
{
s_materila.mainTexture = AssetDatabase.LoadAssetAtPath<Texture>(newTextruePath);
//替换成功后删除.dds格式的贴图文件
AssetDatabase.DeleteAsset(ddsPath);
Debug.Log("替换dds完成:= " + s_materila.name);
}
else
{
Debug.LogError("同目录下没有找到png文件:= " + newTextruePath + "请使用Xnconvert进行转换");
}
}
}
}
}
EditorUtility.ClearProgressBar();
}
//保存并刷新资源
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
最后点击Jenkins/一键替换DDS格式(需要选中Assets目录)就大功告成了