public bool RenameAsset(string newFilename) {
var filenameComponents = new FilenameComponents(newFilename);
Debug.Assert(filenameComponents.directory ==
Path.GetDirectoryName(filename));
// If the target file exists, delete it.
if (AssetImporter.GetAtPath(newFilename) != null) {
if (!AssetDatabase.MoveAssetToTrash(newFilename)) {
UnityEngine.Debug.LogError(
"Failed to move asset to trash: " + filename);
return false;
}
}
string error = AssetDatabase.RenameAsset(
filename, filenameComponents.basenameNoExtension);
if (!String.IsNullOrEmpty(error)) {
UnityEngine.Debug.LogError(
"Failed to rename asset " + filename + " to " +
newFilename + " (" + error + ")");
return false;
}
AssetDatabase.ImportAsset(newFilename);
filename = newFilename;
UpdateAssetLabels();
return true;
}