2D Toolkit Forum
2D Toolkit => Support => Topic started by: Drips 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:
//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.
-
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.
-
thanks for the reply i ended up modifying FormatText to return the number of new line characters present in the string.
-
@Drips: what did you do in GetMeshDimensionsForString to get it to work? I'm apparently incapable of getting this one working.
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.
-
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.
-
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.
-
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?
-
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!
-
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
/// <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.
-
Cool! Thanks for that! I really didn't want to add a bloated gui/text library just for this one thing.