效果如图所示
实现:
1.Text添加ContentSizeFitter组件,如图所示:
2.Button添加脚本TextAutoSize
public class TextAutoSize : MonoBehaviour
{
public RectTransform textRect;//Text组件
// Start is called before the first frame update
void Start()
{
LayoutRebuilder.ForceRebuildLayoutImmediate(textRect);
this.GetComponent<RectTransform>().sizeDelta = new Vector2(textRect.sizeDelta.x + 30, 40);
}
// Update is called once per frame
void Update()
{
}
}