Switching fonts

We have set up a system to switch atlases at runtime depending on the screen's size. We can also use reference fonts to make sure we can easily switch multiple labels' fonts in one click.

ScreenFont

We will now create a fourth font, which will be a reference font, pointing to another one. We'll use it to change the font used for all labels very easily:

  1. In the Project view, go to the Assets/Resources/Fonts folder.
  2. Select the SmallLato font prefab file, as follows:
    ScreenFont
  3. Duplicate it with Ctrl + D.
  4. Rename the new duplicate from SmallLato 1 to ScreenFont.
  5. Select our new ScreenFont prefab file.

The Inspector view displays the following content:

ScreenFont

The ScreenFont prefab's Font Type (1) is currently set to Bitmap. Switch it to Reference now. The Inspector panel now looks like this:

ScreenFont

You might notice that all the font information has disappeared; this reference font currently points to nothing. Let's make it point to the MediumLato font by default:

  1. Click on the Font button (1).
  2. Select the MediumLato font in the pop-up window.

Good! Now, we should use ScreenFont instead of any other when we configure our label widgets. We have just configured it so that it points to the MediumLato font by default. We can now assign it to a new label.

Assigning ScreenFont

We have created our reference ScreenFont. We can now create a new label using it:

  1. Select our UI Root GameObject.
  2. Hit Alt + Shift + L to create a new child label.
  3. Set its Transform position to {0, -280, 0}.
  4. Rename UI Root | Label to SmallText2, and then:
    • Make sure its font type is set to NGUI
    • Set its Font value to ScreenFont
    • Set Font Size to 35
    • Set its Overflow value to ResizeFreely
    • Change its Text value to Paragraph Heading

Now, let's make sure that all labels except LargeText use new ScreenFont:

  1. Select both the UI Root | SmallText and MediumText GameObjects.
  2. Change their Font values to ScreenFont.

Now, all labels except the LargeText label are using ScreenFont, which currently points to the MediumLato font. Let's change the LargeText label to use the Coalition font:

  1. Select our UI Root | LargeText GameObject.
  2. Click on its Font button:
    • Click on the window's Show All button
    • Select the Coalition font

Good. Now, your Game view should look like this:

Assigning ScreenFont

The ScreenFont reference font assigned to the preceding labels 2, 3, and 4 allows us to change their font in only one simple step. Let's try it now:

  1. In the Project view, select our Assets/Resources/Fonts/ScreenFont prefab.
  2. For its UIFont component:
    • Click on the Font button
    • Click on the Show All button in the pop-up window
    • Select the SciFi Font – Normal font

And that's it! By changing the pointed font for the ScreenFont reference font, we have now changed the used font for all labels with ScreenFont assigned.

You should now have all labels, except LargeText, using the SciFi Font – Normal font as shown in the following screenshot:

Assigning ScreenFont

Great! Now, let's summarize what you've learned in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.212.217