Batch replace gameobjects in Unity

When creating Harry the Fairy, we often found ourselves having to replace hundreds of prefab instances so I created this little script. It replaces a number of gameobjects with another and gives you the possibility to save all the transform values i.e. position, rotation and scale.

C# Script must be placed in "editor" folder.


using UnityEngine;
using UnityEditor;
using System.Collections;

public class ReplaceGameObjects : ScriptableWizard
{
public bool copyValues = true;
public GameObject NewType;
public GameObject[] OldObjects;

[MenuItem("Custom/Replace GameObjects")]


static void CreateWizard()
{
ScriptableWizard.DisplayWizard("Replace GameObjects", typeof(ReplaceGameObjects), "Replace");
}

void OnWizardCreate()
{
foreach (GameObject go in OldObjects)
{
GameObject newObject;
newObject = (GameObject)EditorUtility.InstantiatePrefab(NewType);
newObject.transform.position = go.transform.position;
newObject.transform.rotation = go.transform.rotation;
newObject.transform.parent = go.transform.parent;

DestroyImmediate(go);

}

}
}

Here's a visual walkthrough:

Go into Custom ->Replace Gameobjects


Select any number of gameobjects and drag them into "Old Objects".
Then, from the project pane, drag the gameobject that you want to replace with into "New Type".


Press "Replace"


Enjoy :)

/Kristian
And credits to Michael L. Croswell

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 komentar:

Posting Komentar