Created
January 29, 2023 19:26
-
-
Save AldeRoberge/f755d9eeff609a5ddc7131cb4dfbc629 to your computer and use it in GitHub Desktop.
Ajust size of content to TextMeshPro size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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