1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Events;
- public class BestfitText : Text
- {
- #region
- protected string FormerText;
-
- #endregion
- protected void Update()
- {
-
- }
- protected override void OnPopulateMesh(VertexHelper toFill)
- {
- base.OnPopulateMesh(toFill);
- if (text != FormerText)
- {
- FormerText = text;
-
- StartCoroutine(DelayCall(Bestfit, 2));
- }
- }
- protected void Bestfit()
- {
- TextGenerationSettings textGenerationSettings = GetGenerationSettings(rectTransform.rect.size);
- bool shrink = false;
- bool expand = false;
- int antiCrush = 0;
-
- while (true)
- {
- antiCrush++;
- if (antiCrush > 100)
- {
- Debug.LogError("Crush");
- break;
- }
- float bestHeight = cachedTextGenerator.GetPreferredHeight(text, textGenerationSettings);
-
- if (bestHeight > rectTransform.rect.size.y)
- {
- textGenerationSettings.fontSize--;
- if (expand)
- {
- break;
- }
- shrink = true;
- }
- else if (bestHeight < rectTransform.rect.size.y)
- {
- if (shrink)
- {
- break;
- }
- textGenerationSettings.fontSize++;
- expand = true;
- }
- else if (bestHeight.Equal(rectTransform.rect.size.y))
- {
- break;
- }
- }
-
- fontSize = textGenerationSettings.fontSize;
- }
- protected IEnumerator DelayCall(UnityAction unityAction, int frame)
- {
- while (frame-- > 0)
- {
- yield return null;
- }
- unityAction.SafeInvoke();
- }
- }
|