Author Topic: Easier way to get Screen Rect of a Panel  (Read 3679 times)

vbalbio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Easier way to get Screen Rect of a Panel
« on: June 25, 2014, 08:35:34 PM »
Hi guys, i'm using Ngui but i need to get the Rect in screen coords of a Panel to send to another system.

I wrote this code below but it have limitations such it only works when using a Panel with center X=0,Y=0
There's a easier way to get it? I spend one day work to understand how NGUI works and make this code, looks like NGUI don't like too much give to us space screen coords, it's really sad for a GUI system.  :(

  1.  
  2. function GetPanelRect()
  3. {
  4.        
  5.         var Root = GUIRoot.GetComponent(UIRoot); // The Root of the GUI
  6.         var UI_RootHeight = Root.manualHeight;
  7.         var UI_RootWidth = UI_RootHeight * (parseFloat(Screen.width)/Screen.height);
  8.        
  9.         // Need to Keep Center X = 0 e Y = 0
  10.         var Panel = transform.GetComponent(UIPanel);
  11.         // Local Coordinates
  12.         var UI_PanelLocalwidth = (Panel.GetSides(transform)[2][0])*2;
  13.         var UI_PanelLocalheight = (Panel.GetSides(transform)[1][1])*2;
  14.        
  15.         // Screen Coordinates
  16.         var UI_PanelScreenWidth = (UI_PanelLocalwidth*Screen.width)/ UI_RootWidth;
  17.         var UI_PanelScreenHeight = (UI_PanelLocalheight*Screen.height)/ UI_RootHeight; 
  18.        
  19.         var View = GameObject.Find("GUI_Cam").camera.WorldToScreenPoint(transform.position);
  20.        
  21.         var FinalRect = new Rect(View[0],View[1],UI_PanelScreenWidth,UI_PanelScreenHeight);
  22.         return FinalRect;
  23. }
  24.  
  25.  
« Last Edit: June 25, 2014, 08:48:11 PM by vbalbio »

vbalbio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Easier way to get Screen Rect of a Panel
« Reply #1 on: June 25, 2014, 09:08:29 PM »
For record i fixed the Center limitation, now it's working nice. but i yet think it's too much code for a simple GetRect function. If anyone have a better solution post please, if anyone need to use some like that, here's the code i'm using now:

  1. function GetPanelRect(){
  2.        
  3.         // Root NGUI object, set it in Editor, GUIroot is a public GameObject
  4.         var Root = GUIRoot.GetComponent(UIRoot);
  5.         var UI_RootHeight = Root.manualHeight;
  6.         var UI_RootWidth = UI_RootHeight * (parseFloat(Screen.width)/Screen.height);
  7.        
  8.         // The Panel Component of this object
  9.         var Panel = transform.GetComponent(UIPanel);
  10.        
  11.         // Local Coordinates
  12.         var ClipRegion = Panel.finalClipRegion;
  13.         var UI_PanelLocalwidth = ClipRegion[2]; // Panel Width
  14.         var UI_PanelLocalheight = ClipRegion[3]; // Panel Heigth
  15.        
  16.         // Screen Coordinates
  17.         var UI_PanelScreenWidth = (UI_PanelLocalwidth*Screen.width)/ UI_RootWidth;
  18.         var UI_PanelScreenHeight = (UI_PanelLocalheight*Screen.height)/ UI_RootHeight; 
  19.        
  20.         var View = GameObject.Find("GUI_Cam").camera.WorldToScreenPoint(transform.position);
  21.        
  22.         // Offset to Screen Coordinates
  23.         var OffsetX = ClipRegion[0]*Screen.width/UI_RootWidth;
  24.         var OffsetY = ClipRegion[1]*Screen.height/UI_RootHeight;
  25.        
  26.         var FinalRect = new Rect(View[0]+OffsetX,View[1]+OffsetY,UI_PanelScreenWidth,UI_PanelScreenHeight);
  27.         return FinalRect;
  28. }
  29.  

PoN

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 111
    • View Profile
Re: Easier way to get Screen Rect of a Panel
« Reply #2 on: June 25, 2014, 10:00:45 PM »
NGUI has a methods for calculating bounds, so try these methods -    NGUIMath.CalculateAbsoluteWidgetBounds(...) , NGUIMath.CalculateRelativeWidgetBounds(...) .
Worked on Doc&DogAge Of Fury 3D. Actually working on WarMach.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Easier way to get Screen Rect of a Panel
« Reply #3 on: June 26, 2014, 11:01:51 AM »
Screen rect of a panel? Panels only have a rect if they're using clipping. If you want the on-screen dimensions of this rectangle, take the panel's worldCorners and transform them from world space to screen space.

vbalbio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Easier way to get Screen Rect of a Panel
« Reply #4 on: June 26, 2014, 04:41:52 PM »
I'm using Clipping in fact, i'm using it to delimitation of a screen space. Get the corners in world coords can be easier than what i did. Thanks for help, i will take a look on it  ;)