top of page

Flyweight Introduction-001

Project Files for the course - Download

1.1 Introduction

1. In this tutorial we are going to about Flyweight Design Pattern in Unity.

2. Flyweight design pattern is a structural design pattern .

3. Flyweight pattern is used to optimize the space that is required to run by sharing data among instances.

4. In this tutorial we will learn how to implement flyweight pattern using scriptable objects in Unity.

5. Open the Introduction001 Folder in FlyweightPattern Folder and open the scene.

1.2 Lots of Cars!!

1. As soon as we open the scene we see 100's of cars in the scene

image.png

2. They probably are taking a lot of space.

3. Now let us look at the car script attached to each car.

FlyweightPattern.png
FlyweightPattern2.png

1.3 Problem

1. From what we saw above we can see that a lot of space is occupied by 

a. By Car Model (can't change that part)

b. By Car Script-Having lots of fields.

Coupled with the fact that there are 100's of cars. So lots of space is getting occupied

Now that we have figured the problem that we need to save space. We are going to fix the problem

​

1.4 Solution

1. So now that we have identified the problem that there are lots of references to properties.

2. So to fix the problem we need to reduce the number of references

3. So between all the car scripts we have two types of data

   a. Current Car Data- Current Speed, Current Health

   b. Common Car Data- Maximum Speed, Car Color, Engine sound etc.

4. The common car data will be common among all the cars and only the current car data will remain the same

5. To fix the problem we will club all the common data into a single scriptable object file and have the car script grab reference to it. This way we are going to save lots of space by avoiding duplication.

image.png
image.png
image.png
image.png

6. Let us now make CarData scriptable object that is going to hold all the common properties.

7. In the CarData Scriptable we will copy all the common properties and remove all the common properties from the original Car class. In the car Class now add the reference to CarData Scriptable Object.

​

CarData 

image.png
image.png
image.png

Car

image.png

1.5 Inspector

1. Let us now take a look at the car inspector before and earlier.

​

image.png
image.png

2. We have significantly reduced the number of references from before.

1.6 Making CarData

1. Let us now create CarData scriptable object and assign it as reference to all the cars

2. Now all the cars are referencing to only 1 single CarData object allowing us to save massive amounts of space that was earlier used in reference.

1.7 Understanding Flyweight pattern from above

1. Flyweight pattern basically means that if the data is common among lots of instances of a class then instead of duplicating make a reference to the common data.

2. In the above example we seperated common data from Car and made a reference to CarData thus saving space.

3. Instead of referencing to lots of properties we are only referencing the CarData property.

bottom of page