ogdc 2014: architecting games in unity
DESCRIPTION
TRANSCRIPT
![Page 1: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/1.jpg)
![Page 2: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/2.jpg)
Architecting Games in Unity
Common patterns for developing games in Unity
![Page 3: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/3.jpg)
If this was your house, what would you do?
![Page 4: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/4.jpg)
Broken Windows
• Fix the windows.• In New York City, Albuquerque and The Netherlands, improving the
physical appearance of a neighbourhood has been shown to reduce crime and increase quality of life.
• People are influenced by their environment:• Social norms and conformity• Routine monitoring
![Page 5: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/5.jpg)
Ah, Freedom!
• Unity gives you a rich set of building blocks that can be used toconstruct any type of game.
• Unity is flexible.• There are hundreds of ways to build projects.• And developers use ALL of them!
![Page 6: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/6.jpg)
Architecture for Games
• But let's look at what you need• And let's create a structure that can be reused for efficiency and
robustness
![Page 7: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/7.jpg)
Good Architecture = Good Standards
• Use C#• Naming conventions
• Use descriptive names• Use standardized capitalization• In Unity don’t be afraid of using spaces in names
• Logical folder structure• Zero-tolerance for warnings and errors• Zero-tolerance for runtime memory allocation
![Page 8: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/8.jpg)
Core Application Logic
• Main Controller• Base controller to manage high level application• Uses public static methods so everyone can access it• Uses Object.DontDestroyOnLoad so it is available throughout the project
• Scenes• Other scenes are loaded on top of this.• MainController loads and unloads other scenes and cleans up.
![Page 9: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/9.jpg)
Main Controller – Reset State
• Call GC.Collect to try to reclaim some memory.
![Page 10: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/10.jpg)
Main Controller – Preload State
• Start loading the level asynchronously and change state to Load
![Page 11: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/11.jpg)
Main Controller – Load State
• Keep going until loading is done
![Page 12: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/12.jpg)
Main Controller – Unload State
• Resources.UnloadUnusedAssets()• Keep going until Unloading is done
![Page 13: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/13.jpg)
Main Controller – Postload State
• Do things immediately after loading• Update currentSceneName
![Page 14: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/14.jpg)
Main Controller – Ready State
• Call GC.Collect to try to reclaim some memory.• Avoid this if you have unused assets in the scene that may be used later.
• Do things just before beginning: e.g.: Get User Input to Start
![Page 15: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/15.jpg)
Main Controller – Run State
• We stay here until currentLevelName != nextLevelName• This can be changed by calling SwitchScene static public method on
the Main Controller from anywhere
![Page 16: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/16.jpg)
Scene State Machine Implementation
• We can use a switch-case• Gets difficult to maintain and read
• We can use delegates• Unfortunately, setting a delegate allocates memory
• So we use an array of delegates.
![Page 17: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/17.jpg)
Scene State Machine Implementation
• Example Implementation• MainController, MenuController and GameController• Let’s look at the Profiler too!
![Page 18: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/18.jpg)
Implementing Gameplay
• Controllers manage the objects• Inter-object communication
• Static public methods• Temporary public instance methods• Messages, events
• Most common are:• Singletons• “Pool” controllers
![Page 19: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/19.jpg)
Singleton
• Only one of them in the game• Static public methods• Examples:
• Score• Player• World• Game
![Page 20: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/20.jpg)
Singleton
• Example Implementation
![Page 21: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/21.jpg)
Pool Based Objects
• Numerous instances in the game• Limited number at any one time• Preload simultaneously used quantity and disable• Static public method to “spawn” one finds first disabled one and
enables it• Examples:
• Explosions• Bullets• Enemies• Scenery
![Page 22: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/22.jpg)
GameObjects are loaded from storage and placed in a pool and disabled.
Object Pool
Storage
Pool Based Objects
![Page 23: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/23.jpg)
GameObjects are activated when needed.
Object Pool
Storage
Pool Based Objects
![Page 24: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/24.jpg)
GameObjects are deactivated when no longer needed. This returns them to the pool.
Object Pool
Storage
Pool Based Objects
![Page 25: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/25.jpg)
Pool Based Objects
• Example Implementation
![Page 26: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/26.jpg)
Download
• Find the Template.zip here: http://goo.gl/8WZGxn
![Page 27: OGDC 2014: Architecting Games in Unity](https://reader033.vdocument.in/reader033/viewer/2022061212/5495e658b479595f5e8b49c0/html5/thumbnails/27.jpg)
Best-Practices
• Use C#• Naming conventions• Logical folder structure• Zero-tolerance for warnings and errors• Zero-tolerance for runtime memory allocation