Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Setting the vertical size of a Sprite object equal to the vertical size of a TextMesh?

Discussion in 'Scripting' started by tytbone, Apr 17, 2015.

  1. tytbone

    tytbone

    Joined:
    Jul 16, 2013
    Posts:
    18
    I'm trying to create a background color for a TextMesh (to make the text easier to read against the main game background) which involves re-sizing a separate white gameobject/sprite to fit the size of the TextMesh.

    I seem to be able to get the horizontal/x size correct but for some reason the vertical/y size never matches up with the vertical size of the TextMesh; it's always too high or too low, seemingly never equal to the bottom-most part of the TextMesh. (I assume there's something wrong in the calculations or a better way to do this.)

    Shown on the attached picture, I would like the white background to line up with the bottom of the text.

    I've experimented with various code but this is what I currently have:

    1. public void ShowDialog()
    2. {
    3. ...
    4. Vector3 punkTalkPosNew = new Vector3(punkDialog.transform.position.x, punkDialog.transform.position.y, 0);

    5. dialogBackground.transform.position = punkTalkPosNew;
    6. dialogBackground.transform.localScale = new Vector3(punkDialog.renderer.bounds.extents.x,
    7. punkDialog.renderer.bounds.size.y, 0);
    8. ...
    9. }
    Originally I was just drawing the text in Photoshop and have it show up as Sprites; should I stick with that instead? Or what's the best way to go about this? Thanks.

    (PS I'd post this on http://answers.unity3d.com/ to avoid bothering forum-ers but my past questions have never received any responses there.)
     

    Attached Files:

    • text.jpg
      text.jpg
      File size:
      21.8 KB
      Views:
      682
  2. tytbone

    tytbone

    Joined:
    Jul 16, 2013
    Posts:
    18
    I guess I'll stick to using text created in Photoshop (for now) since I know how to do that, but I'd still appreciate somebody trying to explain what's happening here.