Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: rinesh on October 03, 2016, 01:53:29 AM

Title: UIWidget width and height
Post by: rinesh on October 03, 2016, 01:53:29 AM
I want to get the width and height of the UIWidget to get Rect like the unity GUI. It works on iPhone resolution but return the same width and height for iPad. I even tried scaling the width and height( see commented code

                 
  1.  Rect GetRect()
  2.       {
  3.          UISprite sp = GetComponent<UISprite> ();
  4.          Vector3 pt = sp.transform.TransformPoint (sp.transform.position);
  5.          pt = UICamera.currentCamera.WorldToScreenPoint(pt);
  6.  
  7.          //float width = (touchZone.width*NGUITools.screenSize.x)/touchZone.root.manualWidth;
  8.          //float height = (touchZone.height*NGUITools.screenSize.y)/touchZone.root.activeHeight;
  9.          
  10.          Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(UICamera.currentCamera.transform, sp.transform);
  11.          float width = bounds.size.x;
  12.          float height = bounds.size.y;
  13.  
  14.          Debug.LogError("Button Width:"+ width + " Height: " + height);
  15.          Rect rect = new Rect(pt.x - width / 2, pt.y - height / 2, width, height);
  16.          return rect;
  17.       }

This is my UIRoot setup is
Scaling style : Constrained on mobile
Content Widht: 1280  Fit: false
Content Height: 768   Fit: true

Title: Re: UIWidget width and height
Post by: ArenMook on October 06, 2016, 05:49:12 AM
UIWidget.worldCorners gives you the 4 corners in world space. Convert from world to screen space using the UI camera, and you have your 4 screen points.