Graphics Reference
In-Depth Information
tmp=(int)aMillis;
mills=tmp.ToString();
if(mills.Length<2)
mills="0"+mills;
The function uses the .Length of each string to discover whether or not an extra '0'
should be prefixed to the time value. This is purely for aesthetics, making the time strings
consistently have two numbers even when its value is less than 10.
The final string is composed of a colon separator between each time value and then
returned like this:
// pull together a formatted string to return
timeString=minutes+":"+seconds+":"+mills;
return timeString;
}
At the very end of TimerClass.cs, a GetTime() function provides a way for other
scripts to process the value of currentTime:
public int GetTime ()
{
// remember to call UpdateTimer() before trying to use this
// function, otherwise the time value will not be up to date
return (int)(currentTime);
}
An example of how GetTime() may be used can be found in the Tank Battle game
source code provided with this topic (http://www.crcpress.com/product/isbn/9781466581401),
where a game controller script takes the return value from a timer's GetTime() function
and checks it to see whether the game has been running long enough to end.
4.3 Spawn Scripts
In many cases, using Unity's built-in Instantiate function would be enough for creat-
ing objects for a desktop computer-based game, but instantiating gameObjects in Unity
can be an expensive process, particularly noticeable on mobile devices. One common
method of getting around the processor hit is to use pooling. Pooling is where you have
a group of objects that you use and reuse without destroying them. Instead, pooled
objects are hidden rather than entirely removed from memory. With this method, the
memory is already allocated and the objects already exist but you are enabling and dis-
abling them rather than having the performance hit of creating new ones and deleting
unused ones.
Building a pool management system is beyond the scope of this topic, although cen-
tralizing the spawning system will make it easy to switch out spawning for a better solu-
tion in the future. There are a couple of pool managers available to buy from the Unity
Asset Store, or you may want to take a look at some free code to handle pooling from an
awesome website called Unity Patterns (http://unitypatterns.com/new-tool-objectpool/).
Search WWH ::




Custom Search