Author Topic: Scrambled text with UILabel / UIFont of NGUI 3.5.7  (Read 1835 times)

rayark

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Scrambled text with UILabel / UIFont of NGUI 3.5.7
« on: April 14, 2014, 10:55:54 PM »
Hi,

My project suffers from scrambled text with UILabel / UIFont of NGUI 3.5.7.
Scrambled text only appears when running play mode in Unity Editor, and in standalone application, everything is OK.

After debugging,
I found that when running play mode in Unity Editor,
Unity will always create object with default constructor for private fields which class has the attribute [System.Serializable], even if those fields are private.

In UIFont.cs,
  1. public class UIFont : MonoBehaviour
  2. {
  3.     ...
  4.     UISpriteData mSprite = null;
  5.     ...
  6. }
  7.  
because UISpriteData is a pure C# class with the attribute [System.Serializable],
mSprite will be initialized with default constructor when compiled managed DLLs is loaded by UnityEditor.

Therefore, when running play mode in Unity Editor, some initialization logic won'be executed because mSprite is not initialized as null.



To fix this problem,
I put attribute [System.NonSerialized] to mSprite, and it won't be initialized with default constructor anymore, and the rendering of UILabel becomes correct.

  1. public class UIFont : MonoBehaviour
  2. {
  3.     ...
  4.     [System.NonSerialized]
  5.     UISpriteData mSprite = null;
  6.     ...
  7. }
  8.  


Alvin

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scrambled text with UILabel / UIFont of NGUI 3.5.7
« Reply #1 on: April 15, 2014, 09:23:20 AM »
I vaguely remember running into this back in Unity 3.4 days, and I've not seen it in years... what version of Unity are you running?

rayark

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Scrambled text with UILabel / UIFont of NGUI 3.5.7
« Reply #2 on: April 15, 2014, 10:24:20 PM »
I currently use Unity 4.3.4f1.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scrambled text with UILabel / UIFont of NGUI 3.5.7
« Reply #3 on: April 16, 2014, 10:34:05 AM »
Interesting... well, I'll add that line, it doesn't do any harm.