+1

Data storing in Unity

Unity is a 3D game engine with a purpose of providing low budget game studios a set of tools to make professional 3D games, this is a fact that well known by the game developers these days. Unity support 3 programing languages, being the most used by developers, C#.

Unity’s C# uses Mono, which is based on .Net Framework, so before stepping in becoming a pro as a Unity developer, it is worth to understand the basic and advanced C#. In this post, let’s dwell deeper into data storing.

Array

This build in, is the most basic type of array, limited by fixed size, can now be resized with function:

System.Array.Resize.(arrayname, size);

They are the fastest kind of array, making them the best choice for performance, especially if you are targeting iPhone. If you have decided on a number of items, this is going to be your best choice.

Also this built in array is good for determining a limited number of items, in example in a shooting game where you need only 50 bullets active on the scene, this is a good way to control the number of objects.

//Declaring
Type array = new Type [lengthOfArray];
//Declaring an array with a set of integers
int[] numbers = new int[10];
//Declaring an array with a set of game objects
GameObject[] objects = new GameObject[16];
//Get a number of objects in array with i index
int count = myArray.Length;
//Set a value for object with i index in arary
myArray[i] = newValue;
//Assign a new value with array index
TheType thisValue = myArray[i];
//Resize array index
System.Array.Resize(ref myArray, size);

Generic List

Generic List has dynamic size and support adding, getting and removing items. What makes it special from the rest is that you specify the type to be used once declare, the type of object the List will contain.

Once declaring is done, you can add only objects of the same type, due to this, there is no need for casting when you get them, also the performance is pretty good.

//Declare a list of integers
List<int> someNumbers = new List<int>();
//Declare a list of game objects
List<GameObject> enemies = new List<GameObject>();
//Add item to the list
myList.Add(theItem);
//Set an item in the list
myList[i] = newItem;
//Assign a value of list item
Type thisItem = List[i];
//Remove item in list
myList.RemoveAt(i);

Array VS List

Well, this is a fun subject, so you may know or not, but when you add an element an array, an empty copy of the array +1 element is created, then the data from the original array is copied into it, afterward the new data for the new element is then loaded. That doesn’t really sound resource friendly, right? But even then, array is a lot faster than that of list.

A test has shown that reading from a List takes 47% longer than reading from an array, that’s a really big gap, but not as big as writing where List takes 695% longer, with each of the tests involved 100 million reads or writes.

Now why list is so much slower? List implements the IList interface’s slow virtual function, not only that, all index values passed to read or write are checked to make sure they’re within the bounds. For reading this is just a simple if, for writing, there is both if and a non-virtual function call to CheckIndex which has another if with two comparisons in it and another possible exception.

In general, List is great to use being more convenient. Other than that, if you find yourself using them a lot of times per frame you should consider switching to array.

Hashtable

Hashtable is type of collection where each item is created from a key and value pair. It is used in situations where you need to search with a certain value. The information used to perform the search is the key, and returned object is a value.

It is usually used for situations where you want to quickly pick certain item from collection, using unique identifying key (in example string), similar to the way of selecting a record from database with index.

//Declare a hashtable
Hashtable myHashtable = new Hashtable();
//Assign a value with key to hashtable
myHashtable[anyKey] = newValue;
//Get a hashtable value, casting is needed
ValueType thisValue = (ValueType)myHashtable[theKey];
//Get a number of items in hashtable
int count = myHashtable.Count;
//Remove item in hashtable
myHashtable.Remove(theKey);

Generic Dictionary

Generic Dictionary like Hashtable provides a structure for looking up items from collection, but differs in that you have to specify the type for keys and value on declaring.

Due to this, it gets the same benefits to those of Generic List, which means no casting needed, and a better performance over Hashtable. It is also worth mentioned that generics collections are a lot faster as there's no boxing/unboxing. Definitely a better choice over Hashtable.

//Declare a dictionary with strings as key and value
Dictionary<string ,string> myDictionary = new Dictionary<string, string>();
//Assign a value to dictionary with key
myDictionary[anyKey] = newValue;
//Get a value out of dictionary with index
ValueType thisValue = myDictionary[theKey];
//Get a number of items in dictionary
int count = myDictionary.Count;
//Remove item from dictionary
myDictionary.Remove(theKey);

Too long; Didn’t read

Use built in array whenever you can, it has excellent memory efficiency and speed, also changing Lists to Arrays isn't hard in the cases where Arrays are good enough for the job.

If there is no specific reason, always try to use generic containers.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí