unity UGUI 根據text中文本的長度改變text的大小

有我沒寫明白的,歡迎評論。會不斷更新我的博客代碼。

只做點題用,需要使用在其他地方的自己擴展。

其中 (str.Length / 10+1) * 16 10爲一行可容納的10個漢字。16爲一行所佔的高,自行測試你的字體。

    Text text;
    RectTransform recttrans;

    private void Start()
    {
        text = GetComponent<Text>();

        recttrans = GetComponent<RectTransform>();

        string str = text.text;

        Debug.Log(str.Length);

        recttrans.sizeDelta = new Vector2(recttrans.sizeDelta.x, (str.Length / 10+1) * 16);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章