Assetdatabase.renameasset Renaming file failed

Source: Internet
Author: User






The code to write a unity Editor today will change Format for all Texture selected in the Project Panel and then rename it to XXX. Dither.png then automatically makes the 16-bit compression map mentioned in the previous article






Just started to change Format, but do not know how to rename the resource file, after Google, found the appropriate API:









An empty string "" is returned when the rename succeeds, otherwise an error message is returned






The first two parameters I passed in were full-path, but each time I returned an error



Trying to move asset as a sub directory of a directory that does not exist assets/ui/assets/ui/xxx.png



So I take the second parameter out of the full path, leaving only the new file name. That's it, here's the code.





 
 
 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 }





Assetdatabase.renameasset Renaming file failed


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.