Author Topic: How to adjust drawingDimensions of a UISprite/UILabel by code  (Read 8018 times)

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
How to adjust drawingDimensions of a UISprite/UILabel by code
« on: December 10, 2013, 01:34:16 AM »
in UISprite, the drawing dimension is read only.....
Actually not just the dimension, it is very common for us to change attribute of a widget by code,  such as depth, pivot, color,atlas, sprite, sprite type, fill center etc.

Is there any where I can find information of how to change them by code?

<suggestion>
It would be nice to have such feature added. as I find a lot of user here is looking for change those value.
For my case, I spent 2 hours, from documentation, script, forum, google, and cannot find a way to change those value on the fly. So it would be very great if Ngui have a mode that when I change any value, Ngui will tell me what is variable is changed, and how can I get it changed by code as well. If this is not possible write a remark of each field in the editor to how to change that value by code. If no possible write it in the help file, so that I right click on the widget click help, then I know how to adjust those value....
very much appreciated.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to adjust drawingDimensions of a UISprite/UILabel by code
« Reply #1 on: December 10, 2013, 01:38:28 AM »
UIWidget.SetRect lets you do just that.

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: How to adjust drawingDimensions of a UISprite/UILabel by code
« Reply #2 on: December 10, 2013, 01:51:17 AM »
I tried already...cannot work..with error  'SetRect' is not a member of 'UISprite' ;

  1. var w:float=GO.GetComponentInChildren(UILabel).drawingDimensions.w;
  2.         var x:float=GO.GetComponentInChildren(UILabel).drawingDimensions.x;
  3.         var y:float=GO.GetComponentInChildren(UILabel).drawingDimensions.y;
  4.         var z:float=GO.GetComponentInChildren(UILabel).drawingDimensions.z;
  5.  
  6.         var sprite = GetComponent(UISprite);
  7.        
  8.  
  9.         sprite.SetRect( x,  w, Mathf.Abs(w-y), Mathf.Abs(x-z));

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: How to adjust drawingDimensions of a UISprite/UILabel by code
« Reply #3 on: December 10, 2013, 01:55:35 AM »
Basically I am adjusting the background of a button (UISprite) to auto resize according to the length of its text (UILabel)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to adjust drawingDimensions of a UISprite/UILabel by code
« Reply #4 on: December 10, 2013, 10:13:35 PM »
Err... drawing dimensions are in relative-to-transform coordinates. They don't even include the widget's position.

You need to transform the final values by the widget's transform to get world space coordinates. You then need to transform those values by the inverse of the transform's parent you're attempting to position, or you won't be able to do so properly.

To get the background to auto-resize and envelop the text, simply set the sprite to use the label as the anchor's target, and specify the desired padding. No code necessary.

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: How to adjust drawingDimensions of a UISprite/UILabel by code
« Reply #5 on: December 31, 2013, 04:55:36 AM »
Thx a lot! It work well with UIStretch.
Just add UIstretch in the UISprite (The alert background), target it reference to the child UILabel (the alert text) and then set a negative padding no to the UIStretch.