Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity AssetDatabase.RenameAsset(path, newName); can not rename extension CSharpReNameFileNameExtention_And_Meta(string path,string newName) can chenge extension
static public void CSharpReNameFileNameExtention_And_Meta(string path,string newName)
{
string[] pathArr = path.Split("/"[0]);
string tempStr = "";
for (int i = 0; i < pathArr.Length-1; i++)
{
tempStr = tempStr + pathArr[i]+"/";
}
string renameDir = tempStr;
Debug.Log("renameDir="+ renameDir);
string renameFileAndExtension = pathArr[pathArr.Length - 1];
Debug.Log("renameFileAndExtension=" + renameFileAndExtension);
//
string HDpath = Application.dataPath + renameDir;
string renameFilePath = HDpath + renameFileAndExtension;
string renameFilePathMeta = renameFilePath + ".meta";
string newFilePath = HDpath + newName + "";
string newFilePathMeta = newFilePath + ".meta";
/*
string renameFilePath = Application.dataPath + path;
string renameFilePathMeta = Application.dataPath + path + ".meta";
string newFilePath = Application.dataPath + newName + "";
string newFilePathMeta = Application.dataPath + newName + ".meta";
*/
Debug.Log("renameFilePath= " + renameFilePath);
Debug.Log("renameFilePathMeta= " + renameFilePathMeta);
Debug.Log("newFilePath= " + newFilePath);
Debug.Log("newFilePathMeta= " + newFilePathMeta);
if (System.IO.File.Exists(renameFilePathMeta))
{
Debug.Log("OK Exist renameFilePathMeta =" + renameFilePathMeta);
}
else
{
Debug.Log("NG Not Found renameFilePathMeta =" + renameFilePathMeta);
}
if (System.IO.File.Exists(renameFilePath))
{
Debug.Log("OK Exist renameFilePath =" + renameFilePath);
}
else
{
Debug.Log("NG Not Found renameFilePath =" + renameFilePath);
}
//you shuld first rename metafile on Unity
System.IO.File.Move(renameFilePathMeta, newFilePathMeta);
System.IO.File.Move(renameFilePath, newFilePath);
if (System.IO.File.Exists(newFilePathMeta))
{
Debug.Log("OK Exist newFilePathMeta =" + newFilePathMeta+" sucsess renamed!! OK");
}
else
{
Debug.Log("NG Not Found newFilePathMeta =" + newFilePathMeta + " rename faild NG");
}
if (System.IO.File.Exists(newFilePath))
{
Debug.Log("OK Exist newFilePath =" + newFilePath + " sucsess renamed!! OK");
}
else
{
Debug.Log("NG Not Found newFilePath =" + newFilePath + " rename faild NG");
}
AssetDatabase.Refresh();
}
static public void UnityAssetDatabaceRenameFileNameAndExtention(string path,string newName)
{
// Rename
string ret;
ret = AssetDatabase.RenameAsset(path, newName);
if (ret == "")
{
Debug.Log("Material asset renamed to newName="+ newName);
}
else
{
Debug.Log(ret);
}
AssetDatabase.Refresh();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.