用不同的子类型替换 Unity 对象上的脚本?

Replace script on Unity object with different subtype?

给定基数 class A 和继承的 class B。

如果预制件附加了类型 A 的脚本,有什么方法可以将脚本的类型更改为 B?

必须手动删除脚本并添加不同的类型将擦除脚本位置和所有自定义值。

作为一个快速的解决方法,我发现编写一个控制台应用程序来编辑 .prefab 文件相当容易,如果你有一堆预制件要更改的话,这也比使用编辑器快很多。

var pathToPrefabs = @"PATHTOFOLDER";
var searchPattern = "*.prefab";

var oldGuid = "f918e74538022b447823e4cf64700632"; //the old 'guid references in the prefab file'
var newGuid = "f597f3f375d88c645ad124f7cbcd1322"; //the new guid of the new type

foreach (var prefab in System.IO.Directory.GetFiles(pathToPrefabs, searchPattern, System.IO.SearchOption.AllDirectories))
{
    var text = System.IO.File.ReadAllText(prefab);
    text = text.Replace(oldGuid, newGuid);
    System.IO.File.WriteAllText(prefab, text);
}

要获得正确的 guid,只需在一个预制件上手动进行更改,然后查看 git 代码更改。

辛苦了。

注意:这仅在您将资产序列化为文本时才有效(如果您使用的是源代码管理,则应该这样做)。

我使用 Rider 在文件中查找(替换选项),它用旧的 GUID 找到了所有元数据、预制件和场景信息,并将其替换为新的 GUID,它非常有效