Author :JerryYang
Create by 2020.12.18
环境:
Unity:2019.4.2f1
动态加载文字的时想根据文字的内容设置Text对象的尺寸,怎么做呢?
这里有两个重要的属性Text.preferredWidth和Text.preferredHeight,分别表示在有足够的空间下,Text被分配的宽度和高度。这里我们是固定宽度,所以只需要Text.preferredHeight属性。
using UnityEngine;
using UnityEngine.UI;
public class LoopMailBar: MonoBehaviour
{
public GameObject mailTextObj;
public Transform mailParent;
void Start()
{
GameObject mail = Instantiate<GameObject>(mailTextObj, mailParent, false);
Text mailText = mail.GetComponent<Text>();
mailText.text = "Jerry successfully redeemed <color=#E7B22B>$300</color> Paypal Gift Card";
RectTransform rect = mail.GetComponent<RectTransform>();
Vector2 v2 = rect.rect.size;
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, mailText.preferredWidth);
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, v2.y);
}
}
邮箱轮播 · 跑马灯
赠:Win + Shift + S 可以截屏
网友评论