Author Topic: Reference Font Switching Btw Dynamic and BitMap Font  (Read 7282 times)

SamK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Reference Font Switching Btw Dynamic and BitMap Font
« on: January 19, 2015, 09:25:16 AM »
HI,
Im facing a problem in doing Localicazion. I have different fonts.
1) English
2) Chiense
3) etc
For the time beign they are two but can increase.
Im using English Bitmap Font for English and other languages and dynamic font for Chinese.
I want to switch between these two fonts. I have done this many times with atlas but With fonts it creating a problem.

I have tried two methods
1) Load RefFont From resources and change it
2) Place RefFont In scene Get it and do action.

Problem with 1 is All the Lables pointing to the RefFont (placed in resources folder)dont get affected. What i mean is what ever i do from code nothing happens. but when in change the font in RefFont i.e drag drop chiense or english font they change in the game.
2) Now All the lables are pointing to the refFont Placed in the scene. Reffont(Embedded) WHen i switch the font all my lables show blank.

What actually Happens is When ever i execute my following code. The Font(GameObject) type is changed from Reference to Bitmap dont know why this happens. And i couldnt find any way to change font type.
IF i manually Change the fonts type to reference (in the hiererchy while game is running) and Assign it chinese or english it works not through code.
Heres the code

  1. void SwitchFont(int lang)
  2.         {
  3.                 if(!font)
  4. //                      font = (Resources.Load("Fonts/RefFont") as GameObject).GetComponent<UIFont>();
  5.                         font = GameObject.FindGameObjectWithTag ("RefFont").GetComponent<UIFont> ();
  6.                 switch (lang)
  7.                 {
  8.                 case gLocale.LANG_ENGLISH:
  9.                         font.replacement = Resources.Load("Fonts/English Font") as UIFont;
  10.                         break;
  11.                 case gLocale.LANG_CHINESE:
  12.                         font.replacement = Resources.Load("Fonts/Chinese Font") as UIFont;
  13.                         break;
  14.                 case gLocale.LANG_FRENCH:
  15.                         break;
  16.                 }
  17.         }

Any Idea where im going wrong ? and how to tackle this problem ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Reference Font Switching Btw Dynamic and BitMap Font
« Reply #1 on: January 19, 2015, 11:51:08 AM »
You shouldn't be loading the reference font. You should be using it already -- and modifying the prefab itself.

It's the bitmap or dynamic font prefab that should be getting loaded from resources. You load one of them, then set the reference font prefab's value to point to it.

SamK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Reference Font Switching Btw Dynamic and BitMap Font
« Reply #2 on: January 20, 2015, 12:25:12 AM »
Yeah im exactly doing the same thing. I wrote im doing it two ways .
int he second approach i treid the same thing .

  1. void SwitchFont(int lang)
  2.     {
  3.         if(!font)
  4. //                      font = (Resources.Load("Fonts/RefFont") as GameObject).GetComponent<UIFont>();
  5.             font = GameObject.FindGameObjectWithTag ("RefFont").GetComponent<UIFont> ();
  6.         switch (lang)
  7.         {
  8.         case gLocale.LANG_ENGLISH:
  9.             font.replacement = Resources.Load("Fonts/English Font") as UIFont;
  10.             break;
  11.         case gLocale.LANG_CHINESE:
  12.             font.replacement = Resources.Load("Fonts/Chinese Font") as UIFont;
  13.             break;
  14.         case gLocale.LANG_FRENCH:
  15.             break;
  16.         }
  17.     }

Heres the code you can clearly see. Ref font is already in the scene and IM trying to Load other Fonts from Resources and assigning it . using the replacement property.
How to change the prefab value that you are saying ?



P.S. All my labels are pinting to Reference Font. So it should work.

Here are the two screen shots in first you can see what is REF Font.
and in the second you can see the REf Font totaly changes. And all the lables go blank. I just run the above code on clicking the chiense/english button .
« Last Edit: January 20, 2015, 12:31:29 AM by SamK »

SamK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Reference Font Switching Btw Dynamic and BitMap Font
« Reply #3 on: January 20, 2015, 03:55:55 AM »
One more thing I cant Keep RefFont In the Scene . Beacause I have many Prefabs That I load. And their link to  Reference font is broken.  The link to  Ref Font is stable for those gameobjts which are already present in the scene.

Scenarios
Ref Font is placed in Scene which is a Reference Font
Objects alreayd in scene are working fine.
Objects which i load at runtime have to connection to the Reference Font.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Reference Font Switching Btw Dynamic and BitMap Font
« Reply #4 on: January 20, 2015, 09:47:20 PM »
Quote
Heres the code you can clearly see. Ref font is already in the scene
That's your problem right there. The font is a prefab. It shouldn't be in the scene. You aren't supposed to be loading anything, and it shouldn't be something you GameObject.Find. You should already have a pointer to it (all your labels have it, remember?)

SamK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Reference Font Switching Btw Dynamic and BitMap Font
« Reply #5 on: January 21, 2015, 01:21:12 AM »
Aren yeah i know all the labels have it ..
  1. if (font == null)
  2.                 {
  3.                         UILabel label = FindObjectOfType<UILabel>();
  4.                         if(label)
  5.                                 font = label.bitmapFont;
  6.                 }
I got the font from a lable using this  code and it says RefFont wihch is pointing to EnglishFont.
Now how to change the font inside RefFont. if i use
  1. font.replacement = Resources.Load("Fonts/English Font") as UIFont;
The replacement propperty which was previously pinting to EnglishFont becomes NULL and the lables starting Showing Blank.

Im using Reference Atlas at its working fine using the same code
  1. referenceAtlas[0].replacement = Resources.Load("Atlases/" + DAY_ATLAS_NAME, typeof(UIAtlas)) as UIAtlas;

SamK

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Reference Font Switching Btw Dynamic and BitMap Font
« Reply #6 on: January 21, 2015, 01:49:38 AM »
Oh my my ..i was doing it right from the start just needed to change the resources.Load parameter.

  1. font.replacement = Resources.Load("Fonts/English Font", typeof(UIFont)) as UIFont;
  2. or Resources.Load<T>(path);


its Done .. Thanks anyways :)
Working fine still if you think there can be anyimprovements do let me know