Hello Guest

Author Topic: GetMeshDimensionsForString not returning height of textmesh  (Read 7891 times)

Drips

  • Newbie
  • *
  • Posts: 9
    • View Profile
GetMeshDimensionsForString not returning height of textmesh
« on: February 02, 2013, 01:44:26 am »
I may be missing something but,

GetMeshDimensionsForString is returning the same value for all strings i pass it.

I am trying to detect the number of lines of text present in the textmesh, so i can cut it off at a set line and show the rest of the text on the next page.

This is my function:

Code: [Select]
//Returns true if the next word will fit on the current line.
private bool isFitOnLine(string nextText)
{
float currentTextHeight = eventText.GetMeshDimensionsForString(eventString).y;

Debug.Log("isFitOnLine: " + eventString + " " + eventText.GetMeshDimensionsForString(eventString).y.ToString());

if(currentTextHeight != eventText.GetMeshDimensionsForString(nextText).y)
{
Debug.Log("isFitOnLine: false");
return false;
}

Debug.Log("isFitOnLine: " + nextText + " " + eventText.GetMeshDimensionsForString(nextText).y.ToString());

return true;
}

For all my traces it is giving me -0.1 for GetMeshDimensionsForString height. I can see the text is changing in the textmesh and is definitely going onto multiple lines.

Halp.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: GetMeshDimensionsForString not returning height of textmesh
« Reply #1 on: February 02, 2013, 09:43:44 am »
GetMeshDimensionsForString doesn't take line wrapping into consideration currently. The way that works is that \n is inserted in the string at appropriate places, and the rest of the string function would Just Work without knowing a thing about wrapping.

All of this would be fine if you had a way to get this "formatted" string, but there isn't right now.

What I can think off for now is to modify FormatText so it returns a value instead of modifying inline. Or create a duplicate implementation which does that.

That way you can do GetMeshDimensionsForString(FormatStringEx(...)). There is a reason its done like this, but I will need to support your use case too. I've added to the todo for the next release.

p.s. y = negative as ydown = negative in Unity. I'll fix that too, perhaps with a new function.

Drips

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: GetMeshDimensionsForString not returning height of textmesh
« Reply #2 on: February 04, 2013, 05:18:29 am »
thanks for the reply i ended up modifying FormatText to return the number of new line characters present in the string.

roaet

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: GetMeshDimensionsForString not returning height of textmesh
« Reply #3 on: February 07, 2013, 03:40:52 am »
@Drips: what did you do in GetMeshDimensionsForString to get it to work? I'm apparently incapable of getting this one working.
Code: [Select]
public Vector2 GetMeshDimensionsForString(string str)
{
str = FormatText(str);
Debug.Log (str);

int newlines = str.Length - str.Replace("\n","").Length;
Debug.Log("There are " + newlines.ToString() + " newlines");

Returns the expected amount of new lines but the mesh isn't updating the size properly.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: GetMeshDimensionsForString not returning height of textmesh
« Reply #4 on: February 07, 2013, 07:16:59 pm »
I'm investigating this. If necessary I'll create a new function to return something a bit more sensible for you to use.

edit: The current function is a remnant of how the system used to work.
« Last Edit: February 07, 2013, 07:18:34 pm by unikron »

Madrayken

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: GetMeshDimensionsForString not returning height of textmesh
« Reply #5 on: August 06, 2013, 11:00:12 pm »
Did this ever get resolved? I'm running into the same problems now, and the neither the tooltips, nor the autocomplete options indicate that this is any different to how it was back in Frebruary.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: GetMeshDimensionsForString not returning height of textmesh
« Reply #6 on: August 07, 2013, 09:55:58 am »
It isn't. Kinda got started fixing it but ultimately decided against it as the code is still used by the system. We ended up changing a lot of code around it and didn't want to risk too much. What exactly would be useful here? The physical mesh bounds assuming the text is rendered on screen?

Madrayken

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: GetMeshDimensionsForString not returning height of textmesh
« Reply #7 on: August 08, 2013, 05:40:15 pm »
Yes. In general, this function (or one like it) would return the size of the mesh so we could, for example, scale a speech bubble mesh around it. At the moment, this is actually impossible, and means I'm looking for other solutions with other packages (which is just silly). At the moment, the existing function just lies!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: GetMeshDimensionsForString not returning height of textmesh
« Reply #8 on: August 08, 2013, 10:41:00 pm »
It doesn't lie, it just returns the information the internals need to draw the mesh and nothing else. It was an internal function that was (probably incorrectly) exposed.

Add this function to tk2dTextMesh return an estimated bounds
Code: [Select]
/// <summary>
/// Calculates an estimated bounds for the given string if it were rendered
/// using the current settings.
/// </summary>
public Bounds GetEstimatedMeshBoundsForString( string str ) {
tk2dTextGeomGen.GeomData geomData = tk2dTextGeomGen.Data( data, _fontInst, _formattedText );
Vector2 dims = tk2dTextGeomGen.GetMeshDimensionsForString(str, geomData);
float offsetY = tk2dTextGeomGen.GetYAnchorForHeight(dims.y, geomData);
float offsetX = tk2dTextGeomGen.GetXAnchorForWidth(dims.x, geomData);
float lineHeight = (_fontInst.lineHeight + data.lineSpacing) * data.scale.y;
return new Bounds( new Vector3(offsetX + dims.x * 0.5f, offsetY + dims.y * 0.5f + lineHeight, 0), Vector3.Scale(dims, new Vector3(1, -1, 1)) );
}

You'll also need to make GetYAnchorForHeight & XAnchorForWidth public, but that should be it mostly.

Madrayken

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: GetMeshDimensionsForString not returning height of textmesh
« Reply #9 on: August 08, 2013, 11:04:12 pm »
Cool! Thanks for that! I really didn't want to add a bloated gui/text library just for this one thing.