Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Mostley · Jul 28, 2011 at 05:35 PM · guiprofilerfontcache

How to prepopulate Font Cache?

Changing Texts on the GUI has some negative effect on the CPU Speed. ComputationSpikes are the result. Especially calls to Font.CacheFontForText if the Profiler can be trusted on this. 368.55ms for 2 Calls to this function...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Graham-Dunnett · Jul 28, 2011 at 08:40 PM

You must be using a dynamic texture. With dynamic textures Unity renders glyphs into a texture as you submit character strings for rendering. Unity effectively manages a texture with all the fonts that you have used in all the different sizes. Font.CacheFontForText is basically the code that is trying to find where in this texture to put the new glyphs. Occasionally it runs out of space in the texture, in which case the texture is replaced by one twice the size and all the glyphs from the old texture are copied across into the new one. Also, find space for glyphs (which might all be different sizes) can take time.

To pre-populate the cache you might want to try and render as much of your text, perhaps by rendering GUITexts onto a render texture (or something that you are not going to display. I guess a different approach is rendering one single glyph that is super ginormous in size to force the dynamic font texture to be enlarged up to the size you need it to be.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image liortal · Mar 29, 2016 at 06:37 AM 0
Share

do you mean dynamic fonts? (what are dynamic textures?)

avatar image
6

Answer by jonas-echterhoff · Jul 29, 2011 at 09:50 AM

What Graham said is all correct. Also, you might consider just using a static Font texture (change the font import settings from "Dynamic" to one of the other settings). Especially, if you don't need the flexibility of displaying asian character sets or very big characters, then you might not need a dynamic font.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Emery-Monzerol · Jul 01, 2013 at 08:50 PM 0
Share

You're my hero! :D I'd upvote this very very much, if only I had 15 reputation xD

avatar image
2

Answer by IVxIV · Dec 24, 2014 at 08:40 PM

If you know ahead of time what dynamic font(s) you are going to be using, one approach that I have used with success in the past is to make repeated calls to Font.RequestCharactersInTexture() inside of a coroutine (to spread the work of precaching font glyphs across several frames). Here is some example code illustrating the approach I have used:

 public class GUIClass : MonoBehaviour
 {
     private static readonly string kPrecacheFontGlyphsString= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_+=~`[]{}|\\:;\"'<>,.?/ ";
     
     // utility struct used in font caching
     struct CacheFont
     {
         public Font theFont;
         public int size;
         public FontStyle style;
     };
     
     void Awake()
     {
         // gather custom fonts that we will be using
         CacheFont[] myCustomFonts= RetrieveMyCustomFonts();
 
         if (null != myCustomFonts)
         {
             for (int fontIndex= 0; fontIndex < myCustomFonts.Length; ++fontIndex)
             {
                 StartCoroutine(PrecacheFontGlyphs(
                     myCustomFonts[fontIndex].theFont,
                     myCustomFonts[fontIndex].fontSize,
                     myCustomFonts[fontIndex].fontStyle,
                     kPrecacheFontGlyphsString));
             }
         }
         
         return;
     }
 
     // Precache the font glyphs for the given font data.
     // Intended to run asynchronously inside of a coroutine.
     IEnumerator PrecacheFontGlyphs(Font theFont, int fontSize, FontStyle style, string glyphs)
     {
         for (int index= 0; (index < glyphs.Length); ++index)
         {
             theFont.RequestCharactersInTexture(
                 glyphs[index].ToString(),
                 fontSize, style);
             yield return null;
         }
         
         yield break;
     }
 
 
     void OnGUI()
     {
         // now that dynamic font glyphs have been precached,
         // there won’t be a terrible hitch the first time this is called
     }
 };
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by ickydime · Oct 29, 2015 at 06:25 PM

I modified @IVxIV's script so you can just add the following to entities with Text behaviors.

You'll need to create your own GlyphSet enum and constants but that is pretty straight forward.

 public class FontCacher : MonoBehaviour {
     
     public Text TextField; 
     public GameConstants.GlyphSets GlyphSet = GameConstants.GlyphSets.Standard;
     public string CustomGlyphs;
 
     protected void Awake () {
         if(TextField==null)
         {
             TextField = gameObject.GetComponent<Text>();
         }
         StartCoroutine(CacheFont());
     }
     
     protected IEnumerator CacheFont()
     {
         string glyphs;
 
         switch(GlyphSet)
         {
         case(GameConstants.GlyphSets.Custom):
             glyphs = CustomGlyphs;
             break;
         case(GameConstants.GlyphSets.Current):
             glyphs = TextField.text;
             break;
         case(GameConstants.GlyphSets.NumbersOnly):
             glyphs = GameConstants.GLYPH_NUMBERS;
             break;
         case(GameConstants.GlyphSets.Standard):
         default:
             glyphs = GameConstants.GLYPH_STANDARDS;
             break;
         }
 
         Font font = TextField.font;
         font.RequestCharactersInTexture(glyphs, TextField.fontSize, TextField.fontStyle);
 
         yield break;
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to measure the pixel with/height of a string with a given font? 2 Answers

Font.CacheFontForText is 87.3% in the profiler and causes a HUGE lag spike 1 Answer

SetFont Requirements? 0 Answers

How to find a font via script? 2 Answers

Changing Font Size 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges