Author Topic: How to get the pixel position and pixel size of Widget ?  (Read 20247 times)

xiaoniaojjj

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
How to get the pixel position and pixel size of Widget ?
« on: September 14, 2012, 10:57:00 PM »
Can I get the pixel width of the Panel object? I want control the panel position for fit the screen size.
=========================================
Thanks for reply,Let explain again;
If i created a Button by NGUI,how to get the Button position(Pixel) on  Screen,you are right, in fact It's the background,  and how to get the width & height(Pixel on screen) of background of Button.  and how to change the position(pixel),
Why we need it? if i want to put the button on left&top or any position on screen,  i want to the button looks always on the left&top of screen when i resize the Screen.
« Last Edit: September 16, 2012, 12:13:24 AM by xiaoniaojjj »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: How to get the Panel pixelwidth?
« Reply #1 on: September 15, 2012, 02:04:53 AM »
Panel's don't have a width. Do you mean size of clipping area? That's UIPanel.clipRange. If you mean the size of the contents of a panel, you need to use NGUIMath.Calculate series of functions.

xiaoniaojjj

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: How to get the Panel pixelwidth?
« Reply #2 on: September 15, 2012, 06:41:44 AM »
Sorry for my bad expression,I mean,how to get the pixel position and pixel size of Panel or Widget on the screen, like the below code:
if(GUI.Button(new Rect(5,60,50,50),"Login hiden"))
in above code, i know the pixel Button's position and size on screen,

and how to  get the same value like GUI.Button of the NGUI widget?
« Last Edit: September 15, 2012, 06:46:09 AM by xiaoniaojjj »

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: How to get the Panel pixelwidth?
« Reply #3 on: September 15, 2012, 11:52:54 AM »
I think this is a word order problem, you aren't looking for the "pixel size" which would be 1, you are looking for the "size in pixels" of the button, which would be the size of the  background I am guessing.

xiaoniaojjj

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: How to get the Panel pixelwidth?
« Reply #4 on: September 15, 2012, 02:44:10 PM »
Thanks for reply,Let explain again;
If i created a Button by NGUI,how to get the Button position(Pixel) on  Screen,you are right, in fact It's the background,  and how to get the width & height(Pixel on screen) of background of Button.  and how to change the position(pixel),
Why we need it? if i want to put the button on left&top or any position on screen,  i want to the button looks always on the left&top of screen when i resize the Screen.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #5 on: September 16, 2012, 01:16:11 AM »
All widgets have UIWidget.relativeSize which tells you the size in relative values. Vector3.Scale(widget.relativeSize, widget.localScale) tells you the size in pixels.

Or you can just use NGUIMath.CalculateRelativeWidgetBounds function like I suggested earlier. That one works for a group of widgets as well.

petrucio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #6 on: February 06, 2013, 01:10:36 AM »
Major Necromancy here, but I'm posting it here in case it helps anyone (maybe myself from the future, googling the same problem again...)

If you want to get the pixel coordinates exactly like the OnGUI coordinates work:

  1.                 Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(UICamera.current.transform, this.transform);
  2.                 float x = bounds.center.x + Screen.width  / 2 - bounds.size.x / 2;
  3.                 float y = Screen.height - (bounds.center.y + Screen.height / 2 + bounds.size.y / 2);
  4.                 float w = bounds.size.x;
  5.                 float h = bounds.size.y;
  6.  

Or, probably more useful, if you want to get them in relation to NGUI camera coordinates (the origin is at screen center, not top-left):
  1.                 float x = bounds.center.x - bounds.size.x / 2;
  2.                 float y = bounds.center.y + bounds.size.y / 2;
  3.  

Took me a while to get to these, too. Seems most on the NGUI documentation is really sparse, and if it doesn't tell you what you want to know, you are left with trial and error until you find it, or posting here.

petrucio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #7 on: February 06, 2013, 02:03:19 AM »
Actually, UICamera.lastTouchPosition seems to return a value with origin in screen bottom-left. If you want to use that (like me), this is what you want for widget origin in pixels:

  1.         float x = bounds.center.x + Screen.width  / 2;     // (- bounds.size.x / 2) if you want top-left widget origin
  2.         float y = bounds.center.y + Screen.height / 2;     // (+ bounds.size.y / 2) if you want top-left widget origin
  3.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #8 on: February 06, 2013, 11:51:17 AM »
NGUIMath.CalculateAbsoluteWidgetBounds gives you the world-space bounds, from which you can get the 4 points (corners). Transform each of those 4 into screen space using the camera -- cam.WorldToScreenPoint(), and there you go, screen space coordinates without any necromancy.

petrucio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #9 on: February 06, 2013, 08:33:02 PM »
Yeah that seemed obvious enough and was in fact my very first try, but it didn't seem to work at the time. Now that you mentioned it, I went and retested it, and have now found out that it seems to be using another UICamera I have setup in my scene, which has no components involved in the current event whatsoever, to do it's calculations - if I disable it, or change the size and viewport of that camera to be the same as the proper UICamera that should be used, I get the correct values.

So I assumed I was the one doing something wrong and not NGUI, and proceeded to find another way. Turns out I was right all along.

So I guess you have a bug to fix... sorry about that :)

Oh, and the necromancy comment was only because I was reviving a 6 months old dead thread, not because of any code.

petrucio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #10 on: February 06, 2013, 08:51:03 PM »
Done some more testing, and figured out that UICamera.current is not really pointing to the camera that generated the event I'm handling as I expected.

If I expose a UICamera to the inspector, and do a
  1. UICamera usedCam = this.cam ? this.cam : UICamera.current;
, everything works as expected. So it seems that UICamera.current may be bugged?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #11 on: February 06, 2013, 10:25:51 PM »
UICamera.current does indeed point to the camera that sent out an input event. Is your logic actually being triggered from an input event, such as a click? Any other place, and it won't work.

You can always use UICamera.FindCameraForLayer if your game object layers are unique.

petrucio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #12 on: February 06, 2013, 11:00:55 PM »
Yes, indeed I had doubled checked that my logic was being triggered by an input event before posting that. It runs on either an OnClick or OnDrag, both with the same results and the wrong camera.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,151
  • Toronto, Canada
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #13 on: February 07, 2013, 09:55:57 AM »
Do you have some old version of NGUI or something? (like the Free one?)

Current version of NGUI sets UICamera.current in UICamera's Update() function -- before all events get sent out.

petrucio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: How to get the pixel position and pixel size of Widget ?
« Reply #14 on: February 07, 2013, 10:01:09 AM »
2.2.2 (non-free)