Prefabs
Last updated
Last updated
Prefab (Pre Defined GameObject) that is an ordinary game object with all its components and properties configured in them, stored on the hard disk as an asset. In fact, this is a template that we can reuse in scenes.
If you want to place a background for your game (static objects) or, for example, to put lights on your map, it is again better to do this through prefabs than using a regular copy paste. Since, perhaps, during the development process, you will rework the texture of the lantern or you will want the lanterns to shine at a certain time of the day. At this point, you will understand the full power and benefits of prefabs – a couple of minutes will be enough for you to change one prefab and the changes will be applied to all its copies. If you used a copy paste, you will have to configure each game object separately, which, as you understand, is long and inefficient.
However, this does not mean all Prefab instances have to be identical. You can override settings on individual prefab instances if you want some instances of a Prefab to differ from others.
You should also use prefabs if some game object should appear during the execution of the game using a script.
To create a Prefab Asset, drag a GameObject from the Explorer into the Browser. The GameObject, and all its components and, becomes a new Asset in your Browser.
Process of creating the Prefab Asset also turns the original GameObject into a Prefab instance and saving it as prefab in asset, but original object stay the same, i.e not a prefab. Prefab instances are shown in the Inspector with blue array, and the root GameObject of the Prefab is shown with the blue cube Prefab icon.
You can also create a prefab by using the object's context window. Click RMB on the object and select Create -> New Prefab.
After you have created a prefab, you will need to drag it into the Explorer window and then it will be created not as a regular object but as a prefab and will be displayed with a different icon.
You can also create instances of Prefabs at runtime using scripting. For more information, see Prefabs Using Scripts.