3d technologies for the web
Post on 21-Mar-2016
25 Views
Preview:
DESCRIPTION
TRANSCRIPT
UFCFSU-30-13D Technologies for the Web
3D Technologies for the Web
Declaring and Using Arrays in Unity 3D
UFCFS3-30-23D Technologies for the Web
Agenda
Why Use an Array Array Declaration in C# in Unity 3D Searching and Sorting an Array Parallel Arrays Arrays of Data Array Access Errors
UFCFS3-30-23D Technologies for the Web
Need to Store Five Numbers
Use individual variables for each number stored:
int numberOne;int numberTwo;int numberThree;int numberFour;int numberFive;
messy - have to process as individual items ratherthan as a collection of grouped items. Solution - use an Array - contiguous data storage
UFCFS3-30-23D Technologies for the Web
Arrays and Data Storage
15
95
14
70
23
Elements
The value stored at array element number 2 is 14
Arrays store values in ‘elements’ (compartments)i.e. contiguous memory locations
0
3
2
4
1
UFCFS3-30-23D Technologies for the Web
Arrays and Data Storage
15
95
14
70
23
Elements
The array has 5 elements 0..4
The value stored at array element number 4 is 23
Array element numbering starts at 0
0
3
2
4
1
Thus the size of the array is 5
UFCFS3-30-23D Technologies for the Web
Arrays and Data Types
15
23
14
70
5
An Array ofnumbers (integers)
Gibson
Fender
Aria
Martin
Array of stringsLes Paul
UFCFS3-30-23D Technologies for the Web
Declaring Arrays in C#// create a new array
int[] numberArray = new int[5];// store the numbers into the array// 15 95 14 70 23
numberArray[0] = 15;numberArray[1] = 95;numberArray[2] = 14;numberArray[3] = 70;numberArray[4] = 23;
UFCFS3-30-23D Technologies for the Web
Array Declaration and Assignment in Unity 3d
String[] nameArray = new string[5];
void Start () {
nameArray[0] = "Hello";
nameArray[1] = "from";
nameArray[2] = "the";
nameArray[3] = "Name";
nameArray[4] = "Array";
}
UFCFS3-30-23D Technologies for the Web
Accessing Array Elements in Unity 3d
// iterate through the array for (int index = 0; index <= 4; index++) {
Debug.Log(nameArray[index]);}
UFCFS3-30-23D Technologies for the Web
Typical Aspects of using Arrays
• Search an array for a given value• Sort an array in a given order• Find the size of the array • Use the length method in searching algorithms• Compare the contents of one array with another array
UFCFS3-30-23D Technologies for the Web
// total the numbers stored in the number arrayint index;int sum;sum = 0;
for (index = 0; index <= 4; index = index + 1){sum = sum + numberArray[index]
}
Debug.Log("The sum of the numbers in the array is " + sum)
Summing the Values of an Array
UFCFS3-30-23D Technologies for the Web
Summing the Values of an Array 2
// total the numbers stored in the number array using array// length method
int index;int sum;sum = 0;
for (index = 0; index <= numberArray.length; index++){sum = sum + numberArray[index]
}Debug.Log("The sum of the numbers in the array is " + sum)
UFCFS3-30-23D Technologies for the Web
Parallel Arrays
jfg@adobe.com
nameJohn
Ben
Freda
Sue
Mike
mkf@audodesk.com
sue@unity.com
ben@uwe.ac.uk
freda@unity.com
UFCFS3-30-23D Technologies for the Web
Parallel Array Search
jfg@adobe.com
nameJohn
Ben
Freda
Sue
Mike
mkf@audodesk.com
sue@unity.com
ben@uwe.ac.uk
freda@unity.com
Search on name
Extract email on name
UFCFS3-30-23D Technologies for the Web
Parallel Array Search
jfg@adobe.com
nameJohn
Ben
Freda
Sue
Mike
mkf@audodesk.com
sue@unity.com
ben@uwe.ac.uk
freda@unity.com
Search on name
Find occurrences on name of email
UFCFS3-30-23D Technologies for the Web
Arrays of Data (sounds)Array of .mp3 audio files
footsteps.mp3
shell.mp3
siren.mp3
alarm.mp3
ambience.mp3
UFCFS3-30-23D Technologies for the Web
Array of Data (sounds)footsteps.mp3
shell.mp3
siren.mp3
alarm.mp3
ambience.mp3
Use code to produce random sound effect in 3D environment
UFCFS3-30-23D Technologies for the Web
Example of Accessing Random Array Elements in Unity 3d
void Update () {
// use Random.Range function that matches array sizegetRandomName = Random.Range(0.0, 4.0);// convert the decimal random value to an integerindex = Mathf.RoundToInt(getRandomName);// use the value to access a random array elementDebug.Log(nameArray[index]);
}
UFCFS3-30-23D Technologies for the Web
Beware! The classic runtime error is where a program tries to access an array element beyond the array’s size.
getRandomName = Random.Range(0.0, 5.0);
The array has only five elements, but the code is attempting to access an element number six, which does not exist.
The computer speak is the code has ‘fallen off the end of the array’
UFCFS3-30-23D Technologies for the Web
Summary
• Arrays are a fundamental structured data type in any programming language
• Arrays store values usually of the same type in a contiguous way which allows the data to be accessed and manipulated as a collection of data
• Typical array access is to search and sort the contents• Associated data may be held in parallel arrays and use cross
referencing to extract the required information• Algorithms that manipulate arrays must stay within the array
bounds or runtime errors will occur causing the program to ‘freeze’ or ‘crash’.
top related