Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help with fonts cache

Discussion in 'UGUI & TextMesh Pro' started by barbe63, May 14, 2015.

  1. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    Hello,

    I'm using dynaminc fonts in my game and I'm struggling against the huge spikes I'm having on my game due to Font.CacheFontForText. I have seens some threads about that and I found some code I tried to adapt on mine, so I did:
    Code (CSharp):
    1.   public Font jackCandleStick;
    2.      public Font timerFont;
    3.      private static readonly string glyphs= "0123456789+: "; // all the characters I need
    ...in the class and in the awake I have:

    Code (CSharp):
    1.   for (int index= 0; (index < glyphs.Length); ++index)
    2.          {
    3.              jackCandleStick.RequestCharactersInTexture(
    4.                  glyphs[index].ToString(),
    5.                  jackCandleStick.fontSize, FontStyle.Normal);
    6.              timerFont.RequestCharactersInTexture(
    7.                  glyphs[index].ToString(),
    8.                  timerFont.fontSize, FontStyle.BoldAndItalic);
    9.          }
    ...but I don't see any impact, the spikes are still there so I guess I'm doing something wrong. I don't really understand how this is working so please help me!

    Thanks.
     
  2. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    In order for the text to be displayed at different point size, Unity's Dynamic Font System needs to raster these characters for each of the sizes used and add them to the Font Atlas. So if you are using different point size or Best Fit on the text objects, this will cause duplicates of these characters to be added. I believe using a CanvasScaler might also produce the same result.

    The benefit of Unity's Dynamic Font System is that you don't have to worry about which characters to include. The downside is that it has a run-time performance overhead as new characters are added and the # of character that can be added and packing of the Font Atlas is inefficient since the same characters will be present several times not just for sizes but for styles like bold.
     
  3. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    Thanks for replying. I searched for a solution and i found many threads where they seems to explain some workaround, one example here:

    http://answers.unity3d.com/questions/149762/how-to-prepopulate-font-cache.html

    I don't understand the Graham's way and IVxIV released his code in many thread but unfortunately I didn't managed to get it working.

    I found the C# sample in unity doc even more obscure:
    http://docs.unity3d.com/ScriptReference/Font.RequestCharactersInTexture.html

    I would really like to keep the benefits of dynamic fonts and best fit.
     
    Last edited: May 14, 2015
  4. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    You are simply running into the limitations of Unity's Dynamic Font system. Every time the point size or style of a text object ends up changing, it will trigger Font.CacheFontForText.
     
  5. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    I'm beginning to think of a system would not use the dynamic fonts but instead i'll had fixed size which would be set accordingly to the size of the screen (seems quite hard). i also have to have 2 different sizes for the timer as i want it to add decimals on some occasions (like if the time is below 10s) so maybe i should try like graham said something with a text GUI that would always have some text with my timer font halfed sized but not rendered. You have some hints for me?