Version: 2020.1
Prefabs
Editing a Prefab in Prefab Mode

Creating Prefabs

In Unity’s PrefabAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary
system, Prefab Assets act as templates. You create Prefab Assets in the Editor, and they are saved as an Asset in the Project windowA window that shows the contents of your Assets folder (Project tab) More info
See in Glossary
. From Prefab Assets, you can create any number of Prefab instances. Prefab instances can either be created in the editor and saved as part of your ScenesA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary
, or instantiated at runtime.

Creating Prefab Assets

To create a Prefab Asset, drag a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
from the Hierarchy window into the Project window. The GameObject, and all its components and child GameObjects, becomes a new Asset in your Project window. Prefabs Assets in the Project window are shown with a thumbnail view of the GameObject, or the blue cube Prefab icon, depending on how you have set up your Project window.

Two prefabs (FatBlob and “Key”) shown in the Project window in two-column view (left) and one-column view (right)
Two prefabs (“FatBlob” and “Key”) shown in the Project window in two-column view (left) and one-column view (right)

This process of creating the Prefab Asset also turns the original GameObject into a Prefab instance. It is now an instance of the newly created Prefab Asset. Prefab instances are shown in the Hierarchy in blue text, and the root GameObject of the Prefab is shown with the blue cube Prefab icon, instead of the red, green and blue GameObject icon.

A Prefab instance (Key) in the scene
A Prefab instance (Key) in the scene

Creating Prefab instances

You can create instances of the Prefab Asset in the Editor by dragging the Prefab Asset from the Project view to the Hierarchy or Scene viewAn interactive view into the world you are creating. You use the Scene View to select and position scenery, characters, cameras, lights, and all other types of Game Object. More info
See in Glossary
.

Dragging a Prefab Key into the Scene
Dragging a Prefab “Key” into the Scene

You can also create instances of Prefabs at runtime using scripting. For more information, see Instantiating Prefabs.

Replacing existing prefabs

You can replace a Prefab by dragging a new GameObject from the Hierarchy window and dropping it on top of an existing Prefab asset in the Project window.

If you are replacing an existing Prefab, Unity tries to preserve references to the Prefab itself and the individual parts of the Prefab such as child GameObjects and Components. To do this, it matches the names of GameObjects between the new Prefab and the existing Prefab that you are replacing.

Note: Because this matching is done by name only, if there are multiple GameObjects with the same name in the Prefab’s hierarchy, it is not possible to predict which will be matched. Therefore if you need to ensure your references are preserved when saving over an existing prefab, you must ensure all GameObjects within the Prefab have unique names.

Also note: You may encounter a similar problem in the case of preserving references to existing Components when you save over an existing Prefab, if a single GameObject within the Prefab has more than one of the same Component type attached. In this case it is not possible to predict which of them will be matched to the existing references.


  • 2018–07–31 Page published

  • Nested Prefabs and Prefab Variants added in 2018.3

Prefabs
Editing a Prefab in Prefab Mode
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961