Game Development Reference
In-Depth Information
However, after doing this, on my machine, the frames per second went
from being about 30 to 33 fps to 45 to 47 fps. This is a significant speedup
for just a little bit of detective work. Of course this can be taken too far and
suddenly the textures turn to mush, so work with caution, but be aware of the
significant benefits of tweaking down textures.
Optimizing with Scripts
Writing fast code is a skill that is highly desired, but too rarely mastered. The
code that we have written for this project without a doubt are not the fastest
or most efficient solutions, but they are effective. Unfortunately, some of the
most efficient code can sometimes be difficult to interpret for the uninitiated.
So at the risk of slower code, our approach has been to focus on understanding
some core ideas of scripting and creating in-game functionality.
However, there are some general notes to keep in mind that we've largely
subscribed to as the scripts were created. Following is a recap of some ideas
for faster code.
Be Careful of Where Lookups Occur
Lookups can refer to things like GameObject.Find(“name of object”) or
GetComponent(“name of script”)—situations in which the script is telling Unity
to go find something before the script can complete the task. Placing this sort
of thing in a function Awake or a function Start works great—it means they fire
once. However, occasionally beginner scripters will include them in something
like a function Update. Included in an Update function means the script is telling
Unity to go out and find a particular object every frame of the game . That's an
awful lot of work for something that the script could do once and just remember.
Type the Variables
JavaScript allows for a variable to be simply declared (var myVariable;) without
indicating what type of thing will be contained in that variable. In our code for
this topic, we have been careful to assign types to variables that we declare
(GameObject, boolean, int, float, string). But when reading code others have
posted online, occasionally you'll see the lazy scripter who simply declares a
variable without declaring a type to save that little bit of extra typing it takes to
type it. When there is not a type assigned to a variable, Unity has to infer what
the type is and make some guesses at how to treat it. When you consider how
many variables we have declared even in our basic game, you can see how this
sort of guesswork can add up quickly for Unity and start to impact performance.
Don't Fire a Function or Activate an AI if You're Not Close Enough
to See It
One of the scripts on the steam prefab simply checks the distance between
the player and the steam object. If the player is too far away, the steam shuts
itself off. In Unity's documentation, they list the example of having an enemy
go to sleep when the player can't see him or be close enough to be aware
Search WWH ::




Custom Search