Skip to content

Commit 46b6d51

Browse files
committed
Updated readme
1 parent 73d3b6a commit 46b6d51

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Diff for: _text/command.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ You have a base class called Command which has a method that a child can impleme
3131
- **Memento.** With this pattern you can also return to a previous state.
3232

3333

34-
## [Back](https://github.com/Habrador/Unity-Programming-Patterns)
34+
## [Back](../)

Diff for: _text/flyweight.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 2. Flyweight
2+
3+
Even though a single object uses little memory – instantiating many of them can cause trouble, so you need to make the objects lighter by sharing code.
4+
5+
**How to implement?**
6+
7+
Separate the data that’s not specific to a single instance of that object and can be shared across all of them. You can do that by creating a new class and put the shared data in it. Then each object that should share data gets a reference to a single instance of that "storage" class.
8+
9+
**When is it useful?**
10+
11+
- If you make Minecraft and have a million cubes in the scene. All cubes can share the same texture if you put all textures that belongs to each cube type (grass, stone, sand, etc) into a [texture atlas](https://en.wikipedia.org/wiki/Texture_atlas).
12+
13+
- If you make a strategy game, all infantry units share the same mesh, texture, maxHealth settings, etc. You only need to create one object with this data and then all infantry units can share that object. Each individual infantry unit only need to keep track of its own position and health.
14+
15+
- This is implemented in Unity as [sharedMesh](https://docs.unity3d.com/ScriptReference/MeshFilter-sharedMesh.html) and [sharedMaterial](https://docs.unity3d.com/ScriptReference/Renderer-sharedMaterial.html). If you make a change to a sharedMesh then all objects using that mesh will get a new mesh.
16+
17+
**Related patterns**
18+
19+
- **[Type Object](#12-type-object).** The main difference is that in Type Object you don't need to have the exact same data and you can also have behavior.
20+
21+
22+
## [Back](https://github.com/Habrador/Unity-Programming-Patterns)

0 commit comments

Comments
 (0)