Author Topic: Broken Dynamic Font  (Read 15747 times)

vip_prizrak_3

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Broken Dynamic Font
« on: August 17, 2015, 12:01:32 PM »
Hi!
I updated Unity to 5.1.2 and NGUI to 3.9.2.
Now I have the wrong text is displayed in the UILabel (look image).
If the UILabel is turned off and then on, the text will appear normal.
How can I fix it?

vip_prizrak_3

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: Broken Dynamic Font
« Reply #1 on: August 18, 2015, 04:35:46 AM »
I tried on a bare stage to make a single UILabel.
In editor text is displayed (image 1).
In runtime - not (image 2).

BeShifty

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 7
  • Posts: 52
    • View Profile
Re: Broken Dynamic Font
« Reply #2 on: August 18, 2015, 01:16:48 PM »
I'm also seeing this issue (the garbled label chars) on nearly every label after updating from NGUI 3.7.9 to 3.9.2 on Unity 4.6.7. Some playthroughs it appears normal for a while, but other times its an immediate issue.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Broken Dynamic Font
« Reply #3 on: August 18, 2015, 05:37:32 PM »
Ok, how would I reproduce this on my end?

BeShifty

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 7
  • Posts: 52
    • View Profile
Re: Broken Dynamic Font
« Reply #4 on: August 18, 2015, 06:19:02 PM »
Repro steps are hard to nail down at the moment. The only new error I'm seeing from NGUI is a null reference in NGUIText line 111 but it doesn't correlate to seeing the corrupted labels.  I'll respond again if I can reliably reproduce the issue.

vip_prizrak_3

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: Broken Dynamic Font
« Reply #5 on: August 20, 2015, 06:28:51 AM »
The problem disappeared by itself. :)
Font was himself properly displayed.

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Broken Dynamic Font
« Reply #6 on: September 08, 2015, 03:04:37 AM »
The problem disappeared by itself. :)
Font was himself properly displayed.

I am having the same problem. But no fix yet. What Unity version are you using? I'm on 4.6.7 f1

mdeletrain

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 71
    • View Profile
Re: Broken Dynamic Font
« Reply #7 on: September 09, 2015, 02:13:50 PM »
Hi !

Just to be sure, did you use the DynamicFontFix class I provided in this forum before aren included the corresponding fix in NGUI ?
If yes then you should remove it now, or it will conflict with the actual fix (sorry aren about that :( !).

Apart from that, the error at line 111 of NGUIText is indeed quite suspect, since nothing can be null there (dynamicFont has already been checked, CharacterInfo is a struct and its vert member is a Rect which also is a struct.

@aren : now that unity 4.6.8 is out, including all 4.6.7's patches, I think everybody that updated it now sees the new warnings about deprecated use of old callbacks I already talked about once. Could you fix that ?

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Broken Dynamic Font
« Reply #8 on: September 10, 2015, 02:14:05 AM »
I am using your fix with Unity 4.6.8f1 and yeah I get some warnings, but with the latest NGUI (3.9.2) the issue still appears, so I implemented your fix and it still appears but less often.  I don't know how to fix it.  :-\



Examples:

BADGOOD
« Last Edit: September 10, 2015, 02:24:49 AM by hexaust »

mdeletrain

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 71
    • View Profile
Re: Broken Dynamic Font
« Reply #9 on: September 11, 2015, 09:35:07 AM »
Actually, my fix should not be used with NGUI 3.9.2 which embed it.
The warnings are expected and could be fix, if aren has the time to do so.

As for the error at line 111 of NGUIText, I thought it was due to my fix conflicting with NGUI, but I just encountered it without it too... and clicked the clear console button too fast.

Next time I encounter it I'll post the stack trace here.

mdeletrain

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 71
    • View Profile
Re: Broken Dynamic Font
« Reply #10 on: September 11, 2015, 12:58:01 PM »
Ok I found the bug.

In NGUIText, lines 106 to 111 :
 * The Update(bool request) method of NGUIText checks if dynamicFont is null
 * If not, it requests some characters to help computing font's size
 * This request sometimes triggers a font change, which calls UILabel's OnFontChanged callback where UIPanels get refreshed, which rebuilds every labels and clears the dynamicFont field (UILabel.cs, line 1751)
 * just after that, NGUIText tries to get informations from ')' character from the dynamicFont which was not null... bam! it has indeed been nulled by the callback.

Here is the NRE stack trace :
  1. NullReferenceException: Object reference not set to an instance of an object
  2. NGUIText.Update (Boolean request) (at Assets/modules/ngui/src/Internal/NGUIText.cs:124)
  3. NGUIText.Update () (at Assets/modules/ngui/src/Internal/NGUIText.cs:105)
  4. UILabel.UpdateNGUIText () (at Assets/modules/ngui/src/UI/UILabel.cs:2046)
  5. UILabel.ProcessText (Boolean legacyMode, Boolean full) (at Assets/modules/ngui/src/UI/UILabel.cs:1237)
  6. UILabel.ProcessText () (at Assets/modules/ngui/src/UI/UILabel.cs:1197)
  7. UILabel.ProcessAndRequest () (at Assets/modules/ngui/src/UI/UILabel.cs:1088)
  8. UILabel.set_text (System.String value) (at Assets/modules/ngui/src/UI/UILabel.cs:280)
  9.  

And here is the stacktrace of the call that clears dynamicFont field just before the NRE :
  1. UILabel:OnFill(BetterList`1, BetterList`1, BetterList`1) (at Assets/modules/ngui/src/UI/UILabel.cs:1751)
  2. UIWidget:UpdateGeometry(Int32) (at Assets/modules/ngui/src/Internal/UIWidget.cs:1461)
  3. UIPanel:UpdateWidgets() (at Assets/modules/ngui/src/UI/UIPanel.cs:1596)
  4. UIPanel:UpdateSelf() (at Assets/modules/ngui/src/UI/UIPanel.cs:1237)
  5. UIPanel:LateUpdate() (at Assets/modules/ngui/src/UI/UIPanel.cs:1198)
  6. UIPanel:Refresh() (at Assets/modules/ngui/src/UI/UIPanel.cs:1711)
  7. UILabel:OnFontChanged(Font) (at Assets/modules/ngui/src/UI/UILabel.cs:1001)
  8. UILabel:<SetActiveFont>m__2E() (at Assets/modules/ngui/src/UI/UILabel.cs:951)
  9. UnityEngine.Font:RequestCharactersInTexture(String, Int32, FontStyle)
  10. NGUIText:Update(Boolean) (at Assets/modules/ngui/src/Internal/NGUIText.cs:121)
  11. NGUIText:Update() (at Assets/modules/ngui/src/Internal/NGUIText.cs:105)
  12. UILabel:UpdateNGUIText() (at Assets/modules/ngui/src/UI/UILabel.cs:2046)
  13. UILabel:ProcessText(Boolean, Boolean) (at Assets/modules/ngui/src/UI/UILabel.cs:1237)
  14. UILabel:ProcessText() (at Assets/modules/ngui/src/UI/UILabel.cs:1197)
  15. UILabel:ProcessAndRequest() (at Assets/modules/ngui/src/UI/UILabel.cs:1088)
  16. UILabel:set_text(String) (at Assets/modules/ngui/src/UI/UILabel.cs:280)
  17.  

One UGLY fix would be to insert
  1. NGUIText.dynamicFont = font;
at the end of UILabel's OnFontChanged callback (line 1005), but this is dangerous since it could have a lot of side effects, just like the one that causes this bug. I think a better fix would be to make NGUIText be not static, immutable from outside, and instantiated when needed, instead of configured when needed then somewhat reset randomly.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Broken Dynamic Font
« Reply #11 on: September 13, 2015, 06:04:32 AM »
Try simply replacing NGUIText.Update function on line 98 of NGUIText.cs with this:
  1.         static public void Update (bool request)
  2.         {
  3.                 Font font = dynamicFont;
  4.                 finalSize = Mathf.RoundToInt(fontSize / pixelDensity);
  5.                 finalSpacingX = spacingX * fontScale;
  6.                 finalLineHeight = (fontSize + spacingY) * fontScale;
  7.                 useSymbols = (bitmapFont != null && bitmapFont.hasSymbols) && encoding && symbolStyle != SymbolStyle.None;
  8.  
  9. #if DYNAMIC_FONT
  10.                 if (font != null && request)
  11.                 {
  12.                         font.RequestCharactersInTexture(")_-", finalSize, fontStyle);
  13.  
  14. #if UNITY_4_3 || UNITY_4_5 || UNITY_4_6
  15.                         if (!font.GetCharacterInfo(')', out mTempChar, finalSize, fontStyle) || mTempChar.vert.height == 0f)
  16.                         {
  17.                                 font.RequestCharactersInTexture("A", finalSize, fontStyle);
  18.                                 {
  19.                                         if (!font.GetCharacterInfo('A', out mTempChar, finalSize, fontStyle))
  20.                                         {
  21.                                                 baseline = 0f;
  22.                                                 return;
  23.                                         }
  24.                                 }
  25.                         }
  26.  
  27.                         float y0 = mTempChar.vert.yMax;
  28.                         float y1 = mTempChar.vert.yMin;
  29. #else
  30.                         if (!font.GetCharacterInfo(')', out mTempChar, finalSize, fontStyle) || mTempChar.maxY == 0f)
  31.                         {
  32.                                 font.RequestCharactersInTexture("A", finalSize, fontStyle);
  33.                                 {
  34.                                         if (!font.GetCharacterInfo('A', out mTempChar, finalSize, fontStyle))
  35.                                         {
  36.                                                 baseline = 0f;
  37.                                                 return;
  38.                                         }
  39.                                 }
  40.                         }
  41.  
  42.                         float y0 = mTempChar.maxY;
  43.                         float y1 = mTempChar.minY;
  44. #endif
  45.                         baseline = Mathf.Round(y0 + (finalSize - y0 + y1) * 0.5f);
  46.                 }
  47. #endif
  48.         }

mdeletrain

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 71
    • View Profile
Re: Broken Dynamic Font
« Reply #12 on: September 14, 2015, 04:27:42 AM »
If I'm not mistaken, you just cached dynamicFont into a local font variable so that it don't get lost.
That should be working, and I'll test that from my side today.

Anyway that hides the problem inherent to NGUIText which is being a static class with lots of public fields accessed from several places with some conflicts, so I hope there is no other problems that will surface because of this.

quaigon

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Broken Dynamic Font
« Reply #13 on: September 14, 2015, 09:24:13 AM »
I have a similar problem. My labels goes crazy, when their parent-widget are scale-tweened from 1 to -1 and backwards. but only on the device iPad Air, not in Editor.

with the new update-function, from these thread, it seems to become worse (almost always than from time to time). There are no recognizable rules for the effect.

i use a UIFont-Prefab with font-type = dynamic, set the shaders to "Cull Back" and NGUI 3.9.21 with Unity3D 4.6.7f1.


edit: i tried the old version NGUI 3.9.0b. Now there is no problem - so far. Are there some significant changes?
« Last Edit: September 14, 2015, 10:14:34 AM by quaigon »

poolts

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Broken Dynamic Font
« Reply #14 on: September 15, 2015, 05:37:49 AM »
After updating from NGUI 3.9.0 to 3.9.2 I'm also seeing the garbled font issue.

Also weird on 3.9.2 sometimes the fonts would be displaced from their starting positions after enabling / disabling them (they didn't have any anchors assigned to them) this issue does not happen on 3.9.1.

I'll try to get some repo steps / screenshots to add to this post.