Introduction to Game Programming
Autumn 2016
3. Game architecture case
Unity game engine
Juha Vihavainen
University of Helsinki
Outline
Basic concepts and architecture of Unity
On origins/developments of Unity
Unity as an integrated development environment (IDE):
Unity world/level editor and its major user interfaces
The logical architecture of a Unity game
scenes => game objects => components => settings, scripts
Asset handling in Unity
Unity;; project's reusable models and images
(textures
textures)) are seen in Project pane (located in Assets folder)
Briefly on implementation and architecture of the Unity engine
Unity scripting in more detail later
6.12.2016 Juha Vihavainen / University of Helsinki 2
References
Lots and lots of Unity documentation and manuals, both online and
printed versions
Michael P. Rogers, Bringing Unity to the classroom. In Journal
of Computing Sciences in Colleges 27 (2012), 5 (May), 171-
171-
177.
Philip Chu, Game development with Unity,
Unity, Technicat LLC,
2010.
(One nice condensed introduction to Unity)
Joseph Hocking , Unity in Action - Multiplatform game
development in C# with Unity 5. Manning, 2015.
. . .
6.12.2016 Juha Vihavainen / University of Helsinki 3
Choosing a game engine
One option was an in
in--house game engine that usually only did what the
game required
Then, commercial full-
full-featured Unreal (by Epic Games,
Games, USA) and
CryEngine (by German game developer Crytek
Crytek)) were often licensed on
a per-
per-game basis
The Unity engine and development environment has become a popular
choice as an engine for small studios and individuals
offers a lot of high-
high-end features (physics, integrated asset mgmt..)
has a free version and low-
low-cost Pro versions (details changing)
runs either on a Mac (originally) or on a Windows PC
targets several platforms; in 2013, made the iOS and Android
licenses free for Unity free-
free-version users
Unity Free has no fee; it is available for any use to individuals or
companies with less than $100,000 of annual gross revenue
6.12.2016 Juha Vihavainen / University of Helsinki 4
Background: many kinds of libraries/engines
Game development is mostly in C/C++ and script languages, e.g.
Unreal Engine uses C++ (for core system code) and optionally a
custom visual script language Blueprint (for gameplay)
Unity is mostly implemented in C++ (and partly in BooBoo,, I think)
but can use UnityScript and C# as scripting languages
Blender is coded in C, C++ and Python
Python;; Python is used as an
internal scripting language (including its game engine part)
Ogre graphics engine (over 1,300,000 lines of C++)
OpenGL and DirectX graphics libraries (low level, in C)
Use mostly the same fundamental concepts (graphics, update loop)
Well do little or no 3D modeling; it is assumed friendly artists will
do that for us (or we can search free assets from the web)
Well only be interested in interactive video games, and not in
general graphics
6.12.2016 Juha Vihavainen / University of Helsinki 5
Unity basics
Unity is a multi
multi--platform development tool
platforms include:
include: desktops,
desktops, mobile devices,
devices, web pages,
pages, consoles
Unitys visual editor has several sections that work together
build/edit scenes/levels,
scenes/levels, editing of a selected game object (in
inspector pane)
scripts are attached to objects as special kinds of components
components,, and
executed by a hidden game loop
a Unity project is represented by a folder
C# scripts are edited and processed by a separate programming
environment (MonoDevelop
(MonoDevelop/Visual
/Visual Studio); UnityScript by a
suitable text editor
interactions/processing between different sections (views) is
mostly automatic, e.g., updating settings change visuals
6.12.2016 Juha Vihavainen / University of Helsinki 6
On history/developments (2005 -> )
Started 2005 as a Mac
Mac--based tool developing for Mac and Windows
Windows--based version available 2009 (Unity 2.5)
Windows
Unity 3.5 (Feb 2012), new Shuriken particle system, HDR, etc.
Unity 4 (June 2012): Mecanim animation system (bought
(bought),
), Direct
DirectX11
X11
support, Ubuntu platform, etc.
Unity 4.2 (July 2013): Android, iOS, Windows Phone 8, etc. were
included with the free license; dyn. shadows w/ one-
one-directional light
Unity 4.3 (2013) added complete native 2D tools (instead of 3rd-
3rd-party
2D tools), blendshape facial animation, among others
In July 2013, reached over 2 million registered Unity users
(downloads)
In March 3, 2015, the release of Unity 5.0 had improvements in
shading techniques, lighting, audio, animation, WebGL (soon), etc.
6.12.2016 Juha Vihavainen / University of Helsinki 7
Some more history
On May 21, 2002, Nicholas Francis, a Danish programmer, posted on the
Mac OpenGL board asking for assistance with a shader system he was trying
to implement into his game engine; Joachim Ante responded; David
Helgason heard about the project and thought they were "really onto
something", so jumped aboard as the third developer
Unity: "A
"A toolset used to build games, and .. the technology that executes the
graphics, the audio, the physics, the interactions, and the networking"
networking"
Nowadays, Unity has hundreds of
employees around the world
Needed to make a full commercial game using their new
engine; Gooball was published in March 2005
Using the profits from Gooball
Gooball,, the company hired more
developers to refine Unity before its initial 1.0 release in June 2005
an opportunity to tear apart the engine to find bugs, remove annoyances,
and fix the interface before its official release
6.12.2016 Juha Vihavainen / University of Helsinki 8
Some distinguishing features
Supports development of a wide range of games on diverse platforms
Supports C# and UnityScript (earlier also Boo
Boo)) as scripting languages
A single IDE provides prototyping tools for game development
edit world/level => edit components/scripts => run the game => . .
Applies Composite
Composite,, Component
Component,, Prototype
Prototype,, Game Loop, Update
Method plus other design patterns in its design and implementation
Especially, uses a well-
well-thought design for game objects
uses a version of "property
"property--centric
centric"" approach to game objects
dynamic components simulate multiple inheritance/mixins
(to compose an object from multiple varying parts)
Utilizes C# built-
built-in iterators to implement a coroutine facility => one
script (code block with state) may span multiple frames
6.12.2016 Juha Vihavainen / University of Helsinki 9
Inheritance vs. components
Unity in Action - Multiplatform game
development in C# by Joseph Hocking.
6.12.2016 Juha Vihavainen / University of Helsinki 10
Unity game engine: basic concepts
An asset is anything that is imported into your project (project
(project view),
view), and
then to be instantiated & placed into the game world (scene
(scene view)
view)
Scenes are used to create different levels (a term used by Unity docs)
separately loaded / managed (game object space and assets)
to make a "persistent
"persistent"" object: call DontDestroyOnLoad (object)
A game object is a essentially a container for components
A component is something to be attached to a game entity, describing
some property, capability, behaviour, or relationship
created independently but always attached to some game object
can be separately removed and destroyed
A script is a special kind of component that defines the behaviour of a
game object; the same script class can be "attached" to multiple
gameobjects, and multiple scripts can be attached to the same object
6.12.2016 Juha Vihavainen / University of Helsinki 11
How to start a game development with Unity
1. Create a Unity project for the game (creates a folder, actually)
2. Import assets via menu
menu,, or (directly) to the Assets folder
3. Create a scene for each level (or a game screen/page)
4. In each scene, select and place initial assets into the game world
5. Adjust/place the main (default) camera
camera;; add cameras as desired
6. Add light objects, adjust ambient light..
7. Add and adjust materials in objects' renderer components
8. Attach rigidbodies
rigidbodies,, physics materials,
materials, colliders to objects
9. Write and attach scripts to objects.
10. Test the game in the Editor's "Game
"Game Window"
Window" (pane, view)
11. Publish to the desired platform, building selected scenes
6.12.2016 Juha Vihavainen / University of Helsinki 12
The main parts of the UI of Unity Editor
1. Project view shows all of the project's assets, possibly in their
own subfolders (can select one
one-- or two
two--column formats)
2. Scene view where we arrange and manipulate game objects;
describes the initial configuration (state) of the level (scene)
3. Hierarchy view lists the objects of the current scene in a tree form
(an alternative view on all the game objects, in alphabetical order)
4. Game window shows the running game (as seen by the player)
5. Inspector where we can access asset parameters, game objects'
settings, components, script fields, and scene or project settings
6. Console can display system errors and warnings, user log/trace
output with Debug.Log ("Hello");,
("Hello");, etc.
Note. Never rearrange or rename the contents of the projects assets
Note.
folder outside the Unity editor (but just adding/updating is OK).
6.12.2016 Juha Vihavainen / University of Helsinki 13
Parts of the interface in Unity
6.12.2016 14
Editor screenshot: Toolbar
Toolbar,, Scene
Scene,, and Game
6.12.2016 Juha Vihavainen / University of Helsinki 15
6.12.2016 Juha Vihavainen / University of Helsinki 16
Game world/level
Hierarchy Project
Inspector
Running game
6.12.2016 Juha Vihavainen / University of Helsinki 17
Sample of Unity Editor toolbar controls
pan move rotate scale orbit Alt zoom Alt + RHB
Manipulate the
selected game object
Game "player"
(~ video player)
To selectively control which Select from default or
objects are rendered by which predefined world
cameras or lit by which lights Editor layouts
(also collisions and raycasting)
6.12.2016 Juha Vihavainen / University of Helsinki 18
Unity game architecture
The game objects (included in a scene) form a tree-
tree-like hierarchy
determined by parent-
parent-child relationships ( ~ scene graph)
graph)
children are affected by changing the parent's transform
properties: position
position,, rotation
rotation,, and scale
a child can also be changed and updated independently, but the
transform properties are interpreted in relation to the parent
Hierarchy view:
the initial
contents of the
scene
6.12.2016 19
Unity's own The main entities in Unity
object
root class by [Petri Veijonen]
C# root class
- combines aggregation with inheritance
AddComponent (type)
GetComponent (type) : Component
Can be enabled/disabled
Object.Destroy (obj)
Transform is Game object
not optional scripts
childCount Light
parent
GetChild (index):Transform;
AudioListener
materials
AudioSource
(shaders)
6.12.2016 Juha Vihavainen / University of Helsinki 20
BroadcastMessage calls the
Get common Components
method on MonoBehaviours
via public variables.
in this game object or any of
its children (a tree).
Note the duplication of
interfaces in GameObject
and Component. Attach responsibilities to an
object dynamically. Provides an
... alternative to subclassing.
+name is
inherited
.. // these are obsolete
// these four has been
deprecated in 5.0.;
use GetComponent
Note. Not
identical.
GameObject is sealed, Component is not.
21
Common game object components
Transform specifies position, rotation, and scale
MonoBehaviour adds custom functionality (as scripts)
MeshFilter represents a 3D mesh from the Assets folder
MeshRenderer draws the game object (with settings/materials..)
Collider makes rigid bodies react when colliding/touching
Rigidbody makes a game object obey physics (gravity, drag..)
but kinematic rigidbodies if moved only by its Transform
but they still affect other physics
physics--controlled objects
Camera to view the world from the game object's perspective
Light can act as a lighting source (lamp, other emissive object)
CharacterController moves an object by a script, instead of a
Rigidbody (still affected by collisions)
GUIText to be associated with a particular game object
6.12.2016 Juha Vihavainen / University of Helsinki 22
A script's public
variables are viewed
and edited within the
IDE: booleans appear
as checkboxes;
strings as textfields;
etc.
Initialization in the
Inspector overrides
initialization in the
script code
6.12.2016 Juha Vihavainen / University of Helsinki 23
The class MonoBehaviour
The base class for scripts
can be
enabled/
Awake (), Start (), Update (), disabled
FixedUpdate (), LateUpdate (), and
OnGUI () are kind of callbacks
(executed by the engine)
The variable gameObject refers to the
owner game object; also:
also: transform,
name, tag
Delayed calls
Convenient notation to access other
components (null
(null if not attached):
attached):
Coroutines named
c = GetComponent <Collider> (); . . . by string value
c = script.GetComponent <Camera> ();
Sample of services; Callbacks called
some events require colliders if present
24
Colliders in Unity
Colliders give game objects a "physical
"physical presence"
presence" so that they
"interact
interact"" (touch, push, collide) with each other
A mesh collider follows the surface of a geometry (model): expensive
Each primitive (built-
(built-in) game object has a default collider; they are
more efficient
Set physics materials to
define friction and
bounciness; provided as
standard assets.
Note. Put rigidbodies on
Note.
game objects that both
move and collide - that
react to collisions and
other forces (e.g., gravity).
6.12.2016 Juha Vihavainen / University of Helsinki 25
On the implementation of Unity
Unity is closed-
closed-source, so we cannot but guess at its actual internal
implementation (and anyway, it keeps changing)
Unity engine is itself written in C/C++ but
game logic is expressed via scripts (C#, UnityScript
UnityScript))
Generally, a component-
component-based engine supports well user interfaces that
use drag-
drag-and
and--drop-
drop-style game creation
any component can be handled both in the editor and by script code
All scripts are compiled to a .NET .dll
.dll file (i.e., an assembly) - and
finally to native code - so script languages tend to behave rather alike,
except perhaps differences in compile-
compile-time vs. run run--time typing/binding
UnityScript and C# are compiled into different assemblies so you
can't just refer to one from the other (messy compilation orders..)
6.12.2016 Juha Vihavainen / University of Helsinki 26
Unity system architecture (hypothetical sketch only)
[by Petri Veijonen]
Unity physics is based on a version of the PhysX physics simulation
engine; implementing/installing your own would be a major undertaking!
6.12.2016 Juha Vihavainen / University of Helsinki 27