Author Topic: Using widget rect to be used in OnGUI?  (Read 2870 times)

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Using widget rect to be used in OnGUI?
« on: December 23, 2014, 05:14:52 AM »
Hi! I am using plugin that displays web pages in OnGUI loop and requires Rect to be defined. I want to position a ngui widget (or sprite it doesn't matter) so that I can
render OnGUI content on that widget's rect. I have trouble converting ngui's left-top-right-bottom to rect... it should be fairly simple...furthermore, on non pixel perfect setups is it needed to be used UIRoot.GetPixelAdjustments?

Thanks!!

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: Using widget rect to be used in OnGUI?
« Reply #1 on: December 23, 2014, 05:34:00 AM »
Just to answer my own question :)

this should be enough:

  1.         rect.Set(Screen.width - cam.WorldToScreenPoint(Widget.worldCorners[2]).x,
  2.                  Screen.height - cam.WorldToScreenPoint(Widget.worldCorners[2]).y,
  3.                  cam.WorldToScreenPoint(Widget.worldCorners[2]).x - cam.WorldToScreenPoint(Widget.worldCorners[1]).x,
  4.                  cam.WorldToScreenPoint(Widget.worldCorners[1]).y - cam.WorldToScreenPoint(Widget.worldCorners[0]).y);

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: Using widget rect to be used in OnGUI?
« Reply #2 on: December 23, 2014, 12:16:55 PM »
i encountered one more thing, and i think this is the last one to solve so that i can say i get it! :)

unfortunately the plugin i am using (UniWebView, for displaying web pages for mobile devices) uses some kind of inset structure to define on what area
it will render, it is defined like this inset(left,top,right,bottom) so if it's defined like this inset(5,5,5,5) it means that it will render to rect that is 5px from edge of the screen (left,top,right,botom). so to get the rect i got from the widget i tried using something like this:

  1. _webView.insets = new UniWebViewEdgeInsets((int)rect.y,
  2.                                                         Screen.width - (int)rect.width - (int)rect.x,
  3.                                                         (int)rect.y,
  4.                                                         Screen.width - (int)rect.width - (int)rect.x);
  5.  

where rect is defined using widgets dimensions like this:

  1.         rect.Set(Screen.width - cam.WorldToScreenPoint(Widget.worldCorners[2]).x,
  2.                  Screen.height - cam.WorldToScreenPoint(Widget.worldCorners[2]).y,
  3.                  cam.WorldToScreenPoint(Widget.worldCorners[2]).x - cam.WorldToScreenPoint(Widget.worldCorners[1]).x,
  4.                  cam.WorldToScreenPoint(Widget.worldCorners[1]).y - cam.WorldToScreenPoint(Widget.worldCorners[0]).y);
  5.  

this works well if wigder is centered, so insets for top and bottom are the same but i have situation where i need to move widget slightly down to make room
for navigation and in that case i can't get insets for top and bottom right!

i tried using local poisition on the widget,converting it to world coords and subtructing from Widget.worldCorners[0].y but couldn't work it out the proper way.

help!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using widget rect to be used in OnGUI?
« Reply #3 on: December 24, 2014, 07:02:45 AM »
widget.worldCorners already gives you the 4 corners in world space. Use uiCamera.WorldToScreenPoint to convert them to screen space.