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