unity.hfbk.net

03. Prefabs & Assets

A Prefab is a bit like a prototype/template of a GameObject, which includes all Components, Materials, Animations and Settings. To create a Prefab from an existing GameObject just drag the object from the Hierarchy Window into the Project/Assets Window.

Whenever you want to reuse a single object all over your Scene, it makes sense to turn it into a Prefab, as you can then edit all Instances of the Prefab in one place.

You can additionally:

Editing Prefabs

You can edit a Prefab by entering Prefab Mode via:

You can then exit the Prefab Mode via:

Instance Overrides

If you create a Prefab and place it multiple times within your scene, all the settings in every single Instance you placed will be identical – sometimes however you want to adjust single instances a bit to get some variation – this is where Instance Overrides come into play.

Here is what Instance Overrides are allowed to actually override:

Ok? What
reparent a part of the Prefab
remove GameObjects that are part of the Prefab (you could deactivate them however)
Changing the value of a property
Adding a component (e.g. a Script)
Removing a component
Adding a child GameObject

Changed Properties will be marked blue and written in bold in the Inspector:

Note: If you want to apply the changes made in an Override to all other Prefabs, you can select Apply from the Overrides Dropdown menu in the top right of the Inspector window. If you want to get rid of the changes select Revert.

Nested Prefab

Prefabs can contain other Prefabs. This is called nesting Prefabs. If you put Prefabs inside a Prefab, the Prefabs will retain their links to their own assets. This is useful if you e.g. have a Prefab of a table and you want to create a reusable Prefab with table, chairs, a vase and so on – this way you could quickly place the whole Table with chairs, vase etc multiple times within your scene without having to manually moving around each chair or vase. If you still would like to make changes to individual table-sets you can of course use Instance Overrides

To create a nested Prefab:

  1. Go into Prefab Mode (doubleclick it in the Project or Hieararchy window)
  2. Drag in any Prefab like you'd do normally

Prefab Variants

If creating Instance Overrides is getting old, you can also use Prefab Variants.

To create a Prefab Variant, simply:

  1. Rightclick a Prefab in the Project view and select Create > Prefab Variant
  2. Open Prefab Mode for the newly created Variant and modify it with overrides

Unpacking Prefab instances

If you want to convert your Prefab Instance into a regular GameObject, you can unpack it. This is basically the reverse of creating a Prefab and bakes everything you did with that single instace into a Gameobject. After you unpacked a Prefab any modification you might make in the Prefab no longer affect the unpacked Gameobject.

You can unpack a prefab in the Hierarchy view using: Rightclick on the Prefab Instance and then selecting Unpack Prefab Completely

Instantiating Prefabs at runtime

The template-nature of prefabs makes them incredibly useful for anything that needs to create things at run time. This could mean creating Super-Mario-like monsters or bullets, or basically anything that you want to create much of.

Using Prefabs for this is useful because:

This is commonly used to:

To create a prefab at runtime your code needs to reference the Prefab in code. This reference will show up in the Inspector as an assignable field, so you can quickly change what Prefab you want to use.

A code that creates a new instance of your selected Prefab at a given position:

using UnityEngine;
public class InstantiationExample : MonoBehaviour 
{
    // Reference to the Prefab. Drag a Prefab into this field in the Inspector.
    public GameObject myPrefab;

    // This script will simply instantiate the Prefab when the game starts.
    void Start()
    {
        // Instantiate at position (0, 0, 0) and zero rotation.
        Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
    }
}

If you want to create Prefabs on the fly using a Trigger Volume (see BindToTrigger-Script) or via Interaction (see Interactable-Script), you might have to change:

void Start()

into

public void CreateMyObject()

and select that method in the BindToTrigger and Interactable Components you placed onto your objects. So instead of creating a new Instance of the Prefab on Start, you can this way manually trigger when a new Instance should be created.

If you e.g. want to create a new Prefab every 1 second while someone is within a Trigger-Volume, you could use something like this (untested, but should work):

using UnityEngine;

public class CreateRepeatedly : MonoBehaviour 
{
    // Reference to the Prefab. Drag a Prefab into this field in the Inspector.
    public GameObject myPrefab;

   public float delay = 0.0f;
   public float interval = 1.0f;

    void OnTriggerEnter() {
        // Run the CreateMyObject function repeadly every n seconds (where n is the value stored in interval)
        // start doing this after a delay of m seconds (where m is  the value stored in delay)
        InvokeRepeating("CreateMyObject", delay, interval);
    }

    void OnTriggerExit() {
        // Stop repeating when the trigger is beeing exited
        CancelInvoke("CreateMyObject");
    }

    public void CreateMyObject() {
        // Create a object at this fixed position
        Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
    }
}

For a more detailed overview have a look at the unity documentation