Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Majicpanda on March 10, 2013, 09:18:16 PM

Title: Dynamic Sized Sliced Sprite BG Based On Text?
Post by: Majicpanda 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.
Title: Re: Dynamic Sized Sliced Sprite BG Based On Text?
Post by: dlewis 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).
Title: Re: Dynamic Sized Sliced Sprite BG Based On Text?
Post by: jeldrez 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.  
Title: Re: Dynamic Sized Sliced Sprite BG Based On Text?
Post by: ArenMook on April 10, 2013, 09:01:23 PM
Select the panel managing your sprite and uncheck the "static" checkbox.
Title: Re: Dynamic Sized Sliced Sprite BG Based On Text?
Post by: jeldrez 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. }