We continue the cycle about patterns and talk about the Prototype, not the most popular one in Unity.
The purpose of the Prototype template is to create new objects by cloning an existing one. Usually, it happens by providing the interface to the cloned object itself. The main problem of this process is that cloning from the outside of the object is difficult due to the non-obvious mechanism of copying dependent objects or the lack of access to internal fields. But if you focus on the name “Prototype," it becomes evident that the use of prefabs to create new objects is nothing more than the use of this template, since Unity provides us with the ability to clone internal fields. =)
More theory can be found in this article or this chapter of Robert Nystrom’s book.
The basis of this template in .Net is the ICloneable
interface, which consists of a description of one method object Clone ();
.
As an example, let’s look at the simplified implementation of a Skeleton
inherited from Monster
class, which, in turn, is inherited from MonoBehaviour
. Now, having one skeleton, we can assemble our army of undead. =)
|
|
It is a tiny article to draw your attention to the relationship between the pattern and one of the main approaches to work with Unity. See you next time! =)