\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Become A Game Developer (become)} \pdfinfo{ /Title (unity-2d-basics.pdf) /Creator (Cheatography) /Author (Become A Game Developer (become)) /Subject (Unity 2D Basics Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{030303} \definecolor{LightBackground}{HTML}{F7F7F7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Unity 2D Basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Become A Game Developer (become)} via \textcolor{DarkBackground}{\uline{cheatography.com/67591/cs/17006/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Become A Game Developer (become) \\ \uline{cheatography.com/become} \\ \uline{\seqsplit{becomeagamedeveloper}.github.io/site} \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 13th September, 2018.\\ Updated 13th September, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Editor's Interface}} \tn % Row 0 \SetRowColor{LightBackground} Scene view & Build the game world, interact with game objects \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Game view & Preview and play game (pressing Play in Toolbar) \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} Inspector & Show and modify game objects' components' properties \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} Hierarchy & Game objects in game \tn % Row Count 10 (+ 1) % Row 4 \SetRowColor{LightBackground} Project & Assets available \tn % Row Count 11 (+ 1) % Row 5 \SetRowColor{white} Toolbar & Bar with buttons at the top. Contains transform tools, play controls, layers and layout \tn % Row Count 15 (+ 4) % Row 6 \SetRowColor{LightBackground} Assets & Files (scripts, textures, models, prefabs) \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} Console & Contains debug logs and errors \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} Tags, Layers, Sorting Layers & Open from Edit \textgreater{} Project Settings \textgreater{} Tags and Layers. Tags are identifiers for Game Objects. Game Objects can belong to Layers. Objects in last Layers are rendered above the others. SpriteRender Components can belong to Sorting Layers, which define the rendering order for sprites. Camera Components can see or not Sorting Layers by setting the Culling Mask \tn % Row Count 35 (+ 16) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Game Objects and Components}} \tn % Row 0 \SetRowColor{LightBackground} Game Object & Basic entity in Unity. Can be a 3D or 2D object, a particle or audio or video source, a UI element, or an empty object. Game Objects are just containers for Components. Scripts can be attached to Game Objects, to define their behavior and properties. Game Objects in your scene are represented in the Hierarchy \tn % Row Count 16 (+ 16) % Row 1 \SetRowColor{white} Component & Basic entities that implement functionalities inside Game Objects \tn % Row Count 20 (+ 4) % Row 2 \SetRowColor{LightBackground} Component in the Inspector & Each Component has a small header bar with: Turn down arrow, Icon, (De)activate checkbox, Reference book (opens online manual), Preset button, Options gear (allows to copy and paste Components).\{\{nl\}\}Under the bar are all the Component's properties \tn % Row Count 33 (+ 13) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Game Objects and Components (cont)}} \tn % Row 3 \SetRowColor{LightBackground} Prefab & Blueprint for Game Objects. You can make a Prefab out of a Game Object. The Prefab will be like a "model" from which you can instantiate new identical copies of that object in your game. Modifying the Prefab properties will modify all Game Objects instantiated from it \tn % Row Count 14 (+ 14) % Row 4 \SetRowColor{white} Parent/Child & Any Game Object can have other Game Objects as children. The Transform of a child Game Object will be relative to the parent's Transform. If you make a Prefab out of a Game Object with children, all the hierarchy will be copied. You can see parent/child relationships in the Hierarchy \tn % Row Count 29 (+ 15) % Row 5 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Usage}}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Game Objects and Components (cont)}} \tn % Row 6 \SetRowColor{LightBackground} Create new Game Object & Right click on the Hierarchy \textgreater{} select the Game Object type \tn % Row Count 3 (+ 3) % Row 7 \SetRowColor{white} Game Objects' name & Set the name from the Inspector (upper part), or from slow double click on the object in the Hierarchy \tn % Row Count 9 (+ 6) % Row 8 \SetRowColor{LightBackground} Tag & Assign custom Tags to Game Objects from the Inspector (upper part) \tn % Row Count 13 (+ 4) % Row 9 \SetRowColor{white} Add a child & In the Hierarchy, drag a Game Object over another \tn % Row Count 16 (+ 3) % Row 10 \SetRowColor{LightBackground} Add Component & Inspector \textgreater{} Add Component \tn % Row Count 18 (+ 2) % Row 11 \SetRowColor{white} Create Prefab & Drag the Game Object from the Hierarchy to the Project window \tn % Row Count 22 (+ 4) % Row 12 \SetRowColor{LightBackground} Create Game Object from Prefab & Drag the Prefab from the Project to the Scene view or the Hierarchy \tn % Row Count 26 (+ 4) % Row 13 \SetRowColor{white} Modify a Prefab & If you select a Prefab from the Project, and you modify its \seqsplit{properties/components}, all objects of that type will be modified. On the contrary, if you modify a single Game Object, you can then, from the Inspector (upper part) click on Prefab: Apply button to modify the Prefab \tn % Row Count 40 (+ 14) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Game Objects and Components (cont)}} \tn % Row 14 \SetRowColor{LightBackground} Deactivate Game Object & Click the tickbox in the upper part of the Inspector \tn % Row Count 3 (+ 3) % Row 15 \SetRowColor{white} Reference Game Object in the Inspector & If you define public `GameObject` or Component (ex: `Transform`) variables in a script, they will be visible as properties in the Inspector (under the Script Component). You can assign Game Objects to these variables by dragging a Game Object from the Hierarchy to the field in the Inspector. If the variable is of type `GameObject`, you reference the whole Game Object. If it is of some Component type, instead, you will reference that Game Object's Component directly instead \tn % Row Count 27 (+ 24) % Row 16 \SetRowColor{LightBackground} Reference Prefab in the Inspector & The same way you reference a Game Object or a Component in a Script variable from the Inspector, you can drag a Prefab from the Project window to reference it. This is useful for instantiating copies of the Prefab later on \tn % Row Count 39 (+ 12) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Game Objects and Components (cont)}} \tn % Row 17 \SetRowColor{LightBackground} Instantiate Prefab & To instantiate a Prefab from a Script: define a variable of type `GameObject` or of some Component type. Reference the Prefab from the Inspector. You can now use the `Instantiate()` function (see API section) \tn % Row Count 11 (+ 11) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.29402 cm} x{3.68298 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic Game Objects and Components}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Basic Game Objects}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Sprite & 2D graphic Game Object. Contains SpriteRenderer component, that manages the rendering of the texture. If you add 2D Colliders and/or 2D RigidBody Components, the Sprite will behave like a physical object \tn % Row Count 8 (+ 7) % Row 2 \SetRowColor{LightBackground} Camera & Contains a Camera component and an Audio Listener. Gives the window through which you can experience your game's world. In a new scene, there is always a Main Camera already present. You can parent a Camera to a Game Object to follow it (or setup a script that continually sets the Camera's position to the Game Object's position) \tn % Row Count 20 (+ 12) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Basic Components}}} \tn % Row Count 21 (+ 1) % Row 4 \SetRowColor{LightBackground} Transform & Determines position, rotation and scale. It is always present \tn % Row Count 24 (+ 3) % Row 5 \SetRowColor{white} \seqsplit{SpriteRenderer} & Display an image (Sprite property). You can create and set a Sorting Layer to define which sprite is rendered above and which below when two sprites overlap \tn % Row Count 30 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.29402 cm} x{3.68298 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic Game Objects and Components (cont)}} \tn % Row 6 \SetRowColor{LightBackground} Camera & Capture and display the world. Has several options, such as background default color, field of view. In Culling Mask you can set what layers to render and what to ignore. Game Objects belonging to ignored layers won't be seen \tn % Row Count 8 (+ 8) % Row 7 \SetRowColor{white} Script & Defines custom properties and behavior of a Game Object \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.59264 cm} x{3.38436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{2D Physics}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Physics Components}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Rigidbody 2D & Places an object under control of the physics engine, giving it a Body Type (see below), a mass, a new position (overriding the Transform's one), a velocity, an angular velocity, a Material (defining drag and bounce), and allowing it to be affected by forces (gravity, drag, impulse). Attaching a Rigidbody to a Sprite makes it behave in a physically convincing way \tn % Row Count 15 (+ 14) % Row 2 \SetRowColor{LightBackground} Collider 2D & Defines the shape for the purpose of collisions. Can be edited by clicking "Edit Collider". Can be set to "Trigger" to emit events \tn % Row Count 20 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Body types}}} \tn % Row Count 21 (+ 1) % Row 4 \SetRowColor{LightBackground} Dynamic & Body designed to move. Collides with any body type. Can be affected by forces \tn % Row Count 24 (+ 3) % Row 5 \SetRowColor{white} Static & Doesn't move (infinite mass). Collides with Dynamic bodies. Gives back forces when colliding \tn % Row Count 28 (+ 4) % Row 6 \SetRowColor{LightBackground} Kinematic & Designed to move (only via function calls). It moves accordingly to its velocity, but it's not affected by forces. Collides only with Dynamic bodies \tn % Row Count 34 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.59264 cm} x{3.38436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{2D Physics (cont)}} \tn % Row 7 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Mechanics}}} \tn % Row Count 1 (+ 1) % Row 8 \SetRowColor{white} Spatial coordinates & The position of a body is identified by a point (Vector3 with 3 coordinates in 3D space, Vector2 in 2D space). In games, the X axis grows from left to right, while the Y grows from top to bottom (it's reversed) \tn % Row Count 9 (+ 8) % Row 9 \SetRowColor{LightBackground} S = V * T & A body with velocity V moves by S in a timestep T \tn % Row Count 11 (+ 2) % Row 10 \SetRowColor{white} V = A * T & A body with an acceleration A increases its velocity by V in a timestep T \tn % Row Count 14 (+ 3) % Row 11 \SetRowColor{LightBackground} F = m * A & Applying a force F to a body with mass m causes an acceleration A on it \tn % Row Count 17 (+ 3) % Row 12 \SetRowColor{white} Static drag & If there is a static drag D on a surface, a body cannot move unless you apply a force F \textgreater{} D to it \tn % Row Count 21 (+ 4) % Row 13 \SetRowColor{LightBackground} Dynamic drag & If there is dynamic drag D on a surface, a body moving on it will constantly have a force D {\emph{opposed}} to where it's moving \tn % Row Count 26 (+ 5) % Row 14 \SetRowColor{white} Gravity & If there is a gravitational acceleration g, a body will have a downwards acceleration of g \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.59264 cm} x{3.38436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{2D Physics (cont)}} \tn % Row 15 \SetRowColor{LightBackground} Trajectory & Curve on which a body moves. A projectile (Angry Bird) with just an initial velocity and in a gravitational field will "draw" a parabula shape. The projectile lands farther if the initial velocity vector was at 45° \tn % Row Count 8 (+ 8) % Row 16 \SetRowColor{white} Angular mechanics & When dealing with rotations, simply substitute: position with angle, velocity with angular velocity, acceleration with angular acceleration, force with torque, drag with angular drag. The laws stay the same \tn % Row Count 16 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Scripting}} \tn % Row 0 \SetRowColor{LightBackground} What is a script & A script is a file containing code (usually C\#) that defines the properties and the behavior of a Game Object \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} Adding a script & Add a Script to a Game Object from Inspector \textgreater{} Add Component \textgreater{} New script \tn % Row Count 10 (+ 4) % Row 2 \SetRowColor{LightBackground} Editing a script & Double click on the script in the Inspector. It will be open with your default external editor (Visual Studio, Monodevelop). Then change the script and save it \tn % Row Count 18 (+ 8) % Row 3 \SetRowColor{white} Script contents & A script usually has import statements in the upper part, and then the code of a class (with the same name of the file), that contains variables and methods \tn % Row Count 26 (+ 8) % Row 4 \SetRowColor{LightBackground} Set variable from Inspector & From the Inspector you can set values for public variables defined in Scripts. For numbers and strings, type directly in. For `GameObject` or any Component types, drag Game Objects or Prefabs from the editor to the field \tn % Row Count 37 (+ 11) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Scripting (cont)}} \tn % Row 5 \SetRowColor{LightBackground} Compiling & Once a script is saved, Unity automatically (re)compiles it. It may take some time (wait for the loading gif in the bar below to disappear) \tn % Row Count 7 (+ 7) % Row 6 \SetRowColor{white} Debugging & Compile-time errors and debug logs (outputs of Debug.Log() ) are shown in the Console \tn % Row Count 12 (+ 5) % Row 7 \SetRowColor{LightBackground} Accessing classes from other scripts & If a Script defines a `public` class, you can use that class as a reference in any other Script. Ex: the script Enemy contains a reference to the class Player because it needs to chase it \tn % Row Count 21 (+ 9) % Row 8 \SetRowColor{white} Documentation & From the text editor, select a term and press Ctrl + ' \tn % Row Count 24 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{C\#}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Syntax}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `{\emph{statement}} ;` & End every statement with a semicolon \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `using {\emph{namespace}}` & Include namespace, making new classes available \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} `class {\emph{name}} : {\emph{father}} \{ \}` & Define class (inheriting from {\emph{father}} class). A class is a blueprint that you can use to instantiate an object: a special variable that contains its own variables (members) and functions (methods) \tn % Row Count 16 (+ 10) % Row 4 \SetRowColor{LightBackground} `public {\emph{field}}` & Make a member or method visible in the Inspector and accessible from other scripts \tn % Row Count 21 (+ 5) % Row 5 \SetRowColor{white} `private {\emph{field}}` & Deny access from other scripts \tn % Row Count 23 (+ 2) % Row 6 \SetRowColor{LightBackground} `// {\emph{comment}}` & One-line comment \tn % Row Count 24 (+ 1) % Row 7 \SetRowColor{white} `/* {\emph{multi-line \{\{nl\}\} comment}} */` & Multiple line comment \tn % Row Count 26 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Types}}} \tn % Row Count 27 (+ 1) % Row 9 \SetRowColor{white} `bool` & `true` or `false` \tn % Row Count 28 (+ 1) % Row 10 \SetRowColor{LightBackground} `int` & Integer number \tn % Row Count 29 (+ 1) % Row 11 \SetRowColor{white} `float` & Decimal number. Floats always end in {\emph{f}}. Ex: `4.5f` \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{C\# (cont)}} \tn % Row 12 \SetRowColor{LightBackground} `string` & Text \tn % Row Count 1 (+ 1) % Row 13 \SetRowColor{white} `{\emph{someType}}{[}{]}` & Array containing objects of type {\emph{someType}} \tn % Row Count 4 (+ 3) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Variables}}} \tn % Row Count 5 (+ 1) % Row 15 \SetRowColor{white} `{\emph{int}} a;` & Declaring a variable \tn % Row Count 6 (+ 1) % Row 16 \SetRowColor{LightBackground} `{\emph{a}} = {\emph{5}};` & Assigning a value \tn % Row Count 7 (+ 1) % Row 17 \SetRowColor{white} `{\emph{ClassName}} {\emph{b}};` & Declaring a reference (variable that can contain an object) \tn % Row Count 10 (+ 3) % Row 18 \SetRowColor{LightBackground} `{\emph{ComponentClass}} {\emph{myComponent}}; ` & For every Component, there exist a class with the same name that you can use to refer to it (ex: `Transform`) \tn % Row Count 16 (+ 6) % Row 19 \SetRowColor{white} `{\emph{b}} = new {\emph{ClassName}}();` & Instantiating an object \tn % Row Count 18 (+ 2) % Row 20 \SetRowColor{LightBackground} `{\emph{object.variable}}` & Accessing an object's member variable \tn % Row Count 20 (+ 2) % Row 21 \SetRowColor{white} `null` & Value for null reference (a variable referring to no object) \tn % Row Count 23 (+ 3) % Row 22 \SetRowColor{LightBackground} `{\emph{int}}{[}{]} {\emph{myList}} = new {\emph{int}}{[}{\emph{5}}{]};` & Create and assign empty array of {\emph{5 integers}}. \tn % Row Count 26 (+ 3) % Row 23 \SetRowColor{white} `{\emph{myLlist}}{[}{\emph{0}}{]} = {\emph{9}};` & Assign a value to an index of an array (indexing starts from 0) \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{C\# (cont)}} \tn % Row 24 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Methods}}} \tn % Row Count 1 (+ 1) % Row 25 \SetRowColor{white} `{\emph{retType Name}}({\emph{type1 arg1, ...}}) \{\{\{nl\}\}{\emph{body}}\{\{nl\}\}return {\emph{X}}; \}` & Method definition. Can take one or more arguments in. You must specify the type of the returned object/variable. Use `void` if there is no return statement \tn % Row Count 9 (+ 8) % Row 26 \SetRowColor{LightBackground} `{\emph{object.method( )}}` & Calling a method \tn % Row Count 10 (+ 1) % Row 27 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Control flow}}} \tn % Row Count 11 (+ 1) % Row 28 \SetRowColor{LightBackground} `if({\emph{condition}})\{\{\{nl\}\}{\emph{code}}\{\{nl\}\}\} else if ({\emph{condition}}) \{\{\{nl\}\}{\emph{code}}\{\{nl\}\}\} else \{\{\{nl\}\}{\emph{code}}\}` & Conditional statement \tn % Row Count 16 (+ 5) % Row 29 \SetRowColor{white} `while({\emph{condition}})\{\{\{nl\}\}{\emph{code}}\}` & While loop. Executes {\emph{code}} until {\emph{condition}} is false \tn % Row Count 19 (+ 3) % Row 30 \SetRowColor{LightBackground} `for(int i={\emph{X}}; i++; i\textless{}{\emph{N}})\{\{\{nl\}\}{\emph{code}}\}` & For loop. Initializes {\emph{i}} to {\emph{X}} and executes {\emph{code}} as long as {\emph{i\textless{}N}} \tn % Row Count 23 (+ 4) % Row 31 \SetRowColor{white} `foreach({\emph{type}} {\emph{x}} in {\emph{myList}})\{\{\{nl\}\}{\emph{code}}\}` & Executes {\emph{code}} looping over {\emph{myList}}. {\emph{x}} is the current element of the list \tn % Row Count 27 (+ 4) % Row 32 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Operators}}} \tn % Row Count 28 (+ 1) % Row 33 \SetRowColor{white} + - * / \% ? ! ++ -{}- & Operators (4 operations, modulus, ternary conditional, not, increase, decrease) \tn % Row Count 32 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{C\# (cont)}} \tn % Row 34 \SetRowColor{LightBackground} \textless{} \textgreater{} == != \textless{}= \textgreater{}= & Relational operators (lesser, greater, equal, different, less or equal, greater or equal) \tn % Row Count 5 (+ 5) % Row 35 \SetRowColor{white} `{\emph{cond1}} \&\& {\emph{cond2}}` & 'and' operator. True only if both conditions are true \tn % Row Count 8 (+ 3) % Row 36 \SetRowColor{LightBackground} `{\emph{cond1}} || {\emph{cond2}}` & 'or' operator. False only if both conditions are false \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{In {\emph{italics}} generic or sample terms} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Code Flow and Events}} \tn % Row 0 \SetRowColor{LightBackground} Code Flow & Scripts do not run in the traditional manner, looping until they complete a task. Instead, Unity runs the main Game Loop (think of it as a `while` loop where continuously the following things happen: external input is taken, the game state is updated, objects may be created and destroyed, physics and graphics computations are run, and a new frame is rendered on the screen). When events of a certain type happen, Unity passes control to Scripts by calling the corresponding function. These are called Event Functions \tn % Row Count 26 (+ 26) % Row 1 \SetRowColor{white} Event Functions & Callback functions that are called by Unity when certain events occur. Event Functions are provided as methods of the MonoBehaviour class, from which the classes in every Script inherit \tn % Row Count 36 (+ 10) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Code Flow and Events (cont)}} \tn % Row 2 \SetRowColor{LightBackground} Trigger Collider & Checkbox you can tick in a Collider Component. If active, the object will emit a {\emph{trigger event}} when in contact with something. Triggers are used for non-physical collisions (e.g. detecting when someone enters) \tn % Row Count 11 (+ 11) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{MonoBehaviour Event Functions}}} \tn % Row Count 12 (+ 1) % Row 4 \SetRowColor{LightBackground} `Awake()` & Called once before everything else \tn % Row Count 14 (+ 2) % Row 5 \SetRowColor{white} `Start()` & Called once after all Awakes, before any Update \tn % Row Count 17 (+ 3) % Row 6 \SetRowColor{LightBackground} `Update()` & Code that changes the position, state, behavior of objects in game. It is called before each frame is rendered. Updates happen at every iteration of the Game Loop, therefore each update may take a different time. The variable `Time.deltaTime` always contains the duration of the last update iteration \tn % Row Count 32 (+ 15) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Code Flow and Events (cont)}} \tn % Row 7 \SetRowColor{LightBackground} `FixedUpdate()` & Called before each physics update. The physics engine updates in time steps of fixed duration, therefore you don't have to correct for deltaTimes when moving things inside FixedUpdate. Place physics calls inside this \tn % Row Count 11 (+ 11) % Row 8 \SetRowColor{white} `OnMouse***()` & Called when there is a mouse event. *** can be: `Down`, `Enter`, `Exit`, `Over`, `Up`, `UpAsButton` \tn % Row Count 17 (+ 6) % Row 9 \SetRowColor{LightBackground} \seqsplit{`OnCollision***2D(Collision} {\emph{collision}})` & Called when the object is involved in a collision. *** can be: `Enter`, `Stay`, `Exit` (contact is made, held, or broken). The parameter contains info about the collision \tn % Row Count 26 (+ 9) % Row 10 \SetRowColor{white} \seqsplit{`OnTrigger***2D(Collider} {\emph{other}})` & Called when the object is involved in a collision, only if the object's collider is configured as a Trigger. *** can be: `Enter`, `Stay`, `Exit`. The parameter is the other object's collider \tn % Row Count 36 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Unity game loop: \seqsplit{https://docs.unity3d.com/uploads/Main/monobehaviour\_flowchart.svg}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Input}} \tn % Row 0 \SetRowColor{LightBackground} Input Manager & Access in the editor by Edit \textgreater{} Project Settings \textgreater{} Input. Contains the properties of the Axes \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} Axes & Axes are virtual directions ("Horizontal", "Vertical", "Jump", "Fire1",...) that represent possible inputs. Each Axis has a name, and one or two buttons that are mapped onto the {\emph{Positive direction}} and the {\emph{Negative direction}} (e.g. "Fire1" only has a button for the Positive input, since there is no concept of {\emph{firing backwards}}) \tn % Row Count 22 (+ 17) % Row 2 \SetRowColor{LightBackground} Getting Input & When a player presses an axis button, Unity will set the axis state to a value between -1 and 1 (-1 negative, 1 positive, 0 when there is no input). Get the input by querying Axes. Alternatively, you can query using button names (Keys) \tn % Row Count 34 (+ 12) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Input (cont)}} \tn % Row 3 \SetRowColor{LightBackground} `{\emph{value}} = Input.GetAxis({\emph{"Horizontal"}});` & Retrieves the current state for the {\emph{"Horizontal"}} Axis \tn % Row Count 3 (+ 3) % Row 4 \SetRowColor{white} `{\emph{value}} = Input.GetKey({\emph{"a"}});` & Returns `true` if the user is holding down the key {\emph{"a"}} \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{API}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{MonoBehaviour}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `MonoBehaviour` & Each class inherits from MonoBehaviour, inheriting members and methods that should be used for most functionalities in the Scripts \tn % Row Count 8 (+ 7) % Row 2 \SetRowColor{LightBackground} `gameObject` & This Game Object (the Game Object to which the Script is attached) \tn % Row Count 12 (+ 4) % Row 3 \SetRowColor{white} `tag` & This Game Object's Tag \tn % Row Count 14 (+ 2) % Row 4 \SetRowColor{LightBackground} `transform` & This Game Object's Transform \tn % Row Count 16 (+ 2) % Row 5 \SetRowColor{white} `GetComponent\textless{}{\emph{Type}}\textgreater{}()` & Returns the component of type {\emph{Type}} contained in this Game Object \tn % Row Count 20 (+ 4) % Row 6 \SetRowColor{LightBackground} `GetComponents\textless{}{\emph{Type}}\textgreater{}()` & Returns all components of type {\emph{Type}} \tn % Row Count 22 (+ 2) % Row 7 \SetRowColor{white} `Destroy(Object {\emph{obj}}, float {\emph{t}} = 0.0f)`)` & Removes something (after {\emph{t}} seconds). \seqsplit{`Destroy(gameObject)`} destroys the Game Object attached to the Script. `Destroy(this)` destroys the Script Component itself. `Destroy(GetComponent\textless{}...\textgreater{}())` destroys another Component \tn % Row Count 34 (+ 12) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{API (cont)}} \tn % Row 8 \SetRowColor{LightBackground} `Instantiate(Object {\emph{original}})` & Clones a GameObject or Component and returns the clone. If you clone a Component, the whole GameObject it is attached to will also be cloned \tn % Row Count 7 (+ 7) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{GameObject}}} \tn % Row Count 8 (+ 1) % Row 10 \SetRowColor{LightBackground} `GameObject` & Base class for all entities in Unity Scenes \tn % Row Count 11 (+ 3) % Row 11 \SetRowColor{white} `tag` & Tag of this Game Object \tn % Row Count 13 (+ 2) % Row 12 \SetRowColor{LightBackground} `transform` & Transform of this Game Object \tn % Row Count 15 (+ 2) % Row 13 \SetRowColor{white} `SetActive(bool {\emph{value}})` & Activate/deactivate this Game Object \tn % Row Count 17 (+ 2) % Row 14 \SetRowColor{LightBackground} `GameObject.FindWithTag({\emph{"Tag"}})` & Static method that finds and returns the first Game Object with tag {\emph{Tag}} \tn % Row Count 21 (+ 4) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Component}}} \tn % Row Count 22 (+ 1) % Row 16 \SetRowColor{LightBackground} `Component` & Base class for everything attached to GameObjects. For every specifc component there is a class (with the same name as the Component), inheriting from this. Ex: `Transform` \tn % Row Count 31 (+ 9) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{API (cont)}} \tn % Row 17 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Transform}}} \tn % Row Count 1 (+ 1) % Row 18 \SetRowColor{white} `position` & Position as a `Vector3` \tn % Row Count 3 (+ 2) % Row 19 \SetRowColor{LightBackground} `rotation` & Rotation as a `Quaternion` \tn % Row Count 5 (+ 2) % Row 20 \SetRowColor{white} `Rotate(float {\emph{xAngle}}, float {\emph{yAngle}}, float {\emph{zAngle}})` & Rotate around X, Y, Z axis \tn % Row Count 8 (+ 3) % Row 21 \SetRowColor{LightBackground} `Translate(Vector3 {\emph{translation}})` & Moves position. Ex: \seqsplit{`transform.Translate(Vector3.forward*Time.deltaTime)`} \tn % Row Count 12 (+ 4) % Row 22 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Vector}}} \tn % Row Count 13 (+ 1) % Row 23 \SetRowColor{LightBackground} `new Vector2({\emph{x}},{\emph{y}})` & Create new 2D vector \tn % Row Count 15 (+ 2) % Row 24 \SetRowColor{white} `new Vector3({\emph{x}},{\emph{y}},{\emph{z}})` & Create new 3D vector. Transform's positions are always Vector3, even in 2D! (But you shouldn't set positions directly to move objects) \tn % Row Count 22 (+ 7) % Row 25 \SetRowColor{LightBackground} `v + u`, `v - u` & Sum/subtract two vectors \tn % Row Count 24 (+ 2) % Row 26 \SetRowColor{white} `v * 5`, `v / 5` & Multiply/divide a vector by a number \tn % Row Count 26 (+ 2) % Row 27 \SetRowColor{LightBackground} `{\emph{target}}.position - {\emph{player}}.position` & Vector representing the distance between the two objects's Transforms \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{API (cont)}} \tn % Row 28 \SetRowColor{LightBackground} `{\emph{v}}.magnitude` & Vector's length \tn % Row Count 1 (+ 1) % Row 29 \SetRowColor{white} `{\emph{v}}.normalized` & Vector with the same direction, but magnitude of 1 \tn % Row Count 4 (+ 3) % Row 30 \SetRowColor{LightBackground} `{\emph{v}}.x` & Access X component (same for Y and Z) \tn % Row Count 6 (+ 2) % Row 31 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Debugging}}} \tn % Row Count 7 (+ 1) % Row 32 \SetRowColor{LightBackground} \seqsplit{`Debug.Log(*"message");`} & Prints a messaget to the Console \tn % Row Count 9 (+ 2) % Row 33 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Rigidbody2D}}} \tn % Row Count 10 (+ 1) % Row 34 \SetRowColor{LightBackground} `mass` & The body's mass. You can access all the other properties that you see from the Inspector as well \tn % Row Count 15 (+ 5) % Row 35 \SetRowColor{white} `AddForce(Vector2 {\emph{force}})` & Apply a force to the Rigidbody. Use this inside `Update()` for a constantly applied force \tn % Row Count 20 (+ 5) % Row 36 \SetRowColor{LightBackground} `AddTorque(float {\emph{torque}})` & Add a torque (gives an angular acceleration) \tn % Row Count 23 (+ 3) % Row 37 \SetRowColor{white} \seqsplit{`MovePosition(Vector2} {\emph{position}})` & Quickly thrust towards a new position (tries to get there in the time of a physics update, but collisions or long distances may impair it). Use this in `FixedUpdate()` rather than in `Update()` \tn % Row Count 33 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Full scripting API: \seqsplit{https://docs.unity3d.com/ScriptReference/index.html} \newline Manual: \seqsplit{https://docs.unity3d.com/Manual/index.html} \newline 2D guides: \seqsplit{https://unity3d.com/learn/tutorials/s/2d-game-creation}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}