public static Object InstantiatePrefab (Object assetComponentOrGameObject);
public static Object InstantiatePrefab (Object assetComponentOrGameObject, SceneManagement.Scene destinationScene);

参数

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

返回

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

描述

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

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

using UnityEngine;
using UnityEditor;

public class Example : EditorWindow { [MenuItem("Examples/Instantiate Selected")] static void CreatePrefab() { GameObject clone = PrefabUtility.InstantiatePrefab(Selection.activeObject as GameObject) as GameObject; }

[MenuItem("Examples/Instantiate Selected", true)]

static bool ValidateCreatePrefab() { GameObject go = Selection.activeObject as GameObject; if (go == null) return false;

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