Author Topic: Font reference issue  (Read 7471 times)

Real World

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 15
    • View Profile
Font reference issue
« on: April 16, 2015, 06:18:10 AM »
I'm trying to setup a system for swapping the font used by all labels in my game based on the current language. I've created a font reference object and assigned it to all my labels. Should this font reference exist in the scene or as a prefab?
If I make it a prefab I can't alter the font the font reference points to at runtime.
If I add it to the scene then any UI labels that exist as prefabs and are added to my UI at runtime can't maintain a reference to the font reference object because prefabs don't know about objects in my scene.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Font reference issue
« Reply #1 on: April 16, 2015, 07:38:50 AM »
Exist in the scene? No. But it should be a prefab, yes. Instead of 1 prefab like you had before, you would have 2 font prefabs -- one would be a real one, the other one would simply reference the first. All labels would be using the second (reference) font.

To load it at run-time, place the prefab in Resources folder and use Resources.Load, or just reference it from some script that exists in your scene. In other words -- same as any other Unity prefab.

Real World

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: Font reference issue
« Reply #2 on: April 16, 2015, 08:41:44 AM »
Not sure I understand you, or maybe you have misunderstood me. Here's what I have:

PREFABS:

  • UIFont - English
  • UIFont - Japanese
  • UIFont (reference) - pointing to English
  • UILabel - font is set to UIFont (reference)

So at run time I create an instance of my UILabel prefab. The font is correctly set to English but I have no way to change that to Japanese as my font reference is a prefab and I can't modify a prefab at run time, only an instance of one.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Font reference issue
« Reply #3 on: April 18, 2015, 12:37:10 PM »
You can modify it, but you need to Resources.Load it first -- so it needs to be in the Resources folder along with others.

Resources.Load it, GetComponent<UIFont>() and set it to point to another.