Author Topic: Dynamic Sized Sliced Sprite BG Based On Text?  (Read 5032 times)

Majicpanda

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
Dynamic Sized Sliced Sprite BG Based On Text?
« on: March 10, 2013, 09:18:16 PM »
Is it possible to dynamically size a sprite that you have a label on? Ideally I would like to use a sliced sprite that adjusts itself to a set width but scales vertically based on the height the label is taking up. I think UITooltip does this somehow but is there any tutorial on achieving this? Font size etc I would think have to be considered along with lines of text.

Thanks.

dlewis

  • Guest
Re: Dynamic Sized Sliced Sprite BG Based On Text?
« Reply #1 on: March 11, 2013, 03:21:14 AM »
I do this by getting the Y scale of the text then multiplying that by the number of lines I have and adding that to the Y scale of the background/sliced sprite. The way I get the number of lines is using RelativeSize (UILabel), the Y seems to always be a whole number while correlates with the number of lines (for a fixed width UILabel).

jeldrez

  • Sr. Member
  • ****
  • Thank You
  • -Given: 8
  • -Receive: 4
  • Posts: 352
    • View Profile
Re: Dynamic Sized Sliced Sprite BG Based On Text?
« Reply #2 on: April 10, 2013, 06:19:10 PM »
I did this script, but didn't work, the log says the sprite is moving, but nothing happens.


  1. public class SpriteMatchTextLenght : MonoBehaviour
  2. {
  3.    public UILabel textToMatch;
  4.    public UISprite slicedTexture;
  5.      
  6.    void OnGUI ()
  7.    {
  8.       Vector3 textScale = textToMatch.transform.localScale;
  9.       Vector3 textRelSize = textToMatch.relativeSize;
  10.       Vector3 slicedScale = slicedTexture.transform.localScale;
  11.      
  12.       slicedScale.y = (textScale.y*textRelSize.y);
  13.      
  14.       slicedTexture.transform.localScale = slicedScale;
  15.      
  16.       Debug.Log(slicedScale.y);
  17.    }
  18. }
  19.  
  20.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamic Sized Sliced Sprite BG Based On Text?
« Reply #3 on: April 10, 2013, 09:01:23 PM »
Select the panel managing your sprite and uncheck the "static" checkbox.

jeldrez

  • Sr. Member
  • ****
  • Thank You
  • -Given: 8
  • -Receive: 4
  • Posts: 352
    • View Profile
Re: Dynamic Sized Sliced Sprite BG Based On Text?
« Reply #4 on: April 11, 2013, 08:41:58 AM »
Thanks Aren, but it wasn't that.

I was missing adding the actual Y size of the sliced sprite, so I create a public yOffset for this and I can preview the exact size I need.

  1. public class SpriteMatchTextLenght : MonoBehaviour
  2. {
  3.    public UILabel textToMatch;
  4.    public UISprite slicedTexture;
  5.    public float yOffset;//273
  6.      
  7.    void OnGUI ()
  8.    {
  9.       Vector3 textScale = textToMatch.transform.localScale;
  10.       Vector3 textRelSize = textToMatch.relativeSize;
  11.       Vector3 slicedScale = slicedTexture.transform.localScale;
  12.      
  13.       slicedScale.y = (textScale.y*textRelSize.y)+yOffset;
  14.      
  15.       slicedTexture.transform.localScale = slicedScale;
  16.      
  17.       Debug.Log(slicedScale.y);
  18.    }
  19. }