Download presentation
Presentation is loading. Please wait.
Published bySteven Pearson Modified over 6 years ago
1
Data Collections, Loops, Instantiation and some pieces that fell off.
Week 7 Data Collections, Loops, Instantiation and some pieces that fell off.
2
Contents 17 Slides Projects and scenes
Data Structures/ Collections: Arrays, Lists, Queue & Hashtables Loops GameObjects Instantiate Timers Tags
3
Projects and scenes using UnityEngine.SceneManagement;
In Unity, A project is a game, and a scene is a level. Most games have multiple levels, and things like title screens that may dictate which level will be loaded. To change between scenes in our game, we have to use the SceneManger by adding: using UnityEngine.SceneManagement; In our script, we can call SceneManager.LoadScene(“SceneName") on a button click. We can address the level by name, or by it’s build level number. We can reload the level we are on by typing SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); TIP: Keep in mind, computers count from 0, AND the start menu scene also counts as a level.. But how do we set that up in Unity? ?? (Hint: Next Slide)
4
Building your Game Go to FILE --> BUILD AND RUN
Add your levels (Scenes) by using Drap and Drop (Notice the scene numbers, starting from 0) Select your output. Mobile will need plug-ins. In Player settings you can add your company name, game name, logo, icon and build number. Build and run will let you play immediately!
5
Data Structures/ Collections
ARRAYS, LISTS, QUEUES and HASHTABLES are DATA STRUCTURES. You will also hear them referred to as COLLECTIONS. (Hashtables are becoming obsolete in Unity) DATA STRUCTURES: (From Wikipedia:) Consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula In translation : Lists and Arrays work like a grocery list and a bookcase. Most Languages / variations constrain you to a single Data Type per Collection. TIP: We need to add a library to use a list: using System.Collections.Generic;
6
Arrays and Lists The Differences between Arrays and Lists:
Lists must be read sequentially by the computer, until it finds what it wants, but are easy to add and remove information from. Arrays are easier to search, but generally are of a fixed size. New items cannot be added, and empty items just waste resources. Many Languages now have variations of the 2, which are easier to use, size and search through. Each position in the List or Array is called an INDEX, and the count begins from 0. You can use theses Indices (Plural of Index) to find an Item or check what’s at a certain position. Add or remove item(s) from different positions Do Something to the list as a whole. ITERATE through the list: Do something to each item in order. (For Each Loop) Search the list for a match to a String, Number, Object, etc Count the items It contains PERMUTATION is switching the position of 2 items. (Permutation means the act of rearranging something.) A Hashtable runs information in pairs of information called "Key" and "Value".
7
Array Activities Bacon More Bacon Extra Bacon Toast Index 0 Index 1
They put everything in an order. Each position in the order is called an INDEX, and the count begins from 0. You can use theses Indices (Plural of Index) to find an Item or check what’s at a certain position. Do Something to the list as a whole. ITERATE through the list: Do something to each item in order. Search the list for a match to a String, Number, Object, etc Count the items It contains PERMUTATION is switching the position of 2 items. (Permutation means the act of changing or rearranging something.) Example: This array Contains 4 items, starting at Index 0 and ending with Index 3. We could ask : What is in Index 2, and return “Extra Bacon”. We could say: For each item together, Put in the cart, Or For each Item, Check price. We could use Permutation and swap Toast to Bacon. The new order would be Toast, More Bacon, Extra Bacon, Bacon. Bacon More Bacon Extra Bacon Toast Index 0 Index 1 Index 2 Index 3
8
Plagiarized from the internet:
A Collection is a container for grouping objects together. A List is an ordered Collection of objects. A Queue is a Collection of objects accessed in the order they're inserted. An ArrayList is a List of objects whose elements can be accessed randomly.
9
Quickie on Queues Queues are another useful data collection, but with some slightly different rules. Queues need another namespace: using System.Collections.Generic Queues are declared as: public/private Queue <Type> name; Queues are FIRST IN- FIRST OUT (FIFO). Think of a DMV line, the person in front (First) is the first to get helped and leave Queues can use: Enqueue: Put’s something at the End-Of-Queue Dequeue: Removes the first item, because it’s Done-With-Queue Peek: Views the first items paperwork, does nothing about it, and goes to lunch.
10
Moar List<GameObject> enemies = new List<GameObject>();
Type Pros Cons Array Fastest if you have a fixed or maximum size. BuiltIn Arrays Shows in Unity Inspector Window. Generally a fixed size (The Builtin Array can change size) Declaration EG: GameObject[] enemies = new GameObject[16]; string[] weapons = new string[] { "Sword", "Knife", "Gun" }; Lists Dynamically Resizable Slower than Builtin Array, faster than ArrayLIst. Can only use single Data type per Generic List for performance reasons. Must add Library to script to use: using System.Collections.Generic; List<Type> myList = new List<Type>(); List<GameObject> enemies = new List<GameObject>(); Hashtable Can use 2 different values to pull information Obsolete in some programming languages or Unity Publishing types (IE windows store) Hashtable myHashtable = new Hashtable();
11
Loops: A loop is an example of CONDITIONAL EXECUTION, meaning it needs a condition to run. A loop runs a block of code over and over. Conditions can be while something is true( or that it is true that something is false.) or use a counter to create a set number of iterations. Infinite loops are bad (and crash Unity). Update(); and FixedUpdate(); are both loops. Their conditions are to run as long as the game runs.
12
Loops cont. Conditions can be checked BEFORE or AFTER the block of code runs, depending on the type of loop. Tip: Some of them have sneaky syntax. Check the snippets. Fun fact: Apple Computer's Address is " 1 Infinite Loop Cupertino, CA ". A loop runs a piece of code over and over. A loop is conditional execution, which means it needs a condition to run. This condition can be an amount of loops, when something happens, or while something happens. There are 4 major loop types in Unity WHILE LOOP: runs WHILE a condition is true Checks the condition BEFORE it runs DO WHILE LOOP: runs while a condition is true, but checks the condition AFTER it runs, so it will always run at least once. Notice the sneaky extra Semicolon here!! FOR LOOP: Best to use for a count, when the number of iterations is known. FOR EACH: iterating through collections, such as arrays or lists While Loop while (these instructions are true) { do these instructions; } Do while Loop do these instructions ; while (this condition is true ); For Loop for (these conditions, EG MyEnemies < NumEnemies; MyEnemies ++) Do these instructions; For Each Loop foreach (DataType item IN thisvariable)
13
EVENT HANDLING METHOD:
Example: The Event Listener “Listens” for the exciting event to happen: LISTENER: Is the Fire Button Down? Yup! Nope. EVENT HANDLING METHOD: Throw A Cabbage!! Ok then, I’ll Just sit here. Add 1 To the Loop Count Listen Again. Listen Again.
14
GameObjects GameObjects are containers
Every object in your game is a GameObject We make them do things by teaching them Behaviours (adding scripts) and adding components. Sometimes, we need an empty container to put a bunch of other game objects in. We can do that by going to gameobject Create Empty. We can then add components or other objects into the container Or use it as a bookmark in 3d space, such as a spawn point.
15
Instantiate To instantiate is to create a copy (Instance) of the blueprint (Class) We need the following arguments to use the instantiate instruction: What to Instantiate Where to put it (X, Y, Z ) How to Rotate it (X, Y, Z) We code it like so: Instantiate (WHAT THING, new Vector3 (X, Y, Z ), Quaternion.Euler (X, Y, Z )); Quaternion.Euler is for rotation and the (X, Y, Z ) coordinates are done in degrees. *Euler is pronounced like “Oiler” , but you know, fancier.
16
Time / Delta Time Since game engines think in frame rates, if we want it to use a real time, we have to specify that’s what we want. We do this by using Time.deltaTime If we want to make a timer, we have to use a FLOAT! EX: public float timeLeft = 25f; timeLeft -= Time.deltaTime;
17
Tags From Unity Manual Tags
A Tag is a word which you link to one or more GameObjects. For instance, you might define “Player” and “Enemy” Tags for player- controlled characters and non-player characters respectively; a “Collectable” Tag could be defined for items the player can collect in the Scene; and so on. Clearly, Tags are intended to identify GameObjects for scripting purposes. We can use them to write script code to find a GameObject by looking for any object that contains our desired Tag. This is achieved using the GameObject.FindWithTag() function. Basically, you can name and address a large group of game objects by using a tag. Tags can be things like enemies, power ups, cabbages, players, push-able cubes, etc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.