•  
  • Helping you upgrade your CG career

    What is a prefab in Unity and how to use a prefab

    0
    votes

    In Unity, a prefab is a type of asset. If you know the difference between a copy and an instance in DCC tools like Maya or 3dsMax, you will have a better understanding of prefabs. You can think of a pre-fab as an instance. But there is much more to it than the instances we are used to in Maya or 3dsMax.

    A Prefab in Unity can be considered as a collection of one or more than one GameObjects, along with their own properties, scripts, materials, textures, etc.

    How to create a Prefab : The first thing you can do is to create an empty prefab by clicking on

    Assets->Create->Prefab as shown in the below screen-capture :Unity Create Prefab

    That will create an empty prefab for you. Next, you can drag and drop one or more Game Objects into your prefab. Once you create a prefab, you no longer need to keep the Gameobject(s) in the Heirarchy. You may delete it. The prefab acts like a container for your GameObject(s).

    If you choose to create a prefab of a tree for example, you can drag the tree-prefab into your scene multiple times. Say you had a 100 tree-prefabs scattered around your scene, and then you decide to change any attributes/properties of the trees (other than position / rotation) you can either change the properties of all the 100 trees in one go, or you may decide to change the property for one singe tree. When you select a prefab that has been instanciated in the scene, you will find 2 important buttons in the Inspector :

    1. Apply &
    2. Revert

    Clicking on Apply will change all the related instances of that particular prefab. Clicking on Revert will discard all changes that have been made since the creation of the prefab.‚Äč Prefabs also give you the option to change one (or more) individual property/properties of an one asset in your scene, while retaining/inheriting all other properties from the prefab. For example, if we take the example of the 100 tree-prefabs in your scene, you can change the scale of one tree so that it is double the size of the original prefab, but still retain all other properties of the other 99 trees in the scene. Another example would be of 10 lights in your scene which have the same intensity and color, but then you wish to change only the color of one of the lights. If this light is a component of a prefab, then its intensity can be controlled collectively with the other 9 lights, but it still maintains its own color.

    Play around with prefabs - they are one of the fundamental theories of Unity !

    written 2 Years ago by
    Avatar image of CG Helpline user admin

    Submit your Feedback :




    SUBMIT

    Page rendered in 0.1219 seconds. Memory usage : 3.93MB.