Version: 2018.3 (switch to 2019.1)
LanguageEnglish
  • C#

PrefabUtility.InstantiatePrefab

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

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

Parameters

assetComponentOrGameObjectPrefab Asset to instantiate.
destinationSceneScene to instantiate the Prefab in.
parentThe parent Transform to be assigned.

Returns

Object The GameObject at the root of the Prefab.

Description

Instantiates the given Prefab in a given Scene.

This is similar to Instantiate but creates a Prefab connection to the Prefab. If a Scene handle is not given the Prefab is instantiated in the active Scene.

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; } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答