2D Toolkit Forum
2D Toolkit => Support => Topic started by: TwistedSage on December 12, 2014, 04:14:10 pm
-
Hi. I tried the GetEstimatedMeshBoundsForString, but it's not really working for me.
I want to add a scaled sliced sprite as a border for the text, so I need to scale it up by the size of the text mesh.
But so far no luck :)
Thanks in advance
Odin Jensen
-
Also. I have a 2 pixel border but it disappears at certain scales. Is it too small?
-
Basically I'm just asking for the best way to add a background and a border to at text mesh :)
-
Why isn't GetEstimatedMeshBoundsForSTring working for you? It returns the mesh size in world units, which you'll then need to convert into pixel units to feed into sliced sprite dimensions, i.e. if 100 pixels per world unit = div by 100.
You can also use textMesh.renderer.bounds, which also returns world space size that you'll need to convert as above.
-
Ahh that's probably it. Where do I find pixels per world unit?
Thanks in advance
Odin
-
Its in the sprite collection settings for the sliced sprite you're using
-
Pixels per meter is 1 in the sprite collection.
My sliced sprite is 100x100 with 2 pixel border. I have a hard time adding the background and getting the size.
As you can see from below code I kinda hacked it to work, but of course now I need to add boxes exactly after each other and this fails.
If at all you have the time, I would appreciate some pointers. Main issue is probably if the sliced sprite is 100x100 with a 2 pixel border how do I make it fit the size of my text mesh?
Thanks in advance
Odin
public static void AddBackground(GameObject textObject)
{
// Instantiate object
GameObject instance = Instantiate(Resources.Load("Borderback", typeof(GameObject))) as GameObject;
// Get camera
Camera cam = tk2dCamera.Instance.camera;
instance.transform.position = textObject.transform.position;
instance.transform.position = new Vector3 (instance.transform.position.x-10, instance.transform.position.y-6, 1);
instance.transform.parent = textObject.transform;
Bounds extends = textObject.GetComponent<tk2dTextMesh>().GetEstimatedMeshBoundsForString(textObject.GetComponent<tk2dTextMesh>().text);
Vector2 oldDims = instance.GetComponent<tk2dSlicedSprite> ().dimensions;
//Debug.Log (oldDims.x);
float extendsWidth = (extends.extents.x);
Debug.Log (extendsWidth);
float mulFac = 1.0f;
if (textObject.GetComponent<tk2dTextMesh>().text.Length < 3)
{
instance.transform.position = new Vector3 (instance.transform.position.x-12, instance.transform.position.y, 1);
mulFac = 1.5f;
}
instance.GetComponent<tk2dSlicedSprite> ().dimensions = new Vector2 ((73 * (extendsWidth / oldDims.x)+10)*mulFac , extends.extents.y*1.5f);
}
-
Looks far too complicated? Lets say you get the bounds of the text using Renderer.bounds
The dimensions of the sliced sprite should be exactly the bounds width & height to match the size of the text mesh.
The next stage is to work out where it should be, but if you set the sliced sprite anchor to middle center, you can simply set transform.position = the position of the renderer.bounds
-
Haha yeah. You wouldn't think I've been programming for 25 years ;)
Maybe I don't understand the sliced sprite. Should it be border size (2 pixels) and just 1 pixel in the middle. Or can it be any size anf then adjusted somewhere? Because right now my sliced sprite is 100x100. Will this throw it off? And do I need to specify it somewhere?
Best regards
Odin
-
Dimensions doesn't care about the border, middle pixels, or the size of the sliced sprite before setting dimensions. The dimensions is in pixel units, if you're using 1 pixel per meter, then setting dimensions to the renderer.bounds.width and height should result in a sliced sprite that covers the bounding box of the text mesh. Doesn't that work for you? Easiest option is, post a repro case and I'll take a look...
-
Here is an example. Toolkit.cs adds the border. BuildSentence tries to place each word in a string after the other :)
Thanks in advance
Odin
-
Ok :) I found it.
The mesh bounds you get from tk2dTextMesh.GetEstimatedMeshBoundsForString is in local space, and your text mesh is scaled in the hierarchy. You need to scale the mesh bounds before applying it to dimensions. Multiplying by textmesh.transform.lossyScale should do the job.
-
Thanks. Can you perhaps show me in the repo case? Even though I apply the scale they are still not moved far enough and I can't see a pattern in how. It's not like it's always half their size they're moved (because the are centered). It's really weird.
Thanks a lot for your kind help so far ;)
/Odin
-
Something like that
Bounds extends = textObject.GetComponent<tk2dTextMesh>().GetEstimatedMeshBoundsForString(textObject.GetComponent<tk2dTextMesh>().text);
extends.center = textObject.transform.position + Vector3.Scale(extends.center, textObject.transform.lossyScale);
extends.size = Vector3.Scale(extends.size, textObject.transform.lossyScale);
instance.transform.position = extends.center;
instance.GetComponent<tk2dSlicedSprite>().dimensions = new Vector2 (extends.size.x, extends.size.y);
-
That took care of the sliced sprite issue. I multiplied x by 1.5f because it was a bit too tight.
But that sorta worked before. The real problem is how I place them next to each other.
It's in BuildSentence.cs in the function BuildWordInMesh.
Again thank for all your help. Best support ever when this is working please allow me to send you a present ;)
Regards
Odin
-
You're doing that wrong, you're offsetting each word by the length of the extents (which is wrong btw, you should just return the bounds from AddBackground, because of all that is accounted for in there).
You want to set the nextOffset to bounds.max.x, which should place it immediately after the first text.
Second issue is your anchor for the text mesh is the middle center, in order to position the text mesh, you will first need to figure out the size of the string, etc. If you set anchor to bottom left, the position = the bottom left, i.e. you dont need to know the width of the new text mesh to determine its position, i.e. its position = previousBounds.max.x + wordSpacing.