Here’s a modified script that I use to replace prefabs in scenes by name. It works base don selection within the editor, so just select all your objects and and hit CMD+R. It will look through your scene and find prefabs that match the names of the objects in your scene. you can modify the code to create substitutions.
Put this in your Editor folder (requires Unity Pro)
If anyone has any idea how to make this script more flexible please let me know.
@MenuItem ("Tools/Replace with Prefab %r")
static function Replace() {
var transforms = Selection.transforms;
for (var i : int = 0; i < transforms.Length; i++) {
var localPath : String;
var newObject : GameObject;
var objectName : String = transforms*.gameObject.name;*
_ if(transforms*.gameObject.name == “Heart”)_
objectName = “C_Gem”;
_ else if (transforms.gameObject.name == “Star”)_
objectName = “C_Coin”;
else if (transforms.gameObject.name == “Golden_Banana”)
objectName = “C_Banana”;
else if (transforms.gameObject.name == “Banana_Bunch”)
objectName = “C_Boost”;
else if (transforms.gameObject.name == “Propeller_Hat”)
objectName = “C_Hat”;
_ else if (transforms.gameObject.name == “Wings”)_
objectName = “C_Wings”;
else if (transforms.gameObject.name == “P_Steel”)
objectName = “P_Pipe”;
_ //Search through each directory in the prefabs folder for the right object*
* localPath = “Assets/Prefabs/” + objectName + “.prefab”;
if(AssetDatabase.LoadAssetAtPath(localPath, Object))
newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*_
* localPath = “Assets/Prefabs/Backgrounds/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/Cameras/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/Characters/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/Collectibles/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/Effects/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/Menus/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/Obstacles/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/Platforms/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/Story/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/TriggerVolumes/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* localPath = “Assets/Prefabs/Tutorial/” + objectName + “.prefab”;*
* if(AssetDatabase.LoadAssetAtPath(localPath, Object))*
* newObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;*
* //Replace the current object with the prefab (sets position, rotation and scale)*
* if(newObject){*
_ newObject.transform.position = transforms*.position;
newObject.transform.localRotation = transforms.localRotation;
newObject.transform.localScale = transforms.localScale;
DestroyImmediate(transforms.gameObject);*_
* }else{*
_ Debug.Log(transforms*.gameObject.name + " not found in Prefab Library");*_
* }*
* }*
}
This doesn't require Unity pro. No pro feature has been used in this editor script. CMD+R is the hotkey defined in the MenuItem (
– Bunny83%r)%means CMD(Mac) or CRTL(Win). You can use any hotkey you like as long as it isn't used for something else. See <a href="http://unity3d.com/support/documentation/ScriptReference/MenuItem.html">MenuItem</a> for more information. Anyway this script isn't very robust. If there are two prefabs with the same name but in different folders it will instantiate both but only use the last one.It doesn't matter if the models are prefabs or not, the script just replaces the objects by name. I used it to replace broken prefabs and turn them back into prefabs. Bunny is right that the script is kinda crap but it gets the job done. I didn't run into the problem he's pointed out with the multiple instances but not that he mentions it, it's kind of obvious that would happen :S
– anon69280468