Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - charliehelman

Pages: 1 [2]
16
NGUI 3 Documentation / Re: UISprite
« on: January 12, 2014, 12:50:35 AM »
You can adjust the widget while keeping its proportions by setting its Aspect Ratio to something other than "Free".
Ah, I see. I messed around for a few minutes with the aspect ratio options earlier but didn't understand why it wasn't working. I must have accidentally set the ratio value, which wasn't clear to me until your response.

Setting the aspect ratio to Free and then back to Width based reverted the ratio value correctly and the sprite then scaled correctly.

Thank you sir.

EDIT: After some more tinkering, I might have discovered a bug which is what caused my confusion.

Swapping out sprites does not update the aspect ratio value. You have to set it to free, to another option, then back to free before the value actually updates. Intuitively I would expect the value to update whenever the sprite is changed.

17
NGUI 3 Documentation / Re: UIGrid
« on: January 11, 2014, 03:53:54 PM »
The problem is when you have a dynamic number of children that change at runtime.. You cannot set this from editor..

Would be great to be able to set the pivot point for the grid

I have the same issue. It would be nice if this functionality was supported by UIGrid. Still, doesn't seem like a workaround would be too complicated. I'll see if I can't get something working myself.

Edit: I modified the UIGrid script to add this functionality. I added a drop down menu to the UIGrid component in inspector for selecting alignment (right, center, and left) and based on that selection modified the local position of the grid accordingly in the Reposition() method. Is it okay if I share it the code?

My additions:
  1.         public enum Alignment
  2.         {
  3.                 Left,
  4.                 Centered,
  5.                 Right,
  6.         }
  7.  
  8.         public Alignment alignment = Alignment.Left;
  9.  
  10. // ...Snip...
  11.  
  12.                 switch (alignment)
  13.                 {
  14.                         case Alignment.Left :
  15.                                 myTrans.localPosition = new Vector3 (0f, myTrans.localPosition.y, myTrans.localPosition.z);
  16.                                 break;
  17.  
  18.                         case Alignment.Centered :
  19.                                 float centerOffset = myTrans.childCount * cellWidth * 0.5f;
  20.                                 centerOffset = -centerOffset + (centerOffset * 1/myTrans.childCount);
  21.                                
  22.                                 myTrans.localPosition = new Vector3 (centerOffset, myTrans.localPosition.y, myTrans.localPosition.z);
  23.                                 break;
  24.  
  25.                         case Alignment.Right :
  26.                                 myTrans.localPosition = new Vector3 (-(myTrans.childCount - 1) * cellWidth, myTrans.localPosition.y, myTrans.localPosition.z);
  27.                                 break;
  28.                 }
  29.  

The entire file:
  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8.  
  9. /// <summary>
  10. /// All children added to the game object with this script will be repositioned to be on a grid of specified dimensions.
  11. /// If you want the cells to automatically set their scale based on the dimensions of their content, take a look at UITable.
  12. /// </summary>
  13.  
  14. [AddComponentMenu("NGUI/Interaction/Grid")]
  15. public class UIGrid : UIWidgetContainer
  16. {
  17.         public delegate void OnReposition ();
  18.  
  19.         public enum Arrangement
  20.         {
  21.                 Horizontal,
  22.                 Vertical,
  23.         }
  24.  
  25.         public enum Alignment
  26.         {
  27.                 Left,
  28.                 Centered,
  29.                 Right,
  30.         }
  31.  
  32.         public Alignment alignment = Alignment.Left;
  33.  
  34.         /// <summary>
  35.         /// Type of arrangement -- vertical or horizontal.
  36.         /// </summary>
  37.  
  38.         public Arrangement arrangement = Arrangement.Horizontal;
  39.  
  40.         /// <summary>
  41.         /// Maximum children per line.
  42.         /// If the arrangement is horizontal, this denotes the number of columns.
  43.         /// If the arrangement is vertical, this stands for the number of rows.
  44.         /// </summary>
  45.  
  46.         public int maxPerLine = 0;
  47.  
  48.         /// <summary>
  49.         /// The width of each of the cells.
  50.         /// </summary>
  51.  
  52.         public float cellWidth = 200f;
  53.  
  54.         /// <summary>
  55.         /// The height of each of the cells.
  56.         /// </summary>
  57.  
  58.         public float cellHeight = 200f;
  59.  
  60.         /// <summary>
  61.         /// Whether the grid will smoothly animate its children into the correct place.
  62.         /// </summary>
  63.  
  64.         public bool animateSmoothly = false;
  65.  
  66.         /// <summary>
  67.         /// Whether the children will be sorted alphabetically prior to repositioning.
  68.         /// </summary>
  69.  
  70.         public bool sorted = false;
  71.  
  72.         /// <summary>
  73.         /// Whether to ignore the disabled children or to treat them as being present.
  74.         /// </summary>
  75.  
  76.         public bool hideInactive = true;
  77.  
  78.         /// <summary>
  79.         /// Whether the parent container will be notified of the grid's changes.
  80.         /// </summary>
  81.  
  82.         public bool keepWithinPanel = false;
  83.  
  84.         /// <summary>
  85.         /// Callback triggered when the grid repositions its contents.
  86.         /// </summary>
  87.  
  88.         public OnReposition onReposition;
  89.  
  90.         /// <summary>
  91.         /// Reposition the children on the next Update().
  92.         /// </summary>
  93.  
  94.         public bool repositionNow { set { if (value) { mReposition = true; enabled = true; } } }
  95.  
  96.         bool mReposition = false;
  97.         UIPanel mPanel;
  98.         bool mInitDone = false;
  99.  
  100.         void Init ()
  101.         {
  102.                 mInitDone = true;
  103.                 mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
  104.         }
  105.  
  106.         void Start ()
  107.         {
  108.                 if (!mInitDone) Init();
  109.                 bool smooth = animateSmoothly;
  110.                 animateSmoothly = false;
  111.                 Reposition();
  112.                 animateSmoothly = smooth;
  113.                 enabled = false;
  114.         }
  115.  
  116.         void Update ()
  117.         {
  118.                 if (mReposition) Reposition();
  119.                 enabled = false;
  120.         }
  121.  
  122.         static public int SortByName (Transform a, Transform b) { return string.Compare(a.name, b.name); }
  123.  
  124.         /// <summary>
  125.         /// Recalculate the position of all elements within the grid, sorting them alphabetically if necessary.
  126.         /// </summary>
  127.  
  128.         [ContextMenu("Execute")]
  129.         public void Reposition ()
  130.         {
  131.                 if (Application.isPlaying && !mInitDone && NGUITools.GetActive(this))
  132.                 {
  133.                         mReposition = true;
  134.                         return;
  135.                 }
  136.  
  137.                 if (!mInitDone) Init();
  138.  
  139.                 mReposition = false;
  140.                 Transform myTrans = transform;
  141.  
  142.                 int x = 0;
  143.                 int y = 0;
  144.  
  145.                 if (sorted)
  146.                 {
  147.                         List<Transform> list = new List<Transform>();
  148.  
  149.                         for (int i = 0; i < myTrans.childCount; ++i)
  150.                         {
  151.                                 Transform t = myTrans.GetChild(i);
  152.                                 if (t && (!hideInactive || NGUITools.GetActive(t.gameObject))) list.Add(t);
  153.                         }
  154.                         list.Sort(SortByName);
  155.  
  156.                         for (int i = 0, imax = list.Count; i < imax; ++i)
  157.                         {
  158.                                 Transform t = list[i];
  159.  
  160.                                 if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;
  161.  
  162.                                 float depth = t.localPosition.z;
  163.                                 Vector3 pos = (arrangement == Arrangement.Horizontal) ?
  164.                                         new Vector3(cellWidth * x, -cellHeight * y, depth) :
  165.                                         new Vector3(cellWidth * y, -cellHeight * x, depth);
  166.  
  167.                                 if (animateSmoothly && Application.isPlaying)
  168.                                 {
  169.                                         SpringPosition.Begin(t.gameObject, pos, 15f);
  170.                                 }
  171.                                 else t.localPosition = pos;
  172.  
  173.                                 if (++x >= maxPerLine && maxPerLine > 0)
  174.                                 {
  175.                                         x = 0;
  176.                                         ++y;
  177.                                 }
  178.                         }
  179.                 }
  180.                 else
  181.                 {
  182.                         for (int i = 0; i < myTrans.childCount; ++i)
  183.                         {
  184.                                 Transform t = myTrans.GetChild(i);
  185.  
  186.                                 if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;
  187.  
  188.                                 float depth = t.localPosition.z;
  189.                                 Vector3 pos = (arrangement == Arrangement.Horizontal) ?
  190.                                         new Vector3(cellWidth * x, -cellHeight * y, depth) :
  191.                                         new Vector3(cellWidth * y, -cellHeight * x, depth);
  192.  
  193.                                 if (animateSmoothly && Application.isPlaying)
  194.                                 {
  195.                                         SpringPosition.Begin(t.gameObject, pos, 15f);
  196.                                 }
  197.                                 else t.localPosition = pos;
  198.  
  199.                                 if (++x >= maxPerLine && maxPerLine > 0)
  200.                                 {
  201.                                         x = 0;
  202.                                         ++y;
  203.                                 }
  204.                         }
  205.                 }
  206.  
  207.                 switch (alignment)
  208.                 {
  209.                         case Alignment.Left :
  210.                                 myTrans.localPosition = new Vector3 (0f, myTrans.localPosition.y, myTrans.localPosition.z);
  211.                                 break;
  212.  
  213.                         case Alignment.Centered :
  214.                                 float centerOffset = myTrans.childCount * cellWidth * 0.5f;
  215.                                 centerOffset = -centerOffset + (centerOffset * 1/myTrans.childCount);
  216.                                
  217.                                 myTrans.localPosition = new Vector3 (centerOffset, myTrans.localPosition.y, myTrans.localPosition.z);
  218.                                 break;
  219.  
  220.                         case Alignment.Right :
  221.                                 myTrans.localPosition = new Vector3 (-(myTrans.childCount - 1) * cellWidth, myTrans.localPosition.y, myTrans.localPosition.z);
  222.                                 break;
  223.                 }
  224.  
  225.                 if (keepWithinPanel && mPanel != null)
  226.                         mPanel.ConstrainTargetToBounds(myTrans, true);
  227.  
  228.                 if (onReposition != null)
  229.                         onReposition();
  230.         }
  231. }
  232.  
  233.  
I'm very new to programming so I apologize if I made a mistake or my additions are hard to read.

18
NGUI 3 Documentation / Re: UISprite
« on: January 11, 2014, 05:25:39 AM »
Is there a way to adjust the dimension sizes of sprites uniformly in the scene view? Before sprites had dimensions, I would just use the unity scale tool, but now this feels like the wrong thing to do (is it?).

I intuitively expected this to happen when holding SHIFT while adjusting size with the handlers, just like in photoshop (like with the free transform tool).

Does this feature exist in the current version of nGUI?

Also, is there a way to enable snapping while adjusting widget sizes? Having this feature currently when moving sprites is fantastic, but while arranging ui layouts I found myself constantly adjusting dimensions, moving to snap to center, adjusting dimensions again just to try and get the alignment to look good. Obviously the widget  unified alignment options in the inspector fix this problem but I usually don't set those up until after I have the widget arranged the way I want. Edit: I now better understand the widget anchoring options (I was using them wrong, lol). I think I understand Anchoring is a separate feature than snapping while scaling (though some use cases may overlap).

Sorry if part of this belongs in the feedback section.

Pages: 1 [2]