Author Topic: Dynamic font blink and randomly don't show properly REAL REASON  (Read 11434 times)

csun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Hi, ArenMook and everyone who use east asian fonts,
when we want to call Font.GetCharacterInfo() to get a character's width, we should call Font.RequestCharactersInTexture() first, or we couldn't get info.
UIPanel generate widget's mesh in LateUpdate (include UILabel's mesh), when generated a UILabel's mesh, then in LateUpdate WrapText or somewhere else we call Font.RequestCharactersInTexture(), this makes font texture changed, so uv of generated UILabel's mesh is wrong!
I think we should call every dynamic font once before LateUpdate and after Update once, which deliver all text related about this font to the Font.RequestCharactersInTexture(), so there wouldn't be blink and didn't show problem again.

You can use this code and east asian font to test.
You should add several normal dynamic font UILabel to scene and only one UILabel attached this script.
  1. public UILabel lbl;
  2. public int start = 0;
  3. public const int area = 100;
  4.  
  5. void Start()
  6. {
  7.         InvokeRepeating("GenerateText", 0f, 0.2f);
  8. }
  9.  
  10.  
  11. void GenerateText()
  12. {
  13.         System.Text.StringBuilder sb = new System.Text.StringBuilder();
  14.         for (int i = start; i < start + area; ++i)
  15.         {
  16.                 char c = (char)i;
  17.                 sb.Append(c);
  18.         }
  19.         start += area;
  20.         lbl.text = sb.ToString();
  21. }
  22.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #1 on: July 26, 2013, 03:57:15 AM »
I am well aware, and I am doing it. Check the code. :P

csun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #2 on: July 26, 2013, 04:22:15 AM »
I don't have a Professional license and I couldn't access NGUI's git repository, so I can't see the code.
Could you tell me which date will the new version release?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #3 on: July 26, 2013, 04:43:57 AM »
...?

Yes you can. NGUI always comes in source code form, unless you have the demo version.

csun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #4 on: July 26, 2013, 05:03:20 AM »
I know what you mean. I could see NGUI 2.6.3 code, but these Font.RequestCharactersInTexture() still appears in 5 place.
It's still have the problem. What I mean is I can't access to NGUI's git repository, so I can't see the latest code.

So I want to ask when to release the new release such as NGUI 2.6.4?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #5 on: July 27, 2013, 12:20:29 PM »
RequestCharactersInTexture is always called before using the glyph itself, just like your original post says. I'm not sure what not having access to the Pro repository has to do with it. Changes in it have nothing to do with this particular part of the code.

windsky527

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 11
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #6 on: July 28, 2013, 01:17:05 AM »
"the label blink" problem is very frequent, when the game window have many changing labels......and sometimes the text will lose or display error


csun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #7 on: July 28, 2013, 08:03:55 AM »
The problem is that RequestFontInTexture() will make font texture change, this makes generated mesh's uv wrong.
I think unity should add a GetCharacterWidth() which without calling RequestFontInTexture(), then you could use this function to replace.
And I have done a test that only call RequestFontInTexture() once per frame before generate UILabel's mesh, then everything is become so good, no blink, no display wrong!

csun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #8 on: July 28, 2013, 08:26:30 PM »
What do I mean is that diable RequestFontInTexture() in LateUpdate.
NGUI generate mesh through UpdateWidgets in LateUpdate, so I add some code to implement it.



Change these code Macro define to run in non Editor
  1.         int mScreenHeight = 720;
  2.         void Update () { mScreenHeight = Screen.height; hasRequestFont = false; }
  3.  

Add UpdateDynamicFonts() before UpdateWidgets()
  1.         /// <summary>
  2.         /// Update all widgets and rebuild their geometry if necessary.
  3.         /// </summary>
  4.  
  5.         void UpdateWidgets ()
  6.         {
  7.                 UpdateDynamicFonts();
  8.  
  9.         }
  10.  
Add a new function to UIPanel and a static variable to mark
  1.         /// <summary>
  2.         /// Update Dynamic Fonts
  3.         /// </summary>
  4.  
  5.         public static bool hasRequestFont = false;
  6.         void UpdateDynamicFonts()
  7.         {
  8.                 if (hasRequestFont)
  9.                         return;
  10.  
  11.                 hasRequestFont = true;
  12.  
  13.                 // find all dynamic fonts
  14.                 var dynamicFonts = new Dictionary<UIFont, System.Text.StringBuilder>();
  15.  
  16.                 // find all labels
  17.                 UILabel[] labels = NGUITools.FindActive<UILabel>();
  18.                 Camera[] cameras = NGUITools.FindActive<Camera>();
  19.  
  20.                 for (int i = 0, imax = labels.Length; i < imax; ++i)
  21.                 {
  22.                         UILabel lbl = labels[i];
  23.                         UIFont fnt = lbl.font;
  24.                         if (!string.IsNullOrEmpty(lbl.text))
  25.                         {
  26.                                 if (fnt.isValid && fnt.isDynamic)
  27.                                 {
  28.                                         if (!dynamicFonts.ContainsKey(fnt))
  29.                                         {
  30.                                                 dynamicFonts.Add(fnt, new System.Text.StringBuilder());
  31.                                         }
  32.                                         dynamicFonts[fnt].Append(lbl.text);
  33.                                         lbl.MarkAsChanged();
  34.                                 }
  35.                         }
  36.                 }
  37.  
  38.                 // request fonts
  39.                 foreach (var pair in dynamicFonts)
  40.                 {
  41.                         UIFont fnt = pair.Key;
  42.                         string text = pair.Value.ToString();
  43.                         fnt.dynamicFont.RequestCharactersInTexture(text, fnt.dynamicFontSize, fnt.dynamicFontStyle);
  44.                 }
  45.         }
  46.  

Last, add if hasRequestFont to all RequestCharactersInTexture(), disable call after Request all text.
  1.                 if (dynamic && !UIPanel.hasRequestFont)
  2.                 {
  3.                         mDynamicFont.textureRebuildCallback = OnFontChanged;
  4.                         mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize, mDynamicFontStyle);
  5.                         mDynamicFont.textureRebuildCallback = null;
  6.                 }
  7.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #9 on: July 29, 2013, 09:36:22 AM »
UpdateWidgets() is not used anymore with Unity 4.1+...

csun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #10 on: July 29, 2013, 10:50:34 AM »
OK, I know that, I'm using NGUI 2.6.1 , but I have test NGUI 2.6.3, it still has the same problem. And then I have searched whole NGUI project which found RequestCharactersInTexture 5 places.

So I guess the problem is still there. I think there must be a good solution to resolve it. My solution is just the hack that without whole GUI system design.

csun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #11 on: July 29, 2013, 08:41:56 PM »
I think you could add two UIPanel and several UILabel, and set UILabel's text with some of following text:

Quote
这引发了一个问题,用Assembly加载的DLL可能只在程序结束的时候才会被释放,

这也意味着在程序运行期间无法更新被加载的DLL。而这个功 能在某些程序设计时是非常必要的,

考虑你正在用反射机制写一个查看DLL中所有函数详细信息的程序,程序提供一个菜单让用户可以选择DLL文件,

这时就需 要让程序能够卸载DLL,否则一旦用户重新得到新版本DLL时,必须要重新启动程序,重新选择加载DLL文件,

这样的设计是用户无法忍受的。

then use test code above and run, you will find the problem. You could use the latest NGUI.

csun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #12 on: July 29, 2013, 08:51:48 PM »
I have make an example about this problem.
The package doesn't ttf font and NGUI, so you should add these manually, and fix reference.

attack82

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #13 on: August 02, 2013, 12:10:41 PM »
hi~ csun

what is your unity version?
i can't import your unitypackage..T_T

csun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Dynamic font blink and randomly don't show properly REAL REASON
« Reply #14 on: August 02, 2013, 08:32:20 PM »
I forgot to say my Unity3d version is 4.1.2, and I have test it on NGUI 2.6.1 and 2.6.3