


Maybe it is an enemy or perhaps even a tree. / It destroys itself after provided timeout.Oftentimes while creating games you find yourself needing to reuse objects in multiple places. Public class TimedObjectFactory : GenericFactory / Factory for prefabs of TimeoutObjects Now we need a factory and self-destruction script on our prefab! using UnityEngine

Var targetCount = Time.time * (spawnRatePerMinute / 60) Public class PrefabTimedSpawner : MonoBehaviour

/ Component uses factory to spawn new instance every few moments Our spawner will look like this: using UnityEngine In this example, we will create spawner which will spawn and place self-destroying prefab on the scene. Now you see how easy it is to make new factories? Great! So the last thing will be to create a project to use our newly created factory! Public class AudioFactory : GenericFactory Public class TrailFactory : GenericFactory Public class GenericFactory : MonoBehaviour where T : MonoBehaviourĪ lot has changed, isn’t it? Now we can make how many factories of a different kind as we want! 😈 using UnityEngine / Factory design pattern with generic twist! So our goal will be to make our existing Factory script to be a little bit more universal, and for that matter, we are going to use generics! using UnityEngine You heard me right! We are going to implement DRY KISS Factory! 🏜īut we are little evil geniuses here, and we can combine the power of both of them to create DRY KISS!!! 🦂🌵🏜 DRY KISS Factory?!?!?!? So besides KISS, there is also DRY (don’t repeat yourself) way to write code. So let’s take a look at KISS factory script: using UnityEngine However, I believe that KISS (keep it simple, stupid) is the only proper way to write code! 🤓 And this exactly what it is all about! ImplementationĪs mentioned already this design pattern is often used without even realizing it, but I started separating it, and you don’t have to follow me on that. So what exactly is this whole factory thing? Factory is a design pattern used for creating new instances or copies of the predetermined object.Ī great example of it can be an instantiation of bullets or enemies from prefabs.
