Prefab Asset: Difference between revisions

From Pummel Party Mod Documentation
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
Line 1: Line 1:
A prefab is a collection of gameobjects formed into a sub-hierarchy that can be re-used. Changing a prefab will cause all instances of that prefab to be updated.
A prefab is a collection of gameobjects formed into a sub-hierarchy that can be reused. Changing a prefab will cause all instances of that prefab to be updated.


Prefabs can be viewed in the [[Asset Browser]] in the ''Custom > Prefabs'' folder.
Prefabs can be viewed in the [[Asset Browser]] in the ''Custom > Prefabs'' folder.
Line 5: Line 5:
==Creating a prefab==
==Creating a prefab==
===From the hierarchy ===
===From the hierarchy ===
You can turn a group of gameobjects in the hierarchy into a prefab. This group must have a single parent gameobject that you can select; you can't multiselect gameobjects to turn into a prefab.
You can turn a group of gameobjects in the hierarchy into a prefab. This group must have a single parent gameobject that you can select; you can't multi-select gameobjects to turn into a prefab.
To create the prefab, select the parent gameobject and drag it from the [[Hierarchy Window]] into the [[Asset Browser]]. This will create a prefab containing the dragged gameobject sub-hierarchy.
To create the prefab, select the parent gameobject and drag it from the [[Hierarchy Window]] into the [[Asset Browser]]. This will create a prefab containing the dragged gameobject sub-hierarchy.


===Directly from the Asset Browser===
===Directly from the Asset Browser===
In the asset browser right click and choose ''Create > Prefab''. This will create an empty prefab that you can add gameobjects and components to.
In the asset browser right click and choose ''Create > Prefab''. This will create an empty prefab that you can populate with gameobjects and components.


==Editing a prefab==
==Editing a prefab==
To edit a prefab, find it in the hierarchy and double click it.
To edit a prefab, find it in the hierarchy and double-click it.
You can also select it in the hierarchy or scene view so that it is visible in the [[Inspector Window]]. Here you can click the ''Edit Prefab'' button on the [[Prefab Instance Component]].
You can also select it in the hierarchy or scene view so that it is visible in the [[Inspector Window]]. Here you can click the ''Edit Prefab'' button on the [[Prefab Instance Component]].


Alternatively you can locate it in the [[Asset Browser]] and double click it.
Alternatively, you can locate it in the [[Asset Browser]] and double-click it.


==Instantiating prefabs==
==Instantiating prefabs==
Line 23: Line 23:
==Use cases==
==Use cases==
===Re-using groups of gameobjects===
===Re-using groups of gameobjects===
This is the primary usage of prefabs. If you build a house of out many gameobjects (walls, floors, doors, windows, decorations etc) and turn it into a prefab, you can then create many instances of that prefab through out the scene. If you then realize you forgot to add a back door to the house, you can update the prefab and all houses you've placed will update. If you hadn't used a prefab ,then you'd need to manually add a door to every house you'd placed.
This is the primary usage of prefabs. If you build a house of many gameobjects (walls, floors, doors, windows, decorations etc) and turn it into a prefab, you can then create many instances of that prefab throughout the scene. If you then realize you forgot to add a back door to the house, you can update the prefab and all houses you've placed will update. If you hadn't used a prefab, then you'd need to manually add a door to every house you'd placed.


===Usage by other components===
===Usage by other components===

Latest revision as of 02:28, 16 December 2022

A prefab is a collection of gameobjects formed into a sub-hierarchy that can be reused. Changing a prefab will cause all instances of that prefab to be updated.

Prefabs can be viewed in the Asset Browser in the Custom > Prefabs folder.

Creating a prefab

From the hierarchy

You can turn a group of gameobjects in the hierarchy into a prefab. This group must have a single parent gameobject that you can select; you can't multi-select gameobjects to turn into a prefab. To create the prefab, select the parent gameobject and drag it from the Hierarchy Window into the Asset Browser. This will create a prefab containing the dragged gameobject sub-hierarchy.

Directly from the Asset Browser

In the asset browser right click and choose Create > Prefab. This will create an empty prefab that you can populate with gameobjects and components.

Editing a prefab

To edit a prefab, find it in the hierarchy and double-click it. You can also select it in the hierarchy or scene view so that it is visible in the Inspector Window. Here you can click the Edit Prefab button on the Prefab Instance Component.

Alternatively, you can locate it in the Asset Browser and double-click it.

Instantiating prefabs

Prefabs can be instantiated in the scene by creating a Prefab Instance and setting the Prefab field to the prefab you'd like to use. This will place that prefab into the scene.

Use cases

Re-using groups of gameobjects

This is the primary usage of prefabs. If you build a house of many gameobjects (walls, floors, doors, windows, decorations etc) and turn it into a prefab, you can then create many instances of that prefab throughout the scene. If you then realize you forgot to add a back door to the house, you can update the prefab and all houses you've placed will update. If you hadn't used a prefab, then you'd need to manually add a door to every house you'd placed.

Usage by other components

Some components such as the Item Component and Spawner Component require a prefab to be passed to them. The item uses this prefab as the visual object that the player can hold when using the item, and the spawner needs a prefab of objects for it to spawn.