Hello Guest

Author Topic: How to get pixel width of textmesh  (Read 14264 times)

TwistedSage

  • Newbie
  • *
  • Posts: 15
    • View Profile
How to get pixel width of textmesh
« 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

TwistedSage

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to get pixel width of textmesh
« Reply #1 on: December 12, 2014, 04:43:56 pm »
Also. I have a 2 pixel border but it disappears at certain scales. Is it too small?

TwistedSage

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to get pixel width of textmesh
« Reply #2 on: December 12, 2014, 04:50:01 pm »
Basically I'm just asking for the best way to add a background and a border to at text mesh :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get pixel width of textmesh
« Reply #3 on: December 14, 2014, 03:59:12 pm »
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.

TwistedSage

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to get pixel width of textmesh
« Reply #4 on: December 14, 2014, 04:02:13 pm »
Ahh that's probably it. Where do I find pixels per world unit?

Thanks in advance
Odin

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get pixel width of textmesh
« Reply #5 on: December 14, 2014, 04:21:43 pm »
Its in the sprite collection settings for the sliced sprite you're using

TwistedSage

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to get pixel width of textmesh
« Reply #6 on: December 14, 2014, 09:08:59 pm »
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

Code: [Select]
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);
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get pixel width of textmesh
« Reply #7 on: December 14, 2014, 09:20:45 pm »
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

TwistedSage

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to get pixel width of textmesh
« Reply #8 on: December 16, 2014, 03:42:32 pm »
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

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get pixel width of textmesh
« Reply #9 on: December 16, 2014, 05:05:08 pm »
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...

TwistedSage

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to get pixel width of textmesh
« Reply #10 on: December 16, 2014, 06:14:06 pm »
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

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get pixel width of textmesh
« Reply #11 on: December 17, 2014, 01:00:52 am »
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.

TwistedSage

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to get pixel width of textmesh
« Reply #12 on: December 17, 2014, 07:24:17 am »
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

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get pixel width of textmesh
« Reply #13 on: December 17, 2014, 09:10:17 am »
Something like that
Code: [Select]
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);

TwistedSage

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to get pixel width of textmesh
« Reply #14 on: December 17, 2014, 02:28:56 pm »
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