实现功能:
Text在达到最大长度前,gameObject会自动变大,适应文字长度
gameObject达到最大后,gameObject不再变大,Text文字自适应减小
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
[RequireComponent(typeof(ContentSizeFitter))]
public class TextBestFit : MonoBehaviour {
public float MaxWidth = 450f;
private Text text;
private ContentSizeFitter contentSizeFitter;
void Start () {
text = GetComponent<Text>();
contentSizeFitter = GetComponent<ContentSizeFitter>();
}
private void Update()
{
if (text.preferredWidth <= MaxWidth)
{
text.resizeTextForBestFit = false;
contentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
}
else
{
RectTransform tran = text.GetComponent<RectTransform>();
tran.sizeDelta = new Vector2(MaxWidth, tran.sizeDelta.y);
text.resizeTextForBestFit = true;
contentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
}
Debug.Log(text.preferredWidth);
}
}
最新代码见github中的[TextBestFit](https://link.zhihu.com/?target=https://github.com/Hgithubcao/UnityPractice/tree/master/Assets/TextBestFit)场景