1. 程式人生 > >Unity-自適應彈窗(根據文字長度改變Toast)

Unity-自適應彈窗(根據文字長度改變Toast)

    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);
    }