方式一:
IEnumerator DelayHideToast(Text text)
{
CharacterInfo info = new CharacterInfo();
text.font.RequestCharactersInTexture(text.text, 30);
int allTextWidth = 0;
var arr = text.text.ToCharArray();
foreach (var item in arr)
{
text.font.GetCharacterInfo(item, out info, 30);
allTextWidth += info.advance;
}
Debug.LogFormat("长度:{0}--根据这个值来改变Rect的大小", allTextWidth);
_CaptureModeToast.GetComponent<RectTransform>().sizeDelta = new Vector2(allTextWidth + 55.0f, 40.0f);
yield return new WaitForSeconds(3.0f);
_CaptureModeToast.SetActive(false);
}
方式二:
public void ComputeText(Text text)
{
var width = text.preferredWidth;
var Height = text.preferredHeight;
}