Game Design Patterns: Flyweight
<p>Using the <em>flyweight </em>pattern is a great way to optimize performance in your game, especially concerning large volumes of prefabs <em>(reusable objects)</em> and script <em>instances</em>. When using <em>prefabs</em>, you can save performance by reusing a <em>copy </em>of that object. While each <em>instance </em>of that object has a <em>shared </em>material, and uses the same script, it still creates a <em>unique instance</em> of that script. When working with thousands, or tens of thousands of reusable objects with shared variables, the flyweight pattern is a good solution for <em>coupling </em>the shared properties that will remain the <em>same </em>between all instances.</p>
<p>Let’s use an <em>Enemy</em> script as an example, assuming you may have 20,000 enemies in your scene at once. They all have a <em>max</em>, <em>min </em>and <em>current </em>health variable, a <em>speed </em>variable, a handle to the player’s <em>transform </em>component, and a <em>wait time</em> for a coroutine.</p>
<p><a href="https://levelup.gitconnected.com/game-design-patterns-flyweight-b087cabb457"><strong>Learn More</strong></a></p>