1 / 20

3D Technologies for the Web

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.

caron
Télécharger la présentation

3D Technologies for the Web

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3D Technologies for the Web Declaring and Using Arrays in Unity 3D

  2. 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

  3. 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

  4. 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

  5. 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

  6. Arrays and Data Types Gibson 15 Les Paul 23 An Array ofnumbers (integers) Array of strings Martin 14 Fender 70 5 Aria

  7. 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;

  8. 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"; }

  9. Accessing Array Elements in Unity 3d // iterate through the array for (var index = 0; index <= 4; index++) { Debug.Log(nameArray[index]); }

  10. 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

  11. 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)

  12. 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)

  13. 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

  14. 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

  15. 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

  16. Arrays of Data (sounds) Array of .mp3 audio files footsteps.mp3 shell.mp3 siren.mp3 alarm.mp3 ambience.mp3

  17. Array of Data (sounds) footsteps.mp3 Use code to produce random sound effect in 3D environment shell.mp3 siren.mp3 alarm.mp3 ambience.mp3

  18. 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]); }

  19. 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’

  20. 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’.

More Related