200 likes | 304 Views
3D Technologies for the Web. Declaring and Using Arrays in Unity 3D. Agenda. Why Use an Array Array Declaration in JavaScript and in Unity 3D Searching and Sorting an Array Parallel Arrays Arrays of Data Array Access Errors. Need to Store Five Numbers.
E N D
3D Technologies for the Web Declaring and Using Arrays in Unity 3D
Agenda • Why Use an Array • Array Declaration in JavaScript and in Unity 3D • Searching and Sorting an Array • Parallel Arrays • Arrays of Data • Array Access Errors
Need to Store Five Numbers Use individual variables for each number stored: varnumberOne:number varnumberTwo:number varnumberThree:number varnumberFour:number varnumberFive:number messy - have to process as individual items rather than as a collection of grouped items. Solution - use an Array - contiguous data storage
Arrays and Data Storage Elements Arrays store values in ‘elements’ (compartments) i.e. contiguous memory locations 0 15 1 95 The value stored at array element number 2 is 14 2 14 3 70 23 4
Arrays and Data Storage Elements Array element numbering starts at 0 0 15 1 95 The value stored at array element number 4 is 23 2 14 The array has 5 elements 0..4 3 Thus the size of the array is 5 70 23 4
Arrays and Data Types Gibson 15 Les Paul 23 An Array ofnumbers (integers) Array of strings Martin 14 Fender 70 5 Aria
Declaring Arrays in JavaScript // create a new array varnumberArray = new Array(); // 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;
Array Declaration and Assignment in Unity 3d var nameArray = new Array (); function Start () { nameArray[0] = "Hello"; nameArray[1] = "from"; nameArray[2] = "the"; nameArray[3] = "Name"; nameArray[4] = "Array"; }
Accessing Array Elements in Unity 3d // iterate through the array for (var index = 0; index <= 4; index++) { Debug.Log(nameArray[index]); }
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
Summing the Values of an Array // total the numbers stored in the number array varindex:Number; varsum:Number; sum = 0; for (index = 0; index <= 4; index = index + 1){ sum = sum + numberArray[index] } trace("The sum of the numbers in the array is " + sum)
Summing the Values of an Array 2 // total the numbers stored in the number array using array // length method varindex:Number; varsum:Number; sum = 0; for (index = 0; index <= numberArray.length; index++){ sum = sum + numberArray[index] } trace("The sum of the numbers in the array is " + sum)
Parallel Arrays name email jfg@adobe.com John Mike mkf@audodesk.com Sue sue@unity.com ben@uwe.ac.uk Ben Freda freda@unity.com
Parallel Array Search name email Search on name jfg@adobe.com John Mike mkf@audodesk.com Sue sue@unity.com Extract email on name ben@uwe.ac.uk Ben Freda freda@unity.com
Parallel Array Search name email Search on name jfg@adobe.com John Mike mkf@audodesk.com Sue sue@unity.com Find occurrences on name of email ben@uwe.ac.uk Ben Freda freda@unity.com
Arrays of Data (sounds) Array of .mp3 audio files footsteps.mp3 shell.mp3 siren.mp3 alarm.mp3 ambience.mp3
Array of Data (sounds) footsteps.mp3 Use code to produce random sound effect in 3D environment shell.mp3 siren.mp3 alarm.mp3 ambience.mp3
Example of Accessing Random Array Elements in Unity 3d function Update () { // use Random.Range function that matches array size getRandomName = Random.Range(0.0, 4.0); // convert the decimal random value to an integer index = Mathf.RoundToInt(getRandomName); // use the value to access a random array element Debug.Log(nameArray[index]); }
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’
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’.