Montréal

Unity3D

 

Unity3D

Unity3D

About

  • Developed by Rahul Unity Technologies
  • Cross-platform game engine
    • PC : Windows / Linux / macOs
    • Consoles PS3 / X360 - PS4 / XOne / Switch / 3DS / PS Vita
    • Mobile : Android / iOS
    • VR
    • Web
  • Exploits several Graphics API
    • OpenGL
    • DirectX
    • Metal
    • Vulkan

Unity3D

About

  • First version was released in june 8, 2005
  • Only available on Windows and macOs systems for now
  • But there is an experimental version for Linux (old)

Unity3D

Versions

  • Since the beginning Unity worked with the Semantic Versioning System:
    • x.y.z
    • 4.6.1
    • 5.5
    • etc...
  • Today Unity work on a year versioning system:
    • 2017.x
    • 2018.x
    • etc...

Unity3D

The Middleware

Unity3D

Definition

From Wikipedia :

Middleware is computer software that provides services to software applications beyond those available from the operating system. It can be described as "software glue".

In the case of Unity3D

Allows multiple trades to develop a game / application through a single tool

Unity3D

Unity API

C#
Javascript

Boo (Python)

ShaderLab, GLSL

Sealed

Unity Engine Core

Shader Programming API

Network API

Animation Engine

GUI System

Physics Engine

Terrain Engine

Particle System Engine (Shuriken)

Audio Engine

Unity3D

Compilers

  • Mono: based on .NET 2.0 / 3.5 and 4.6
    • Primary Focus: quick iteration and sandboxing, enable productivity
    • Code quality: Not awesome
  • IL2CPP
    • Primary focus: shipping on non-JIT platform (iOS)
    • Code quality: mixed results. Some OK, some bad
    • Difficult to iterate on code quality due to additional C++ step

Today Unity have the possibility to switch between compilers:

Unity3D

Compilers

Burst (available on 2018.2):

  • Custom in-house compiler stack to take advantage of better defaults performance
  • Primary focus : on performance
  • A performance Sandbox

Unity3D

Mono Framework

  • Mono is a based framework on .NET 2.0 / 3.5  and 4.6
  • This framework is used as a scripting framework
  • This is not the compiler that builds Unity
  • Only used to compile JavaScript / C# / Boo scripts

Unity3D

C# with Mono & .NET

C# Code

C# Compiler

IL (Intermediate language) Code

Just-In-Time (JIT) Compiler

Native Machine Code

Execute

The Common Language Runtime

Unity3D

IL2CPP

  • It's an other scripting framework developed by Unity
  • Used as an alternative to Mono when building projects to specific platforms
    • Android
    • iOS
    • Nintendo 3DS and Switch
    • etc...

Unity3D

C# with IL2CPP

Unity3D

The Interface

Unity3D

Unity3D

  1. Scene View : Editable 3D game objects in the current scene
  2. Hierarchy : Text list of game objects and sub-objects in the current scene
  3. Game View : Preview how game will look when executing
  4. Project : Content of Project assets folder
    1. Libraries
    2. Scripts
    3. Sprites
    4. Etc.
  5. Inspector : Properties of the currently selected item
    1. Game Object
    2. File from the Project
    3. Etc.

Unity3D

Technical Organization

Unity3D

C-E-S

  • Component Entity System is a design pattern used in Unity3D
  • Here an example : we having a strong hiearchy of inheritance
  • We have the case where we want to build an EvilTree class
  • There is a problem, because of our hierarchy, our class have to be in the middle and inherits two classes
  • This is where the CES pattern comes

Unity3D

C-E-S

  • Component : Like a C struct. Only capable to storing data
    • Position
    • Velocity
    • Physics
    • Sprite
  • Entity : is something that exists in our game world. It's like a list of components
    • Rock (position, sprite)
  • System : This is where we put the logic. It exploits data which come from entities (their components)
    • Movement (Position, Velocity) : Adds velocity to our position
    • Gravity : Accellerate velocity due to gravity

Unity3D

C-E-S

Unity3D

C-E-S

WARNING:

  • Unity doesn't exploit fully this design pattern
  • It try to exploit mainly the Component and Entity part
  • Since the last GDC they working on a new architecture using the whole Design Pattern in their own way
  • More example : https://www.youtube.com/watch?v=lNTaC-JWmdI

Unity3D

Scripting

Unity3D

An Entity is composed by components which are categorized in 2 parts:

  • Data : position, material (texture and shaders)
  • Scripts : code that manipulate those data or data coming from another Entity

Unity3D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    }
}
  • When we create a script from Unity, it will generate some code to be functionnal
  • Every script is a class which derive from the base class MonoBehaviour
  • With this inheritance, Unity will consider this script as a component that we can attach to an entity

Unity3D

GameObject Paradigm

Unity Components

Customized GameObject

Assets (Resource Library)

Camera

Collider

Light

Rigidbody

Camera

FPS CTRL

Audio Listener

GameObject

Custom GameObject

Script Files

Animation

Prefab

Mesh

...

Compose

Fetch

Unity3D

Unity Events

  • The engine handle it's own main loop game system
  • So we don't need to develop our own
  • But that's mean that we have to respect the engine process and its different events
  • There is a ton of events, here some examples :
    • Awake
    • Start
    • Update
    • FixedUpdate
    • LateUpdate
    • OnTriggerXXX
    • OnCollisionXXX
    • Etc...

Unity3D

Unity Events

Big picture of the monobehaviour flowchart

  • Splited in 11 steps
    • Initialization
    • Editor
    • Physics
    • Input Events
    • Game Logic
    • Scene Rendering
    • Gizmo Rendering
    • GUI Rendering
    • End of Frame 
    • Pausing
    • Decommissioning

Unity3D

Unity Events

Here some fundamentals events you must know :

  • Awake(): is called each time that we instantiate an instance
  • Start(): is called before the first frame update only if the script is enabled
  • FixedUpdate(): is called called at least once per frame (we perform the physics part)
  • Update(): is called once per frame (we perform the game logic part)
  • LateUpdate(): is called once per frame at the end of the frame after Update() (we can perform camera animations)
  • OnCollisionXXX methods : are called when there is an interaction collision between 2 objects
  • OnTriggerXXX methods : are called when there is a collision between 2 objects but there is no physics application (objects can overlap)

Unity3D

A Frame

  • Time.time
  • Time.deltaTime
  • C# System.Timers.Timer

Update() and FixedUpdate()

U

U

U

U

U

U

U

U

U

U

U

U

U

F

F

F

F

F

F

0.0s

0.1s

FixedUpdate() is synchronized with Physics Engine steps while Update() runs on each frame. Always use FixedUpdate() for Physics!

Unity3D

Architecturing a game

Unity3D

Quick answer: it's impossible to make a generic architecture of scripts for every type of game

Not so quick answer: it's possible to respect a set of rules that we can apply on the majority of projects

Will exist the whole runtime

Scene 1

Scene 2

Entry point

SoundManager

LevelManager

SceneManager

Player

Enemy

Weapons

Player

NPCs

Unity3D

  1. Is the script is reusable in the project (or outside)?
  2. How to keep control of our game during the whole execution?
  3. What should be always available for our entities?
  4. How we should manage resources? (textures, prefabs)

Questions

Unity3D

Links

Unity3D

ISART - Unity3D 01

By Vincent Nicopolsky

ISART - Unity3D 01

  • 325