Author Topic: what's the proper way to size a UIDraggablePanel to the screen?  (Read 4673 times)

andrew

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 44
    • View Profile
what's the proper way to size a UIDraggablePanel to the screen?
« on: February 14, 2013, 01:01:51 AM »
I tried adding a UIStretch to the object with the UIDraggablePanel script.

I set the style to BasedOnHeight and then the relative size to a microscopic value until the scale went back to 1 1 1.

This doesn't seem to dynamically scale the draggable panel though when the screen resolution changes.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: what's the proper way to size a UIDraggablePanel to the screen?
« Reply #1 on: February 14, 2013, 02:01:09 PM »
UIStretch only stretches individual widgets, and is not meant to stretch panels. You will need to write your own custom component, as stretching a panel involves changing its clipped rectangle, not scale.

electrodruid

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 48
    • View Profile
Re: what's the proper way to size a UIDraggablePanel to the screen?
« Reply #2 on: February 28, 2013, 08:49:05 AM »
I've just had to write this component myself, based on UIStretch. I thought I'd put the code here in case it's useful to anyone else. The only thing that's a bit of a shame is that it seems to go crazy if you also use a UIAnchor to try to fix the clipping area position relative to another object, and I can't figure out a way to make it play nice. So I can scale panels using relative values, but I still have to place them using pixel transforms.

Usual disclaimers apply about me not having tested this in all possible circumstances, or done any testing to find out just how badly this component affects performance. Use at your own risk...

  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/MyExtensions/Panel Stretch")]
  5. public class UIPanelStretch : MonoBehaviour
  6. {
  7.         public enum Style
  8.         {
  9.                 None,
  10.                 Horizontal,
  11.                 Vertical,
  12.                 Both,
  13.                 BasedOnHeight,
  14.         }
  15.  
  16.         public Camera uiCamera = null;
  17.  
  18.         public UIWidget widgetContainer = null;
  19.  
  20.         public UIPanel panelContainer = null;
  21.         public Style style = Style.None;
  22.         public Vector2 relativeSize = Vector2.one;
  23.  
  24.         UIRoot mRoot;
  25.        
  26.         UIPanel mPanel;         // The panel on the object which we'll be manipulating
  27.        
  28.         void Awake ()
  29.         {
  30.                 // Get the panel
  31.                 mPanel = GetComponent<UIPanel>();
  32.         }
  33.  
  34.         void Start ()
  35.         {
  36.                 if (uiCamera == null) uiCamera = NGUITools.FindCameraForLayer(gameObject.layer);
  37.                 mRoot = NGUITools.FindInParents<UIRoot>(gameObject);
  38.         }
  39.  
  40.         void Update ()
  41.         {
  42.                 if(mPanel == null) return;
  43.  
  44.                 if (style != Style.None)
  45.                 {
  46.                         Rect rect = new Rect();
  47.  
  48.                         if (panelContainer != null)
  49.                         {
  50.                                 if (panelContainer.clipping == UIDrawCall.Clipping.None)
  51.                                 {
  52.                                         // Panel has no clipping -- just use the screen's dimensions
  53.                                         rect.xMin = -Screen.width * 0.5f;
  54.                                         rect.yMin = -Screen.height * 0.5f;
  55.                                         rect.xMax = -rect.xMin;
  56.                                         rect.yMax = -rect.yMin;
  57.                                 }
  58.                                 else
  59.                                 {
  60.                                         // Panel has clipping -- use it as the rect
  61.                                         Vector4 pos = panelContainer.clipRange;
  62.                                         rect.x = pos.x - (pos.z * 0.5f);
  63.                                         rect.y = pos.y - (pos.w * 0.5f);
  64.                                         rect.width = pos.z;
  65.                                         rect.height = pos.w;
  66.                                 }
  67.                         }
  68.                         else if (widgetContainer != null)
  69.                         {
  70.                                 // Widget is used -- use its bounds as the container's bounds
  71.                                 Transform t = widgetContainer.cachedTransform;
  72.                                 Vector3 ls = t.localScale;
  73.                                 Vector3 lp = t.localPosition;
  74.  
  75.                                 Vector3 size = widgetContainer.relativeSize;
  76.                                 Vector3 offset = widgetContainer.pivotOffset;
  77.                                 offset.y -= 1f;
  78.  
  79.                                 offset.x *= (widgetContainer.relativeSize.x * ls.x);
  80.                                 offset.y *= (widgetContainer.relativeSize.y * ls.y);
  81.  
  82.                                 rect.x = lp.x + offset.x;
  83.                                 rect.y = lp.y + offset.y;
  84.  
  85.                                 rect.width = size.x * ls.x;
  86.                                 rect.height = size.y * ls.y;
  87.                         }
  88.                         else if (uiCamera != null)
  89.                         {
  90.                                 rect = uiCamera.pixelRect;
  91.                         }
  92.                         else return;
  93.                        
  94.                        
  95.                         float rectWidth  = rect.width;
  96.                         float rectHeight = rect.height;
  97.                         float adj = (mRoot != null) ? mRoot.pixelSizeAdjustment : 1f;
  98.  
  99.                         if (adj != 1f && rectHeight > 1f)
  100.                         {
  101.                                 float scale = mRoot.activeHeight / rectHeight;
  102.                                 rectWidth *= scale;
  103.                                 rectHeight *= scale;
  104.                         }
  105.                        
  106.                         // In this version we're adjusting the clip range, not the transform.
  107.                         Vector4 clipRange = mPanel.clipRange;
  108.                                
  109.                         // z = width
  110.                         // w = height
  111.                         if (style == Style.BasedOnHeight)
  112.                         {
  113.                                 clipRange.z = relativeSize.x * rectHeight;
  114.                                 clipRange.w = relativeSize.y * rectHeight;
  115.                         }
  116.                         else
  117.                         {
  118.                                 if (style == Style.Both || style == Style.Horizontal)   clipRange.z = relativeSize.x * rectWidth;
  119.                                 if (style == Style.Both || style == Style.Vertical)             clipRange.w = relativeSize.y * rectHeight;
  120.                         }
  121.                        
  122.                         mPanel.clipRange = clipRange;
  123.                 }
  124.         }
  125. }

xushunwang

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: what's the proper way to size a UIDraggablePanel to the screen?
« Reply #3 on: September 23, 2013, 04:37:08 AM »
I used this code , but it seems not work well.
I add this script and DragablePanel and clip in the same gameobject , am I right ? Or how to use it?
« Last Edit: September 23, 2013, 04:42:57 AM by xushunwang »