TextMesh Pro characterInfo coordinates

I am having a hard time making sense of the coordinates I get from the characterInfo struct.

I’m using Unity 5.6

I have a TextMeshProUGUI object that is in Paging mode. When it gets to the end (last page) of text, I want to dynamically create a button underneath the last line of text.

To do this, I am getting the coordinates of the last character of text (I’ve also tried getting the baseline of the last line) and moving down on the y-axis a bit and justifying it to the left.

When I get the coords of the last character I do it like this:
Vector3 bottomLeft = mesh.textInfo.characterInfo [mesh.textInfo.characterCount - 1].bottomLeft;

This gives me a result of (56.2, -35.3, 0.0). (My Canvas is 300x300, something I arbitrarily set for now). However, if I manually place an object using the Scene Editor and align it with the last character, Unity tells me its RectTransform coords are (206.8, -63.5, 0).

Why the big discrepancy? What coordinate system does characterInfo use? I’ve tried converting the result I get from characterInfo using camera.WorldtoScreenPoint, camera.ScreentoWorldPoint, and transform.TransformPoint, but none of these result in coords that I can make sense of either.

This is an old question, but hopefully, this helps somebody!

You were on the right track with TransformPoint, but you may have called it on the wrong object, or worked with the result in a way that wasn’t helpful.

Let’s look at your example, above:

Vector3 bottomLeft = mesh.textInfo.characterInfo [mesh.textInfo.characterCount - 1].bottomLeft;

I’ll assume that mesh is your TextMeshProUGUI object, in this case.

You can do this:

Vector3 worldBottomLeft = mesh.TransformPoint(bottomLeft);

…and this will convert your coordinates of interest into world space. Now this number is going to look very strange to you, because your canvas probably has a scaler on it, so the coordinate space you’re used to is not going to correspond with this. If you want to see what this looks like in your canvas space, divide the resulting coordinates by the scale of your canvas.

ANYWAY, once we have those world coords, we should be in business!

Say you want to offset a button called button 50 from the left, and 40 down from your text baseline, something like this should (hypothetically) work:

Vector3 buttonSpacePos = button.transform.parent.InverseTransformPoint(worldBottomLeft);
button.transform.localPosition = new Vector3(50, buttonSpacePos.y - 40, 0);

What we’re doing here is transforming the coordinates into the button’s parent’s space, and then using them to position the button. You could have, alternatively, used the world coordinates without modification, and set button.transform.position, instead, but this would make it harder to figure out your offsets.