using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; [RequireComponent(typeof(Text))] public class SetTextWidth : MonoBehaviour { // Use this for initialization public float maxWidth; public float minWidth; private Text text; private float width; void Start () { } private void Awake() { text = GetComponent(); } // Update is called once per frame void Update () { var newWidth = text.preferredWidth; if (newWidth!= width) { setControlWidth(); width = newWidth; } } private void setControlWidth() { if (text.preferredWidth > maxWidth) { text.rectTransform.sizeDelta = new Vector2(maxWidth, text.rectTransform.sizeDelta.y); } else if (text.preferredWidth < minWidth) { text.rectTransform.sizeDelta = new Vector2(minWidth, text.rectTransform.sizeDelta.y); } else { text.rectTransform.sizeDelta = new Vector2(text.preferredWidth, text.rectTransform.sizeDelta.y); } } }