Author Topic: [SOLVED] Strange behaviour of CalcAbsWidgetBounds (Same Max&Min, Size=0,0,0)  (Read 2563 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Hello, I have a very simple Icon prefab, which is just a gameObject with a UISprite script attached to it. It has a scale of 1,1,1.
I instantiate it dynamically:
  1.  
  2. private void InitIcon(Texture t)
  3. {
  4.         GameObject go = NGUITools.AddChild(gameObject, inventory.iconTemplate);
  5.         icon = go.GetComponent<UISprite>();
  6.         icon.spriteName = texture.name;
  7. }
  8.  

And then I need to get its size in pixels, which should be about (0.8, 0.8) so I use:
  1. bounds = NGUIMath.CalculateAbsoluteWidgetBounds(trans);
  2. var sMax = cam.WorldToScreenPoint(bounds.max);
  3. var sMin = cam.WorldToScreenPoint(bounds.min);
  4. return sMax - sMin;
  5.  

This has worked for me very well, but for some reason, in this situation, it's not working right, it's giving me the same bounds.Min and bounds.Max, so bounds.Size is (0, 0, 0) - See attachment.

Any idea why? I really want to use this method - Thanks.
« Last Edit: August 24, 2013, 09:44:42 PM by vexe »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
I'd check to see if there are any active widgets underneath the 'trans'. If not, absolute bounds function will just return trans.position.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
What do you mean active widgets under it? - trans is the transform of 'icon' ^ - which is a UISprite attached to an active gameobject, no widgets underneath it.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
OK, I just read the docs for GetCompsInChildren and read your code in Calculate... and yes indeed there was a disabled object. But now I'm still stuck getting NaNs and Infinity. Problem might be that I'm adding the sprite to a normal game object that's not under a UI panel, that will cause trouble right? - widgets must be children to some panel, correct?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Yes. You should not add widgets to random objects like that. They need to be added under a UI hierarchy.