Author Topic: Replace all label fonts  (Read 13241 times)

mossman7777

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 1
    • View Profile
Replace all label fonts
« on: October 10, 2014, 10:55:10 AM »
I had some trouble where on one Android, a Nexus 7, my system embeded arial font wasn't resolving and all my text was invisible. Then I found an arial.ttf font file and added it to my assets. Then all the labels in my interface lost their font setting. I was sadness.

It's easy to write something to replace all the labels with a new font at run time. But I wanted to do this in the Unity Editor. So with a little work, I came up with something. Just sharing in case someone else needs it. Perhaps it already exists? I don't know. I couldn't find one.

So to use this:
1.) create a Resource folder in your Assets folder in your project.
2.) drop a .ttf true type font file in the Resource folder
3.) paste this snippet into an empty c# script
4.) modify this script to replace the yourFontName with the one you want to replace. Don't include the .ttf extension.
5.) use the new menu option in MyMenu to replace all your fonts!
6.) be happiness

  1. using System;
  2. using UnityEngine;
  3.  
  4. #if UNITY_EDITOR
  5.         using UnityEditor;
  6. #endif
  7.  
  8. public class MyMenu : MonoBehaviour
  9. {
  10.         #if UNITY_EDITOR
  11.                 // Add a menu item named "Do Something" to MyMenu in the menu bar.
  12.                 [MenuItem ("MyMenu/Replace All Label Fonts")]
  13.                 static void replaceAllFonts()
  14.                 {
  15.                         UILabel[] labels = GameObject.Find("UI Root").GetComponentsInChildren<UILabel> (true);
  16.                        
  17.                         Debug.Log("We found :" + labels.Length + " labels.");
  18.                        
  19.                         Font go = Resources.Load("yourFontName") as Font;
  20.                        
  21.                         if(go == null)
  22.                                 Debug.LogWarning("We failed to load font.");
  23.                         else
  24.                         {
  25.                                 Debug.Log("Loaded font.");
  26.                                
  27.                                 foreach (UILabel l in labels)
  28.                                 {
  29.                                         l.trueTypeFont = go;
  30.                                 }
  31.  
  32.                                 Debug.Log("success!");
  33.                         }
  34.                 }
  35.         #endif
  36. }
  37.  

Blyzard

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Replace all label fonts
« Reply #1 on: October 16, 2014, 08:35:19 PM »
Simple but very usefull script !

Thanks a lot !

lishuen

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Replace all label fonts
« Reply #2 on: October 23, 2014, 06:49:53 AM »
This is what I want.Thanks for sharing~!

ArvoAnimi

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Replace all label fonts
« Reply #3 on: February 03, 2015, 09:03:20 AM »
Hey guys any idea on how to go with this if I want to change an NGUI font to a Unity one? Bitmap to dynamic.
Gotta change them but it would be a drag to go at it manually.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Replace all label fonts
« Reply #4 on: February 03, 2015, 11:04:01 AM »
Select the bitmap font's prefab and just change it there.

ArvoAnimi

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Replace all label fonts
« Reply #5 on: February 03, 2015, 11:13:42 AM »
(PALMFACE) That works.