Is there a way to replace all the references to an asset to another asset?
For example if I have the path of asset A1, A2 … AN, B and C, while A1-N are all referencing to B. Is there a way, through script, to make all the references of B to C instead?
Would it work if I just do a ReadAllText search and replace the guids? Or are there any good ways to do so?
Hi there,
We don’t have any API or tooling in Unity that provide such functionality. But you can probably do it by hand with a good old search/replace (I know I’ve done it in the past on some projects…)
If the reference you are trying to change is a sub-asset or imported by another importer type. Say you want to change a reference to a Material in a .mat file to a reference to a Material created by a ModelImporter (from a fbx file and not extracted), then changing the guid will not be enough and may break references.
To avoid that, you should probably change one reference manually in the editor, save your project, and then look at the reference in your file and copy the fileID along with guid and replace both at the same time.
Basically what you want to find/replace is the whole “{fileID: 000000000, guid: xx, type: 2}” value and not just the guid (even if the fileID is the same, maybe there are references to other subassets in that same guid that you don’t want to be changed?)
Also, if any of your assets are serialized as binary in your project, then you can’t really edit it by hand (you can force serialize in text in your project settings, but I think there are some very specific assets/tools that force some files to be binary because they would be too big otherwise…)
As always, make sure you don’t do such changes in your project without a backup!