+ All Categories
Home > Documents > UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

Date post: 14-Dec-2015
Category:
Upload: maggie-skillman
View: 220 times
Download: 2 times
Share this document with a friend
Popular Tags:
20
UFCFSU-30-1 3D Technologies for the Web 3D Technologies for the Web Declaring and Using Arrays in Unity 3D
Transcript
Page 1: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

UFCFSU-30-13D Technologies for the Web

3D Technologies for the Web

Declaring and Using Arrays in Unity 3D

Page 2: UFCFSU-30-13D 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

Page 3: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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

Page 4: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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

Page 5: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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

Page 6: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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

Page 7: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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;

Page 8: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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

}

Page 9: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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]);

}

Page 10: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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

Page 11: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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

Page 12: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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)

Page 13: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

UFCFS3-30-23D Technologies for the Web

Parallel Arrays

[email protected]

name

John

Ben

Freda

Sue

Mike

email

[email protected]

[email protected]

[email protected]

[email protected]

Page 14: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

UFCFS3-30-23D Technologies for the Web

Parallel Array Search

[email protected]

name

John

Ben

Freda

Sue

Mike

email

[email protected]

[email protected]

[email protected]

[email protected]

Search on name

Extract email on name

Page 15: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

UFCFS3-30-23D Technologies for the Web

Parallel Array Search

[email protected]

name

John

Ben

Freda

Sue

Mike

email

[email protected]

[email protected]

[email protected]

[email protected]

Search on name

Find occurrences on name of email

Page 16: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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

Page 17: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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

Page 18: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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 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]);

}

Page 19: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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’

Page 20: UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

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


Recommended