EditorUtility.InstantiatePrefab Manual     Reference     Scripting  
Scripting > Editor Classes > EditorUtility
EditorUtility.InstantiatePrefab

static function InstantiatePrefab (target : Object) : Object

Description

Instantiates the given prefab.

This is similar to Instantiate but creates a prefab connection to the prefab.

// Creates a prefab from the selected GameObjects.
// if the prefab already exists it asks if you want to replace it

class CreatePrefabFromSelected extends ScriptableObject {

@MenuItem("Examples/Create Prefab From Selected")
static function CreatePrefab() {
var objs = Selection.gameObjects;

for (var go : GameObject in objs) {
var localPath : String = "Assets/" + go.name + ".prefab";
if (AssetDatabase.LoadAssetAtPath(localPath, GameObject)) {
if (EditorUtility.DisplayDialog("Are you sure?",
"The prefab already exists. Do you want to overwrite it?",
"Yes",
"No"))
createNew(go, localPath);
} else {
createNew(go, localPath);
}

}
}
@MenuItem("Examples/Create Prefab From Selected", true)
static function ValidateCreatePrefab() {
return Selection.activeGameObject != null;
}

static function createNew(obj : GameObject, localPath : String) {
var prefab : Object = EditorUtility.CreateEmptyPrefab(localPath);
EditorUtility.ReplacePrefab(obj, prefab);
AssetDatabase.Refresh();

DestroyImmediate(obj);
var clone : GameObject = EditorUtility.InstantiatePrefab(prefab) as GameObject;
}
}