Prefabs
- Creating a prefab file
- The prefab object
- Prefab user properties
- Creating a prefab user property
- Simple property type
- Option property type
- Color property type
- Expression property type
- Object Variable property type
- Object Constructor property type
- Texture Config property type
- Asset Key property type
- Animation Key property type
- Audio Key property type
- Scene Key property type
- Initializing other properties
- User properties in a prefab instance
- Creating a prefab instance
- Prefab code generation
- Changing a property of a prefab instance
- Prefab variants
- Nested prefabs
- Using a prefab as a black box
A prefab is a key concept in the Scene Editor. It allows for creating reusable, customized objects. It borrows some concepts from the Unity Engine prefabs.
In a game level, there are many objects with a common behavior and appearance. For example, several enemies look the same and act the same. Or there are the “coins” that have the same animation and play the same sound when are taken. Or the player object is the same in all the levels. All these objects are perfect candidates to be created as prefab instances.
You can see a prefab as the template of multiple objects. Or you can see a prefab as a class, so you can create multiple instances of that class and place them in a scene.
In the end, a prefab is just a class. If you look into the code generated by the scene compiler, a prefab scene is compiled into a class that extends a Phaser built-in type or another prefab class. And the prefab instances are just instances of the generated class.
At the same time, you can customize prefab instances by changing the value of its properties. If a property of the prefab (the class) is modified, this modification is applied to all the prefab instances, if it is not overriding in the prefab instance.
A very precise way to know how prefabs work, is to look into the code generated by the scene compiler. You will realize that it is just about Object Oriented Programming concepts like class, inheritance, properties, composition, and instances.