Prefabs

Prefabs

Sometimes we need in game development to use the same object with the same behaviour multiple times. Let's say we need to collect some apples. There are 3 apples on the map and these apples should have a specific movement. Instead of handling each apple separately, we can create a prefab of the apple and update it with all of its features. As a result, instead of doing the same task 3 times, we will do it only one time. Here you can read more about what prefabs are and when we should use them.

Some other examples in which we might need prefabs:

  • Environmental Assets - for example a certain type of tree used multiple times around a level (as seen in the screenshot above).

  • Non-player characters (NPCs) - for example a certain type of robot may appear in your game multiple times, across multiple levels. They may differ (using overrides ) in the speed they move, or the sound they make.

  • Projectiles - for example a pirate’s cannon might instantiate a cannonball Prefab each time it is fired.

  • The player’s main character - the player prefab might be placed at the starting point on each level (separate Scenes) of your game.

Creating Prefab Assets

To create a Prefab Asset, drag a GameObject from the Hierarchy window into the Project window. The GameObject 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.

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.

Prefab Creation

After creating a prefab, you can create instances of the Prefab Asset in the Editor by dragging the Prefab Asset from the Project view to the Hierarchy or to the Scene.
We can also add prefabs in our games using scripts. This is how we can do it:
        1. Create a new script and attach it to a game object (do NOT attach it to the prefab).
        2. Inside the script create a public GameObject variable.

public GameObject myPrefab;

        3. After you have declared the public GameObject in your script, from Unity, you will be able to drag and drop the prefab to the script.
Prefab Instantiation
        4. Create a new instance in scene using:
Instantiate(myPrefab, position, rotation)
This method creates a copy of the myPrefab prefab at the specified position and rotation.

This is how the script would look like:
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);
    }
}

You can read more about this from the official Unity documentation. And in order to use prefabs in your scripts, here are some explained examples.

Ultima modificare: Saturday, 24 July 2021, 18:07