Game Development Reference
In-Depth Information
Best Fit : This allows the text to resize the font to ensure it fits within the
bounds of the control. It also allows you to set minimum and maximum font
sizes if you want to constrain the scaling.
Color : The base color of the font (which can be overridden by Rich Text
formatting features), the default color of text is Black .
Material : The default material the text should be rendered with. Note that
this does not include Font atlases; these are not supported in the base Text
control (but you could extend it). This is also used to assign shaders.
A simple FPS control
The simplest text example that is used in most games is an FPS counter, so first
let's create a script for that. Start by creating a new C# script in your project called
FPSCounter and replace its contents with the following:
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class FPSCounter : MonoBehaviour {
}
We start off with the expectation that the control has a Text component attached to
the GameObject using the RequireComponent attribute, we also add the brand new
UnityEngine.UI namespace that holds all the new UI functionality.
Next, we'll add some simple properties to track the FPS for our script:
private Text textComponent;
private int frameCount = 0;
private float fps =0;
private float timeLeft = 0.5f;
private float timePassed = 0f;
private float updateInterval = 0.5f;
Nothing too fancy, and now we need to capture a reference to the Text
component used by the GameObject (always try to use strict references and
not to keep looking up the Text component reference on each use), we do this
in the Awake function as follows:
void Awake()
{
textComponent = GetComponent<Text>();
 
Search WWH ::




Custom Search