Author Topic: Resize Container to Grid  (Read 7600 times)

Dozer

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Resize Container to Grid
« on: March 17, 2014, 11:09:37 PM »
I just got started with NGUI a few days ago, and have run into my first difficulty.  I have a grid of buttons that changes at runtime, and I need the background sprite of the window which contains this grid to dynamically resize.  I can't seem to figure out the hierarchy of components that will handle this.  Is there an example along these lines somewhere?

An example of what I mean:


If I were to add a Button 5, I would need the bottom section, and the overall window, to resize accordingly.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Resize Container to Grid
« Reply #1 on: March 18, 2014, 03:15:49 PM »
Create a script that will use NGUITools.CalculateRelativeWidgetBounds inside of it, passing the root game object that contains your buttons (but make sure the background is not a part of this hierarchy). Resize the background widget according to the bounds you get back.

Dozer

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Resize Container to Grid
« Reply #2 on: March 19, 2014, 11:27:29 AM »
After some searching, I assume you meant NGUIMath rather than NGUITools.  I tried several different approaches, but nothing is working quite right.  I really need to see an example, because I'm still figuring out the basics of NGUI.  I have to imagine that resizing nested containers to fit dynamic content is a fairly common task.  Is there not a more general approach that doesn't require writing custom scripts?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Resize Container to Grid
« Reply #3 on: March 20, 2014, 01:40:59 AM »
Yes, sorry -- NGUIMath.

Remind me again tomorrow when I am back from GDC and I can give you an example.

Dozer

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Resize Container to Grid
« Reply #4 on: March 21, 2014, 02:44:31 PM »
Just following up about that example.  Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Resize Container to Grid
« Reply #5 on: March 21, 2014, 03:49:31 PM »
  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7.  
  8. /// <summary>
  9. /// Example script that resizes the sprite it's attached to in order to envelop the target content.
  10. /// </summary>
  11.  
  12. [RequireComponent(typeof(UIWidget))]
  13. public class EnvelopContent : MonoBehaviour
  14. {
  15.         public Transform targetRoot;
  16.         public int padLeft = 0;
  17.         public int padRight = 0;
  18.         public int padBottom = 0;
  19.         public int padTop = 0;
  20.  
  21.         void Start () { Execute(); }
  22.  
  23.         [ContextMenu("Execute")]
  24.         public void Execute ()
  25.         {
  26.                 if (targetRoot == transform)
  27.                 {
  28.                         Debug.LogError("Target Root object cannot be the same object that has Envelop Content. Make it a sibling instead.", this);
  29.                 }
  30.                 else if (NGUITools.IsChild(targetRoot, transform))
  31.                 {
  32.                         Debug.LogError("Target Root object should not be a parent of Envelop Content. Make it a sibling instead.", this);
  33.                 }
  34.                 else
  35.                 {
  36.                         Bounds b = NGUIMath.CalculateRelativeWidgetBounds(transform.parent, targetRoot, false);
  37.                         float x0 = b.min.x + padLeft;
  38.                         float y0 = b.min.y + padBottom;
  39.                         float x1 = b.max.x + padRight;
  40.                         float y1 = b.max.y + padTop;
  41.  
  42.                         UIWidget w = GetComponent<UIWidget>();
  43.                         w.SetRect(x0, y0, x1 - x0, y1 - y0);
  44.                 }
  45.         }
  46. }
P.S. UIWidget.SetRect is bugged in 3.5.4 though, and must be changed:
  1.         public void SetRect (float x, float y, float width, float height)
  2.         {
  3.                 Vector2 po = pivotOffset;
  4.  
  5.                 float fx = Mathf.Lerp(x, x + width, po.x);
  6.                 float fy = Mathf.Lerp(y, y + height, po.y);
  7.  
  8.                 int finalWidth = Mathf.FloorToInt(width + 0.5f);
  9.                 int finalHeight = Mathf.FloorToInt(height + 0.5f);
  10.  
  11.                 if (po.x == 0.5f) finalWidth = ((finalWidth >> 1) << 1);
  12.                 if (po.y == 0.5f) finalHeight = ((finalHeight >> 1) << 1);
  13.  
  14.                 Transform t = cachedTransform;
  15.                 Vector3 pos = t.localPosition;
  16.                 pos.x = Mathf.Floor(fx + 0.5f);
  17.                 pos.y = Mathf.Floor(fy + 0.5f);
  18.  
  19.                 if (finalWidth < minWidth) finalWidth = minWidth;
  20.                 if (finalHeight < minHeight) finalHeight = minHeight;
  21.  
  22.                 t.localPosition = pos;
  23.                 this.width = finalWidth;
  24.                 this.height = finalHeight;
  25.  
  26.                 if (isAnchored)
  27.                 {
  28.                         t = t.parent;
  29.  
  30.                         if (leftAnchor.target) leftAnchor.SetHorizontal(t, x);
  31.                         if (rightAnchor.target) rightAnchor.SetHorizontal(t, x + width);
  32.                         if (bottomAnchor.target) bottomAnchor.SetVertical(t, y);
  33.                         if (topAnchor.target) topAnchor.SetVertical(t, y + height);
  34. #if UNITY_EDITOR
  35.                         NGUITools.SetDirty(this);
  36. #endif
  37.                 }
  38.         }

wallabie

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 200
    • View Profile
Re: Resize Container to Grid
« Reply #6 on: March 23, 2014, 06:06:31 AM »
I just downloaded 3.5.5,

Which version of the code above works for 3.5.5 ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Resize Container to Grid
« Reply #7 on: March 24, 2014, 06:34:57 PM »
Neither. It's already a part of 3.5.5.