Skip to content

Instantly share code, notes, and snippets.

@AldeRoberge
Created January 29, 2023 19:26
Show Gist options
  • Save AldeRoberge/f755d9eeff609a5ddc7131cb4dfbc629 to your computer and use it in GitHub Desktop.
Save AldeRoberge/f755d9eeff609a5ddc7131cb4dfbc629 to your computer and use it in GitHub Desktop.
Ajust size of content to TextMeshPro size
public class ChatBubbleController : MonoBehaviour
{
[SerializeField] private Image _background;
[SerializeField] private TextMeshProUGUI _text;
public void SetText(string text)
{
_text.text = text;
_text.ForceMeshUpdate();
UpdateSize();
}
private void UpdateSize()
{
var bounds = _text.GetRenderedValues(false);
_background.rectTransform.sizeDelta = new Vector2(bounds.x + 0.5f, bounds.y + 0.25f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment