I had a similar problem, i put this in the parent and assign all Text items i want to the array in the editor. Cant get it to work in the editor though due to the way the font is made:
using UnityEngine.UI;
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.fontSize < 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;
}
}
Pretty much the same answer as Gwom, but to cater for canvas scaling. Import the script below to the relevant panel in your canvas. Assign the canvas variable in editor by drag and drop. In one of your managing scripts, create a variable:
public SetAllFontSizesToSmallestInGroup smallestFontHandler;
Then assign the SetAllFontSizesToSmallestInGroup script to the variable above in editor.
Set the Text items using the method:
smallestFontHandler.SetTextItems(newTextItems);
Then the font sizes will be set (mostly) equal and to smallest after a frame. I say mostly equal, because the resizeTextMaxSize is set 1 higher than calculated. Due to canvas scaling, and then rounding, the calculated font can be too small if 1 is not added. The end result offers a good balance between readability, and sameness.
There is a script option called “onlyCalculateSmallestSizeOnce”, which can be set in the editor. If you set this true, the font size is only calculated once when you first set the text List. Even if you change the text, the original smallest font is used. This is to avoid text size jumpiness, if you change the text group content a lot over a short period of time.
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
public class SetAllFontSizesToSmallestInGroup : MonoBehaviour
{
public Canvas canvas;
int smallestFontSize;
bool hasSetAllFontSizeSameSinceBestFitDone = false;
bool returningFromSmallestFontEnumeratorYield = false;
bool haveCalculatedSmallestFontSizeAtLeastOnce = false;
public bool onlyCalculateSmallestSizeOnce = false;
List<Text> textItems;
const int fontSizeNotSet = -1;
void Start()
{
}
void Update()
{
if (NeedRecalculateSmallestFont())
{
StartCoroutine(SetTextSizesToSmallest());
}
}
bool NeedRecalculateSmallestFont()
{
if (textItems != null)
{
if (hasSetAllFontSizeSameSinceBestFitDone == false && textItems.Count > 0 && returningFromSmallestFontEnumeratorYield == false && (onlyCalculateSmallestSizeOnce == false || (onlyCalculateSmallestSizeOnce == true && haveCalculatedSmallestFontSizeAtLeastOnce == false)))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public void SetTextItems(List<Text> textItems)
{
this.textItems = textItems;
if ((onlyCalculateSmallestSizeOnce == true && haveCalculatedSmallestFontSizeAtLeastOnce == true))
{
SetFontSizeOfTextItemsAsSmallest();
}
else
{
ResetBestFitDone();
}
}
void ResetBestFitDone()
{
hasSetAllFontSizeSameSinceBestFitDone = false;
returningFromSmallestFontEnumeratorYield = false;
}
IEnumerator SetTextSizesToSmallest()
{
SetFontSizeOfTextItemsAsLarge();
returningFromSmallestFontEnumeratorYield = true;
yield return 0;
returningFromSmallestFontEnumeratorYield = false;
smallestFontSize = fontSizeNotSet;
bool updateResult = UpdateSmallestFontSizeFromTextItems();
if (updateResult == true)
{
SetFontSizeOfTextItemsAsSmallest();
hasSetAllFontSizeSameSinceBestFitDone = true;
haveCalculatedSmallestFontSizeAtLeastOnce = true;
}
else
{
smallestFontSize = fontSizeNotSet;
}
}
bool UpdateSmallestFontSizeFromTextItems()
{
foreach (Text item in textItems)
{
if (item == null)
{
return false;
}
bool updateResult = UpdateSmallestFontSizeFromOneTextItem(item);
if (updateResult == false)
{
return false;
}
}
return true;
}
bool UpdateSmallestFontSizeFromOneTextItem(Text item)
{
int fontSize = item.cachedTextGenerator.fontSizeUsedForBestFit;
if (fontSize == 0)
{
return false;
}
if (smallestFontSize == fontSizeNotSet || fontSize < smallestFontSize)
{
smallestFontSize = fontSize;
}
return true;
}
void SetFontSizeOfTextItemsAsSmallest()
{
float multiplier = 1f / canvas.scaleFactor;
foreach (Text item in textItems)
{
item.resizeTextMaxSize = (int)Mathf.Floor(smallestFontSize * multiplier) + 1; // Even if fonts won't all be identical by adding 1, it offers a good balance between readability and sameness in many cases.
}
}
void SetFontSizeOfTextItemsAsLarge()
{
float multiplier = 1f / canvas.scaleFactor;
foreach (Text item in textItems)
{
item.resizeTextMaxSize = (int)Mathf.Floor(100 * multiplier);
}
}
}