批量替换unity中已存在的脚本,比如重命名,或者替换重复的脚本;
[MenuItem("Assets/empty4替换")]
public static void ScriptGuidChange()
{
StringBuilder sb = new StringBuilder();
string oldGuid = "a93801baeef446cdb7503c9f0a1ecb7d";
string newGuid = "179e7073a9e23b8439f037e9ca8f4323";
string[] guids = AssetDatabase.FindAssets("t:prefab");
foreach (var guid in guids)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
if (path.StartsWith("Assets"))
{
var file = File.OpenText(path);
StringBuilder newStr = new StringBuilder();
bool isNew = false;
while (true)
{
var str = file.ReadLine();
if (string.IsNullOrEmpty(str))
{
break;
}
else
{
if (str.Contains(oldGuid))
{
str = str.Replace(oldGuid, newGuid);
sb.AppendLine(path + ":" + str);
isNew = true;
}
}
newStr.AppendLine(str);
}
file.Close();
file.Dispose();
if (isNew)
{
File.WriteAllText(path, newStr.ToString());
}
}
}
Debug.LogError(sb.ToString());
}