-
Notifications
You must be signed in to change notification settings - Fork 0
Flyweight pattern & Scriptable Objects
This pattern focuses on separting the dynamic data from the static data in order to avoid reusing this last type of data in RAM over an over.
For example, you have an enemy, specifically an orc. The orc has a currentLife, a maxLife and an attackSpeed. The currentLife is a dynamic atrribute because it changes trough time. However, the maxLife and the attackSpeed are STATIC attributes because they don't change in the entire gameplay.
In the case of unity, we are going to store that static data on Scriptable Objects.
Why Scriptable Objects you may ask? Well there are 3 solutions static data separation problem.
-
Const attributes
This attributes will save memory compared to a simple attribute. Nonetheless, they are hard coded and not "easy" to change. Also they can't be referenced by other GameObjects.
-
Static attributes
This attributes are better than the const attributes. They save memory but they also can be referenced by GO and also can be changed in the unity inspector. Nonetheless they are hard to deal on the change of scenes.
-
Scriptable Objects
The best solution to save memory with static attributes is the Scriptable Objects. They are stored in the project and not in the scene so they will not have problems with the change of scenes. Scriptable Objects should only be used to stored data
Example of Scriptable Object:

Home
C#
Game Design Patterns
- Command
- Flyweight pattern & Scriptable Objects
- Observer pattern
- State pattern
- Object Pool pattern
- Factory pattern
ECS
PlayFab
- Introduction
- Template Class
- Player Authentication
- Mobile Authentication
- Player Statistics
- Leaderboard
- Player Data
- Friends
Mirror