Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Mischa · Jul 07, 2015 at 12:32 PM · uitextsizegeneratorfit

How do I calculate a text size with the Best Fit option using a TextGenerator

My goal is to link several Text components that they all have the same text size. Text size should be the minimum 'best fit' size of all components.

How I try to achieve it is by turning the 'best fit' option off and calculate the size with a custom TextGenerator, copying all TextGenerationSettings from the original Text components.

My Problem is: The calculated Text size is too small. It adjusts according to the RectTransform size, but is just wrong. Any thoughts about this?

Here is the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 [ExecuteInEditMode]
 public class kgLinkedBestFit : UIBehaviour 
 {
     public int minTextSize = 5;
     public int maxTextSize = 35;
     public Text[] linkedTexts;
 
     //TEMP: Editor Update
     public void Update()
     {
         TextGenerator textGenerator = new TextGenerator();
         int minimumBestFit = 1000;
 
         foreach(Text t in linkedTexts)
         {
             //get current text generation settings for the text field
             TextGenerationSettings settings = t.GetGenerationSettings(t.rectTransform.rect.size);
 
             //change to best fit settings
             settings.resizeTextForBestFit = true;
             settings.resizeTextMinSize = minTextSize;
             settings.resizeTextMaxSize = maxTextSize;
 
             //calcuclate with text generator
             textGenerator.Populate(t.text,settings);
         
             //set new bestFit if new calculation is smaller than current bestFit
             minimumBestFit = textGenerator.fontSizeUsedForBestFit < minimumBestFit ? textGenerator.fontSizeUsedForBestFit : minimumBestFit;
 
         }
 
         /*at this stage minimumBestFit is calculated too small. When the original textfield is set to best fit, the text size will be 22, the calculated text size is only 12*/
 
         foreach(Text t in linkedTexts)
         {
             //adjust font sizes
             t.fontSize = minimumBestFit;
         }
     }
 }




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

2 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by hugojacob · Nov 25, 2015 at 09:19 AM

Hey.Old post, but for others i will answer.

I ran into the same problem. When using fontSizeUsedForBestFit variable the size returned did not match in the editor. After a long research the short answer to the problem is to divide the fontsize with the your canvas scale factor like this:

fontsize = fontsize / canvas.scaleFactor;

hope this helps others.

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 Anisoropos · Aug 09, 2017 at 09:21 AM 2
Share

It sure did help!

Here's my full code for getting the current dynamic font size. Place these in a public static class.

             public static int GetCurrentDynamicFontSize(this Text text)
             {
                 if (text == null) return -1;
 
                 float multiplier = 1;
                 Canvas canvas = text.GetHighestCanvas();
                 if (canvas)
                     multiplier = 1 / canvas.scaleFactor;
 
                 return (int)(text.cachedTextGenerator.fontSizeUsedForBestFit * multiplier);
             }
 
             public static Canvas GetHighestCanvas<T>(this T component) where T : Component
             {
                 if (component == null) return null;
                 Canvas[] parentCanvases = component.GetComponentsInParent<Canvas>();
                 if (parentCanvases != null && parentCanvases.Length > 0)
                 {
                     return parentCanvases[parentCanvases.Length - 1];
                 }
                 return null;
             }
avatar image
4

Answer by Gwom · Apr 19, 2017 at 03:09 PM

In case anyone else wants it, you have to wait a frame so the fontSizeUsedForBestFit gets calculated. I get consistant size of an item by adding anything I want to an array in the inspector and on play, wait a frame, find the size and apply it:

 using UnityEngine.UI;
 
 /// <summary>
 /// Cant work in editor, but will when the play button is pressed
 /// </summary>
 public class ConsistantTextSize : MonoBehaviour {
     public Text[] m_TextList;
     private int m_Size = 1;
 
     // Use this for initialization
     void Start () {
         SetSizes();
     }
 
     public void SetSizes()
     {
         foreach (var item in m_TextList)
             item.resizeTextMaxSize = 1000;
 
         StartCoroutine(WaitFrame());
     }
 
     public IEnumerator WaitFrame()
     {
         // returning 0 will make it wait 1 frame
         // need to wait a frame in order for the layout to work out the size
         yield return 0;
         ApplySizes();
     }
 
     public void ApplySizes()
     { 
         // Get the first one
         if (m_TextList.Length > 0)
             m_Size = m_TextList[0].cachedTextGenerator.fontSizeUsedForBestFit;
 
         // See if any are smaller
         foreach (var item in m_TextList)
         {
             if (item.cachedTextGenerator.fontSizeUsedForBestFit < m_Size)
                 m_Size = item.cachedTextGenerator.fontSizeUsedForBestFit;
         }
 
         // Apply as max size to all of the images
         foreach (var item in m_TextList)
             item.resizeTextMaxSize = m_Size;
     }
 }
 
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

25 People are following this question.

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

Related Questions

Make UI always center the panel 1 Answer

UI Relative to screen size 1 Answer

How do I calculate how big text is in a world space canvas UI Text? 2 Answers

UI Text: Wrap and Expand to keep aspect ratio 2 Answers

How to make parent UI element change size to fit children? 0 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