Hi Unity community,
I’m facing a problem that I really don’t know how to solve. I have a square button sprite (512x512) which I 9-sliced, so the rounded corners are being preserved when scaling the sprite.
This sprite I turned into a game object and added another game object as its child with a “TextMeshPro - Text” component attached to it. I then have a C# script sitting on my sprite, that has should have the following logic:
set text of child object’s TMP component
grab new bounds of TMP
set sprite size to TMP bounds with an added size offset since the values the TMP bounds output seem way too small
By setting the size that way, I kind of get the button to encompass the text, but it’s a lot of guess work. If my text is very short, I don’t have a problem, but if the text becomes a very long word or sentence, then the size offset in the X-axis I’m applying is insufficient. I could LERP the value between a minimum and maximum offset, but again, it’s guess work, I’d like my logic to be more precise.
The other problem that I’m facing is that the bounds I’m receiving from the TextMeshPro component seem to be incorrect, much smaller than the actual text. In my scene I have a game object “whitepixel” which is a 1x1 pixel, that I scaled a bit in order to see it in my scene. In my C# script I’m setting this object’s position to the minimum position in X of the TMP bounds, and as you can see in my screenshot, the position is always inaccurate.
Would love to learn more about this and would really appreciate some pointers.
I attached a few screenshots, the png of my button, as well as my code.
By the way, I tried the “UI” approach by using a standard button with a TextMeshPro UI child, in combination with a “Horizontal Layout Group” and a “Content Size Fitter”, but that doesn’t give me an option to specify a minimum width on the button.
Thanks a lot!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
//By default, MonoBehaviours are only executed in Play Mode. By adding this attribute, any instance of the MonoBehaviour
//will have its callback functions executed while the Editor is in Edit Mode too.
[ExecuteInEditMode]
public class ResizeTest2 : MonoBehaviour
{
public GameObject line;
public float sizeOffsetX;
public float sizeOffsetY;
public float sizeMinX;
public float sizeMinY;
public string text;
private TMP_Text tmp;
private SpriteRenderer sprRend;
private GameObject childObj;
// Start is called before the first frame update
void Start()
{
Debug.Log("Start");
childObj = transform.GetChild(0).gameObject;
tmp = childObj.GetComponent<TextMeshPro>();
tmp.SetText(text);
tmp.ForceMeshUpdate();
sprRend = gameObject.GetComponent<SpriteRenderer>();
Bounds bounds = tmp.bounds;
Debug.Log("Size" + bounds.size.x.ToString());
float newSizeX = Mathf.Max(sizeMinX, bounds.size.x + sizeOffsetX);
float newSizeY = Mathf.Max(sizeMinY, bounds.size.y + sizeOffsetY
);
Vector2 sprSize = new Vector2(newSizeX, newSizeY);
sprRend.size = sprSize;
line.transform.position = new Vector3(bounds.min.x, 0, 0);
}
// Update is called once per frame
void Update()
{
}
}







