Download presentation
Presentation is loading. Please wait.
Published byDarcy Davis Modified over 7 years ago
1
Getting Started with Unity and Entity/Component Model
Dr. Robert Zubek, SomaSim LLC EECS-395/495: Game Development Studio Winter Quarter Northwestern University
2
What’s Unity? Started as a simple game development toolkit back in ~2005 Grew to a *huge* multi-platform engine-and-editor tool Multiplatform development: Win + OSX Multiplatform targeting: Win, OSX, iOS, Android, Xbox, Playstation, and more! Handles mixed mobile + PC targeting better than competition These days Unity is An engine A built-in level editor A marketplace of assets
3
What’s a game engine? Provides functionality expected of all sorts of games Some engines are genre-specific, like (old) Unreal Engine Some engines are genre-agnostic, like Unity Lets you dive right into content authoring and gameplay coding Content authoring: adding levels, character models, animations, etc. Gameplay coding: adding gameplay systems (like weapons or upgrades), as opposed to generic infrastructure (like savefile management or network I/O)
4
What’s a game engine? Diagram from “Game Engine Architecture” by Jason Gregory OK so that’s not actually very readable…
5
What’s a game engine? 3rd party SDKs OS + drivers
(not part of game engine :D ) Hardware
6
What’s a game engine? Memory allocation, math libraries, RNG, data structures and algorithms, etc. Core libraries 3rd party SDKs OS + drivers Hardware
7
Async I/O, networking, serialization, localization, debugging, etc
What’s a game engine? Async I/O, networking, serialization, localization, debugging, etc Core systems Core libraries 3rd party SDKs OS + drivers Hardware
8
What’s a game engine? Loading/managing textures, 3d models, fonts, animations, collision, scenes, etc. Resource management Core systems Core libraries 3rd party SDKs OS + drivers Hardware
9
What’s a game engine? Main engine systems Rendering system, skeletal animation, VFX, particles, collision / physics, cinematics, UI, input management, etc. Resource management Core systems Core libraries 3rd party SDKs OS + drivers Hardware
10
Game objects and reusable systems
What’s a game engine? Game objects and reusable systems Game objects, async events, action scripts, state machines, AI, cameras, components Main engine systems Resource management Core systems Core libraries 3rd party SDKs OS + drivers Hardware
11
What’s a game engine? Title-specific gameplay systems
Resources, buffs, powerups, weapons, loot drops, levels, enemies, etc. Game objects and reusable systems Main engine systems Resource management Core systems Core libraries 3rd party SDKs OS + drivers Hardware
12
What’s a game engine? Title-specific gameplay systems
Game objects and reusable systems Main engine systems Typical C/C++ engine Unity Resource management Core systems .NET (Mono) Core libraries 3rd party SDKs OS + drivers Hardware
13
Game Loop Initialize()
while(KeepPlaying()) { ReadInputs(); UpdateGameState(); Draw(); } Release();
14
Game Loop Initialize()
while(KeepPlaying()) { ReadInputs(); UpdateGameState(); Draw(); } Release(); Physics really wants to run at a fixed framerate Visuals can run on variable framerate but as often as possible Game logic can run at a lower framerate. Also, it can be “spikey” 60 FPS = max 16 ms per frame
15
Game Loop Initialize()
while(KeepPlaying()) { ReadInputs(); UpdateGameState(); Draw(); } Release(); Read controller inputs Read data over the network GAMEPLAY CODE: update player – update AI – run game-specific logic – manage entity lifetime – call engine for anything more complicated ENGINE CODE: physics – animation – vfx / sfx – networking – other specialized systems Prepare meshes for display – viewport culling and other optimizations – convert to raw data and send to the GPU
16
Game Loop Initialize()
while(KeepPlaying()) { ReadInputs(); UpdateGameState(); Draw(); } Release(); Read controller inputs Read data over the network GAMEPLAY CODE: update player – update AI – run game-specific logic – manage entity lifetime – call engine for anything more complicated ENGINE CODE: physics – animation – vfx / sfx – networking – other specialized systems Prepare meshes for display – viewport culling and other optimizations – convert to raw data and send to the GPU
17
Entities Everything is a separate entity!
Entity has no logic inside itself Instead it relies on components for actual gameplay code entity entity entity entity
18
Inheritance vs Composition
Classic way to do OOP via inheritance: Object Agent inherits from Object NPC inherits from Agent Car inherits from Agent Building inherits from Object House inherits from Vehicle Business inherits from Vehicle Decoration inherits from Object Tree inherits from Decoration Object Agent Building Deco NPC House Tree Car Business
19
Cautionary Tale About Inheritance
Sketch of the entity class hierarchy: DECEMBER 2010 Cautionary Tale About Inheritance Zubek, “Engineering CityVille”, GDC Online 2011
20
Sketch of the entity class hierarchy:
DECEMBER 2010 MARCH 2011 Zubek, “Engineering CityVille”, GDC Online 2011
21
Sketch of the entity class hierarchy:
DECEMBER 2010 MARCH 2011 JUNE 2011 Zubek, “Engineering CityVille”, GDC Online 2011
22
??? Sketch of the entity class hierarchy: DECEMBER 2010 MARCH 2011
JUNE 2011 ??? Zubek, “Engineering CityVille”, GDC Online 2011
23
Relentless Evolution “The only constant is change”
Everything will change given enough time Need to keep “future compatibility” in mind Prepare to keep growing and iterating “Construction set” approach to keep building the game out over time Zubek, “Engineering CityVille”, GDC Online 2011
24
Inheritance vs Composition
Using composition instead of inheritance: 1. Game logic stored in “components” Each component responsible for one thing Use as many as needed 2. Entities are just bags of components 3. Each component gets called by the engine Start() Update() etc. House Entity Sprite House Logic - Business Entity Business Logic NPC Entity AI Pathfinding Tree Entity
25
Unity-specific class hierarchy
Object contains one or more of GameObject Component Transform (position, rotation, scale, …) Renderer (how to draw this thing…) Rigidbody (how it moves as a physics object…) … MonoBehaviour User-defined components
26
Unity-specific class hierarchy
Each Unity component combines data and code: Component Transform Renderer Rigidbody … MonoBehaviour User-defined components
27
Composition benefits House Entity Business Entity NPC Entity
Composition presents a “has-a” relationship instead of an “is-a” relationship NPC contains a pluggable AI module, instead of “being a subclass of smart entity” Benefits over inheritance: Easier to mix and match functionality Easier to make data-driven Easier for a large team to work together House Entity Sprite House Logic - Business Entity Business Logic NPC Entity AI Pathfinding Tree Entity
28
Game Loop, Unity-style On Each Frame { … foreach game object in the scene if this game object is active foreach component in this game object component.update() House #52 House #53 NPC #356 Tree #975 sprite comp sprite comp sprite comp sprite comp house comp house comp AI comp
29
But there’s another way
Something that an entity/component engine could do (but Unity doesn’t): 1. Lay out all component data for various entities in one big array 2. An entity just contains array indices to those big arrays 3. Component update: just iterate over everything in the big array
30
Data-centric composition
“Processors” contain arrays of each component’s data. Entities are just indices to those arrays. Preparation: pack all component data into flat arrays, one for each comp. On each frame: For each component type Get its array of data Iterate and process each entry House #52 House #53 NPC #356 Tree #975 Sprite Processor sprite data etc Housing Processor house data - AI Processor AI data
31
Memory access patterns
For each entity For each component Call Component.Update() For each component type Get its array of data Iterate and process each entry (Talk about how entities implemented as dictionaries of components allocated on the heap can have pretty random memory locations)
32
Why is cache friendliness important?
To understand why this is so, let’s take a little detour down the computer architecture lane. RAM HD / SSD
33
Why is cache friendliness important?
Intel i3-2120 Size Random Access Register 100s of bytes 0.3 ns L1 cache 10s of KB 1.5 ns L2 cache 100s of KB 4 ns L3 cache 1s of MB 9 ns RAM 1s of GB 70 ns SSD 100s of GB 15,000 ns HD 1000s of GB 4,000,000 ns L1 L2 L1 L2 L1 L2 L1 L2 To understand why this is so, let’s take a little detour down the computer architecture lane. 1 frame at 60 FPS ,000,000 ns More info: RAM HD / SSD
34
Does this matter? Indie games 100s or 1000s of entities
CPU + memory generally under-utilized Doesn’t matter AAA games 10,000s of entities or more CPU + memory heavily contested by all game systems Yes, definitely matters THIS CLASS :)
35
Unity-specific class hierarchy
Object contains one or more of GameObject Component Transform (position, rotation, scale, …) Renderer (how to draw this thing…) Rigidbody (how it moves as a physics object…) … Accessed in code via GameObject.AddComponent<>() GameObject.GetComponent<>() etc MonoBehaviour User-defined components
36
What’s in a component? A very basic Unity component is a subclass of MonoBehaviour class This one implements two functions: Start() Update() You can attach this component in the visual editor, or in code by calling AddComponent()
37
Component functionality
Implemented as “magic functions” for legacy reasons See list at Some highlights: Awake() – when an object is created Start() – after Awake(), when an object is enabled and ready to run Update() – once per frame (at variable frame rate) FixedUpdate() – once per fixed framerate (when interacting with physics subsystem) … and many more, for physics, collisions, mouse interactions, rendering, etc.
38
Entity in the editor Transform Mesh GameObject Collider Renderer
click to inspect
39
Unity the Engine / Unity the Editor
In addition to the engine and programming support, Unity also contains a built-in editor (Some engines don’t.) Good for Laying out levels, adding lights, etc. Designing the UI Doing visual work in general … or you can ignore it and just code :)
40
2D games Here’s an example of a game done entirely with 2d assets
41
2D sprites Unity has a lot of built in functionality for 2D:
Baking spritesheets from loose images Animating sprites 2D physics and collision
42
2D resources Unity’s own tutorials on everything 2D: Variety of free graphical assets: _sites.php Also free (and paid) assets in the Unity Asset Store
43
3D games More complicated Much more complicated
44
3D asset creation 3d mesh texture map animated 3d model skeleton
animation
45
3D resources Tons of different skills go into making 3d assets from scratch You don’t have to do anything like that for your final project! But if you feel like experimenting with 3D, check out free assets in the Unity Asset Store
46
How to learn? Unity evolves quickly Online resources are best
Official: Recommended:
47
Homework preparation 1. Install Unity (personal edition) from 2. Bookmark sections of the Unity Manual for reference: Unity Overview: Scripting Overview:
48
Homework 1. Follow along the two PDF docs:
“First Steps in Unity for Programmers” “Simple Endless Runner” 2. Pick some other online tutorial and also complete it Recommended: Catlike Coding Unity Tutorials. Pick any one. “Maze” is recommended. You can also do other tutorials. 3. Implement some kind of an interesting variant on one of the tutorials. Deliberately vague. Do what you want! Will it be simple, or ambitious? 4. Submit everything by EOD Sun 1/10 – instructions on Canvas
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.