PrefabUtility.InstantiatePrefab

Switch to Manual
public static Object InstantiatePrefab (Object target);
public static Object InstantiatePrefab (Object target, SceneManagement.Scene destinationScene);

Parameters

target 要实例化的预制件资源。
destinationScene 要在其中实例化预制件的场景。

Returns

Object 位于预制件根部的游戏对象。

Description

将给定场景中的给定预制件实例化。

此函数类似于 Instantiate,但会创建与预制件的连接。 如果未给定场景句柄,则会在活动场景中实例化预制件。


@MenuItem("Examples/Instantiate Selected")
static function CreatePrefab() {
    var clone : GameObject = PrefabUtility.InstantiatePrefab(Selection.activeObject as GameObject) as GameObject;
}

@MenuItem("Examples/Instantiate Selected", true) static function ValidateCreatePrefab() { GameObject go = Selection.activeObject as GameObject; if (go == null) return false;

return PrefabUtility.GetPrefabType(go) == PrefabType.Prefab || PrefabUtility.GetPrefabType(go) == PrefabType.ModelPrefab; }