Graphics Reference
In-Depth Information
When you start a Unity project and the screen resolution box appears, there is a drop-
down menu for the quality settings, usually something along the lines of Fastest, Fast,
Simple, Good, Beautiful, and Fantastic (the default names and quality types). You can
modify these in the Unity Inspector window by going through the menus Edit → Project
Settings → Quality, deleting or adding new settings levels as you see fit. What this means is
that there is no guarantee of there being the same number of quality settings from game
to game—for example, you may choose to only have Good, Bad, and Ugly. To find out how
many settings there are, QualitySettings.names is used to return an array containing all
of the available quality names, and its .Length property tells us how many items are in the
array. This number of available quality settings is held in the variable named detailLevels.
In the Start() function, the code to do this looks like this:
string[] names = QualitySettings.names;
detailLevels= names.Length;
By default, the function also uses the highest quality level as a default setting for qual-
ity when no PlayerPrefs key is found.
graphicsSliderValue= detailLevels;
The actual graphics quality level (QualitySettings.SetQualityLevel) is set in the last
line of the function:
QualitySettings.SetQualityLevel( (int)graphicsSliderValue, true);
Next up, we move straight on to the OnGUI function:
void OnGUI()
{
float resX = Screen.width / default_width;
float resY = Screen.height / default _height;
GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0),
Quaternion.identity, new Vector3 (resX, resY, 1));
The Matrix 4 × 4 function is complicated, and I am not about to try to explain it here
(trust me, I am no mathematician!), but the simplest explanation I have is as follows.
The default width and height are the resolution at which you designed the UI for (so,
if you intend the default screen resolution to be 1024 × 768, you would make the default_
width and default_height variables equal to 1024 and 768, respectively).
The multipliers to use with the GUI.matrix function are established by taking the
width and height of the screen and dividing it by your default width and height values.
The line that does all the hard work is where Unity sets the GUI.matrix. It takes your val-
ues and stretches the UI to fill the screen. This, of course, means that there will be some
stretching at different aspect ratios, but the UI will at least scale to suit every resolution.
It is probably the easiest way to deal with multiple screen resolutions without having to
design multiple user interfaces.
Search WWH ::




Custom Search