0% found this document useful (0 votes)
7 views

4.1 Game Coding Foundation Presentation_26.07.2023

Module 4 covers foundational game coding concepts using C#, focusing on Unity-specific elements such as scenes, GameObjects, and components. It emphasizes the importance of understanding C# basics, Unity APIs, and scripting best practices for effective game development. The module includes practical tutorials and guides for applying these concepts in Unity.

Uploaded by

midare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

4.1 Game Coding Foundation Presentation_26.07.2023

Module 4 covers foundational game coding concepts using C#, focusing on Unity-specific elements such as scenes, GameObjects, and components. It emphasizes the importance of understanding C# basics, Unity APIs, and scripting best practices for effective game development. The module includes practical tutorials and guides for applying these concepts in Unity.

Uploaded by

midare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Module 4

4.1 Game coding foundation


1. C# Basics
2. Unity-specific concepts
3. Unity APIs and scripting
4. Scripting best practices
5. Practical tutorial guide

CRICOS provider number: 00122A | RTO Code: 3046


1. C# basics
1. C# Basics
What is C#? Main C# concepts: Visual Studio
Developed by Microsoft. • Class Is the preferred editor
Powerful, object- • Variables & data types for Unity.
orientated programming • Operators Ensure that you install the
language. Visual Studio (free version)
• Conditional statements when you install Unity.
Widely used in software
development including • Functions.
games developed in Unity.
Rich set of features for
creating robust and
efficient applications.
1. C# Basics
Class public class PlayerController : MonoBehaviour

Fundamental building {
block.
Its purpose is // variables
to encapsulates data and // functions / methods
behaviour. // event handlers

Define the structure, }


properties and methods
that objects of that class
will have.
1. C# Basics
Variables int: Represents integers (whole numbers) without decimal
Used to store and points, such as 1, 2, -5, etc.
manipulate data.
int currentIndex;
They hold a variety of data.
float: Represents floating-point numbers with decimal places,
Required to have specific
data types assigned to such as 3.14, -0.5, etc.
them when they are float score;
created: string: Represents a sequence of characters, such as
• Number (integers or "Hello", "Unity", etc.
floats)
string DrinkOrder;
• String (words)
bool: Represents a Boolean value, either true or false.
• True or false
(Booleans). bool isAllowed;
1. C# Basics
Arrays string DrinksOrder[“Water”, “Apple Juice”, “Milk””];

Are lists are used for


storing and manipulating
multiple values.

Arrays need to be type-


casted to inform what type
of data they will hold.

Same as standard
variable.
1. C# Basics
Operators Arithmetic operators (+, -, *, /),
Allow you to perform Comparison operators (==, !=, <, >),
various operations on
Logical operators (&&, ||, !),
variables and values.
Assignment operators (=, +=, -=), and more. O
Enable you to perform
calculations, make // lives is equal to 3
comparisons and control lives = 3
program flow.
// Compare is DrinksOrder Water
DrinksOrder == “Water”
// isAllowed is false
!isAllowed
// score is equal to itself plus 1
float score += 1;
1. C# Basics
Conditional statements If-else Switch
The most commonly used if(lives == 0){ switch (object){
conditional statements is if- // then end the game case “collectible”:
else and the switch gameEnd();
statement. score += 1;
} break;
The switch statement case “enemy”:
provides multiple cases and lives -= 1;
executes the block of code
associated with the matching break;
case. }
1. C# Basics
Loops for (int i = 1; i <= 5; i++)
Used to execute a block of {
code repeatedly.
// repeat this command until i becomes
The for loop repeats a block
greater than or = to 5
of code a specific number of
times for as long as the }
condition is met.
The while loop repeats a
block of code as long as a while (isAllowed)
specific condition remains {
true.
// Code do this command for as long as
it is true
}
1. C# Basics
Functions/Methods void CheckLives(){
Functions (also called
methods) in C# allow you to // check to see if all lives are gone
encapsulate reusable blocks
of code.
}
They accept input
parameters, perform specific
operations and return values
(if needed).
1. C# Basics
Event handlers private void OnCollisionEnter2D(Collision2D collision)
A method that allows you to {
define custom behaviour that // add in commands for when we hit a specific item
should execute when a }
particular event is triggered.

private void OnCollisionExit2D(Collision2D collision)


Unity provides a variety of
{
built-in events that you can
subscribe to and handle using // add in commands for moving away from a specific item
event handlers. }
2. Unity-specific concepts
2. Unity-specific concepts
Scenes
In Unity we use Scenes to
be the building blocks of
your game. To create a new scene
A Scene can represent
specific levels, menus or
environments.
In the Unity editor, you
can create, modify and
organise scenes, allowing
you to structure and
control the flow of your
game.
Organise scenes in your project window
2. Unity-specific concepts
A variety of Game Objects can be
GameObjects placed into the scene hierarchy.

GameObjects make up
the objects in your game.
They will be the base for
characters, objects and
environments.
A GameObject serves as
a container that holds
components and defines
the properties, behaviours
and relationships of the
objects in your scene. Components
attached to this
GameObject
2. Unity-specific concepts
Components
Components are modular
pieces of functionality that
you attach to
GameObjects.
They provide specific
behaviours or features to
the GameObject they are
attached to.

This player has SpriteRender,


BoxCollider2D, RigidBody2D,
PlayerMovement and
Animator components
attached to it.
2. Unity-specific concepts
Manipulating // create a variable to hold the component
GameObjects using C# you want to access
code GameObject Player;
By obtaining references to Animator Player_ani;
GameObjects through code,
you can perform operations Collider2D Player_coll;
like: RigidBody2D Player_rg;
- moving objects
- activating or deactivating // to access you need to GET the component
them ThePlayer.GetComponent<Animator>();
- changing their appearance
- interacting with other // Access the animator and tell it when to play
game elements.
ThePlayer.enabled = false;
2. Unity-specific concepts
Unity-specific functions Start(): This function is called once when a script is first
enabled or when a GameObject is instantiated.

Start() void Start() {


}
Update()
FixedUpdate() Update():
The Update() function is called every frame of the game. It is
commonly used for updating game logic or handling user inputs.
When you create a script in
void Update() {
Unity, you create a class that
has set of special functions }
already pre-loaded into the FixedUpdate():
script.
The FixedUpdate() function is called at a fixed rate, typically used
for physics-related calculations.
void FixedUpdate() {
Read more about these on Canvas.
}
3. Unity APIs and scripting
3. Unity APIs and scripting
APIs (Application using System.Collections;
Programming Interfaces) using System.Collections.Generic;

API stands for Application using UnityEngine;


Programming Interface. using UnityEngine.UI;
using TMPro;
It is a set of rules and
protocols that allow different using UnityEngine.SceneManagement;
software applications to
communicate with each other.
In the context of Unity, APIs
provide a way to access and
interact with the various
features and functionalities
offered by the Unity game
engine.
3. Unity APIs and scripting
https://docs.unity3d.com/ScriptReference/
Importance of documentation
and online resources:
Unity provides comprehensive
and up-to-date documentation
that explains each API's https://docs.unity.com/
purpose, usage and related
concepts.
Unity Manual: Screenshot taken 15 June 2023
Documentation helps
developers quickly learn and
reference the APIs, enabling https://learn.unity.com/
them to implement desired
features and functionalities
Unity Documentation: Screenshot taken 15 June 2023
efficiently.
• https://docs.unity.com/
• https://docs.unity3d.com/Scr
iptReference/
• https://learn.unity.com/ Unity Learn: Screenshot taken 15 June 2023
4. Scripting best practices
4. Scripting best practices
Be consistent in naming Is it in the syntax? Read the error message
All naming is case sensitive. Have a strong understanding of The console panel in Unity is
how the { } are used and when your friend.
to use a ; and when to not. Window > General > Console

Use a debugger Isolate the issue Test your code consistently


Many scripting programs What part of the code is not Test your code line by line or
have inbuilt de-buggers that will triggering? section by section.
show you possible solutions. Don't wait to the end to test.

You might also like