Version: 2020.2

PrefabUtility.InstantiatePrefab

切换到手册
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 { [MenuItem("Examples/Instantiate Selected")] static void InstantiatePrefab() { Selection.activeObject = PrefabUtility.InstantiatePrefab(Selection.activeObject as GameObject); }

[MenuItem("Examples/Instantiate Selected", true)] static bool ValidateInstantiatePrefab() { GameObject go = Selection.activeObject as GameObject; if (go == null) return false;

return PrefabUtility.IsPartOfPrefabAsset(go); } }