Author Topic: Changing a Labels Font and Pivot through script (C#)  (Read 12457 times)

IIIDeFeKtIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Changing a Labels Font and Pivot through script (C#)
« on: February 07, 2014, 03:14:06 PM »
I am looking for a away to change an NGUI Font and Pivot and cannot find out how to do it through script.
What I am trying to do is make this process easier in my project by setting the values in an XML file. So when I change font="Arial" to another bitmap or dynamic font. Also I have not been able to change the enum of the pivot. I am looking to do this the same way as the font. So if I have a property like align="Center" in my XML id it will center it or align it left or right.

Is this even possible?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #1 on: February 08, 2014, 02:52:45 PM »
If you want to change the font in one place and have your entire UI update, then you should use a reference font same way you'd use a reference atlas. As of version 3.4.9 you can reference dynamic fonts on the UIFont, and have labels keep references to this UIFont, using its dynamic font as you'd expect. So if your entire UI was built using the reference font, then you only need to swap the target font in one place -- on the reference font's prefab.

If you want to change label alignment, then you need to get the pivot. TopLeft, Left, and BottomLeft cause the label to be left-aligned. Top, Center, and Bottom -- center-aligned. TopRight, Right, and BottomRight -- right-aligned.

IIIDeFeKtIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #2 on: February 08, 2014, 03:08:34 PM »
I will look into referencing the Atlas but as far as the enum of the pivot goes I tried calling with UILabel text; text.pivot = center;  of exactly sure how to call it since it's a uiwidget

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #3 on: February 08, 2014, 03:10:25 PM »
No, not atlas. Font. Reference font. Same functionality as reference atlas, just for fonts.

  1. GetComponent<UIWidget>().pivot = UIWidget.Pivot.Center;

IIIDeFeKtIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #4 on: February 08, 2014, 03:46:39 PM »
Now I see what my issue is. I was declaring it as well like a UILabel. Thanks for the help.

IIIDeFeKtIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #5 on: February 09, 2014, 09:07:07 AM »
I was closer with the pivot. I've been tearing my hair out trying to reference a font. I'm not under standing how to access the new font with dynamic set. All I have to do is have a font in there right? Not have to make it a uifont like the bitmap way? When I change from bitmap to dynamic I noticed it goes from uifont to just font. When ever I try to change it I get errors.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #6 on: February 09, 2014, 04:30:30 PM »
1. ALT+SHIFT+N to create a game object.
2. Attach UIFont script to it.
3. Save it as a prefab by dragging it into your Project view. Now you have a blank NGUI font (you can now delete the object from your Hierarchy view)

You can now:

4. Choose it to be a reference font ("reference") and have it point to something else, or give it either a Unity font ("dynamic") or an NGUI font ("normal", I will change it to "bitmap" in the next release).

So if you want to have a reference font, and a dynamic font, repeat this twice. Once creating the dynamic font, and the second time creating a reference to it.

IIIDeFeKtIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #7 on: February 10, 2014, 12:09:16 PM »
what do I actually change tho. So Now I have reference font prefab and a dynamic font prefab(is set to a certain font). Do I change it on each one of my labels? I am trying to just drag it onto it but when the label is set to dynamic it won't let me. Only when I change it to bitmap type I can drag it onto it. It's a little confusing on how it works.

IIIDeFeKtIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #8 on: February 10, 2014, 02:52:35 PM »
Not sure if this is correct, but it works. I was able to just drop the fonts for dynamic text into the resources folder and call it like this:
I'm using xml to store all the properties so it's a little different.

I use this to get and set it:
  1. UILabel title   = NGUITools.FindInParents<UILabel>(GameObject.Find("popup_title"));
  2. StyleManager.SetTextFont(title, XML.getStyle("popupTitle", "font"));

This is in my class called StyleManager:
  1. public static void SetTextFont(UILabel label, string fontType)
  2. {
  3.       label.trueTypeFont = Resources.Load("Prefabs/"+fontType) as Font;
  4. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #9 on: February 10, 2014, 05:43:02 PM »
Alright, let's start over.

What you're currently doing is simply changing the font on your label. This isn't the same as setting a reference font's value.

With the reference font, it should be set up like so:

For example, your UI:

UIRoot
- Label 1 (uses Font 1)
- Label 2 (uses Font 1)
- Label 3 (uses Font 1)

Then you have your font prefab:

Font 1 (a prefab with UIFont on it set to Dynamic type)

Now if you want to change the dynamic font of all of your labels at the same time, all you need to do is to set it on your Font 1 prefab.

Note: on the font itself. Not on the labels.

If you have your Style Manager class, have it have a public reference to your "Font 1":
  1. public UIFont myFont;
...and simply set myFont.dynamicFont to what you need it to be.

IIIDeFeKtIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #10 on: February 10, 2014, 07:01:45 PM »
What is the difference between making it a prefab as dynamic and changing the font on the label? I am not looking to change all at once. Just whatever it is set to in the xml so someone who knows nothing about Unity can choose from the fonts available per title/text/buttons.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing a Labels Font and Pivot through script (C#)
« Reply #11 on: February 11, 2014, 08:41:06 PM »
With the prefab you can set it in one place and have 100 labels all be affected. Changing it at the label level means you have to do it for each label separately. If that suits your purpose, then that's fine.