AssetDatabase.RenameAsset 重命名文件失败
今天想写一段Unity Editor 的代码将在 Project Panel 中选中的所有 Texture 改变 Format,然后重命名 成 xxx.Dither.png 然后自动进行上一篇文章提到的 16位压缩贴图
刚开始改变 Format 都可以了,可是不知道如何对资源文件重命名,经过google,找到了相应API:
当重命名成功时会返回 空串 "" ,否则返回错误信息
刚开始我传入的两个参数都是 全路径的,但每次都返回错误
Trying to move asset as a sub directory of a directory that does not exist Assets/UI/Assets/UI/xxx.png
因此我把第二个参数去掉全路径,只保留新的文件名。。就成功了,以下是代码
1 [MenuItem("Edit/Transform Texture To Dither")] 2 static void SetSelectTextureToDither() 3 { 4 foreach(Object obj in Selection.GetFiltered(typeof(Object), SelectionMode.Assets)) 5 { 6 string path = path = AssetDatabase.GetAssetPath(obj); ; 7 TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; 8 if (textureImporter == null) 9 continue; 10 11 textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor; 12 textureImporter.mipmapEnabled = false; 13 14 AssetDatabase.WriteImportSettingsIfDirty(path); 15 string newPathName = Path.GetFileNameWithoutExtension(path) + ".Dither"; 16 string renameRes = AssetDatabase.RenameAsset(path, newPathName); 17 if (renameRes != "") 18 Debug.Log("Fail to rename, err: " + renameRes); 19 AssetDatabase.WriteImportSettingsIfDirty(newPathName); 20 } 21 Debug.Log("Complete transform Texture to Dither!!"); 22 }