NGUI所见即所得之UIGrid & UITable

尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com

       你是不是对 UIGrid 和 UITable 定位计算方法还模糊不清,那么这篇文章就是你需要的。

       NGUI 提供了 Grid 和 Table 组件,支持的参数很少,功能也很鸡肋,完全不能像 CSS 的 Box 模型那样随心所欲的布局,而且使用 UIGrid 和 UITable 的时候经常会相对 UIGrid 和 UITable 挂载的 Transform 出现偏移,之前使用的时候,都是根据经验来规避的。今天打算把UIGrid 和 UITable 的排列规则看下, 才形成此文。

        UIGrid 和 UITable 的原理很简单,对子 Transform 的 List 进行排序,然后更加不同的规则进行定位排列(UIGrid 和 UITable 还是有很大不同的)。

排序(Sort)

        UIGrid 和 UITable 定义了5种排列方式(其实是3种,None默认不排序即Transform的默认排序,Custom虽然提供virtual 可以重载):

C#代码 复制代码 收藏代码
  1. public enum Sorting 
  2.     None, 
  3.     Alphabetic, 
  4.     Horizontal, 
  5.     Vertical, 
  6.     Custom, 

         对应的三种排序方法:集 Alphabetic 按照名字字符串排序,Horizontal 和 Vertical 按照localPosition 进行的排序

C#代码 复制代码 收藏代码
  1. static public int SortByName (Transform a, Transform b) { return string.Compare(a.name, b.name); } 
  2. static public int SortHorizontal (Transform a, Transform b) { return a.localPosition.x.CompareTo(b.localPosition.x); } 
  3. static public int SortVertical (Transform a, Transform b) { return b.localPosition.y.CompareTo(a.localPosition.y); } 

         这里说下,虽然提供了Custom方式,第一感觉NGUI的developer考虑很周到,但是提供的确实重载 virtual 函数的方式,D.S.Qiu 觉得这种方式太不好了,为了一个方法就要写一个 子类去重载,个人觉得指定一个委托,扩展起来会更直观,但这一要求开发者一开始就得指定这个 Custom Sort Delegate。

UIGrid定位原理

        下面这段代码是 Reposition() 的一部分,原理很简单:根据定义的cellHeight 和cellWidth 来调整子 Transform 的 localPosition。这里还是吐槽下:Reposition() 的代码太多容易了,至少我看到了这段代码在 Reposition 中出现了两次,完全多余,其实就是处理流程就应该是: 先获取所有子 Transform List ,然后对List 排序,最后就是下面这段定位代码了。

C#代码 复制代码 收藏代码
  1. for (int i = 0, imax = list.Count; i < imax; ++i) 
  2.     Transform t = list[i]; 
  3.     if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue
  4.     float depth = t.localPosition.z; 
  5.     Vector3 pos = (arrangement == Arrangement.Horizontal) ? 
  6.         new Vector3(cellWidth * x, -cellHeight * y, depth) : 
  7.         new Vector3(cellWidth * y, -cellHeight * x, depth); 
  8.  
  9.     if (animateSmoothly && Application.isPlaying) 
  10.     { 
  11.         SpringPosition.Begin(t.gameObject, pos, 15f).updateScrollView = true
  12.     } 
  13.     else t.localPosition = pos; 
  14.  
  15.     if (++x >= maxPerLine && maxPerLine > 0) 
  16.     { 
  17.         x = 0; 
  18.         ++y; 
  19.     } 

UITable 定位原理

       UITable 的定位方法在 ReositionVaribleSize 中,跟UIGrid 最大不同点是:UIGrid 只根据定义的cellHeight 和 cellWidth 来计算位置,UITable 根据“内容“(UIWidget)来计算位置的。

C#代码 复制代码 收藏代码
  1. protected void RepositionVariableSize (List<Transform> children) 
  2.     float xOffset = 0; 
  3.     float yOffset = 0; 
  4.  
  5.     int cols = columns > 0 ? children.Count / columns + 1 : 1; 
  6.     int rows = columns > 0 ? columns : children.Count; 
  7.  
  8.     Bounds[,] bounds = new Bounds[cols, rows]; 
  9.     Bounds[] boundsRows = new Bounds[rows]; 
  10.     Bounds[] boundsCols = new Bounds[cols]; 
  11.  
  12.     int x = 0; 
  13.     int y = 0; 
  14.         //这个循环计算每行,每列,每个cell 的内容的编辑 Bounds 
  15.     for (int i = 0, imax = children.Count; i < imax; ++i) 
  16.     { 
  17.         Transform t = children[i]; 
  18.         Bounds b = NGUIMath.CalculateRelativeWidgetBounds(t, !hideInactive); 
  19.  
  20.         Vector3 scale = t.localScale; 
  21.         b.min = Vector3.Scale(b.min, scale); 
  22.         b.max = Vector3.Scale(b.max, scale); 
  23.         bounds[y, x] = b; 
  24.  
  25.         boundsRows[x].Encapsulate(b); 
  26.         boundsCols[y].Encapsulate(b); 
  27.  
  28.         if (++x >= columns && columns > 0) 
  29.         { 
  30.             x = 0; 
  31.             ++y; 
  32.         } 
  33.     } 
  34.  
  35.     x = 0; 
  36.     y = 0; 
  37.         //计算位置 
  38.     for (int i = 0, imax = children.Count; i < imax; ++i) 
  39.     { 
  40.         Transform t = children[i]; 
  41.         Bounds b = bounds[y, x]; 
  42.         Bounds br = boundsRows[x]; 
  43.         Bounds bc = boundsCols[y]; 
  44.  
  45.         Vector3 pos = t.localPosition; 
  46.         pos.x = xOffset + b.extents.x - b.center.x; 
  47.         pos.x += b.min.x - br.min.x + padding.x;   //以每列最右边的x值为标准,即保证每列最左边的基准点 
  48.  
  49.         if (direction == Direction.Down) 
  50.         { 
  51.             pos.y = -yOffset - b.extents.y - b.center.y; 
  52.             pos.y += (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y; 
  53.         } 
  54.         else 
  55.         { 
  56.             pos.y = yOffset + (b.extents.y - b.center.y); 
  57.             pos.y -= (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y; 
  58.         } 
  59.  
  60.         xOffset += br.max.x - br.min.x + padding.x * 2f; 
  61.  
  62.         t.localPosition = pos; 
  63.  
  64.         if (++x >= columns && columns > 0) 
  65.         { 
  66.             x = 0; 
  67.             ++y; 
  68.  
  69.             xOffset = 0f; 
  70.             yOffset += bc.size.y + padding.y * 2f; 
  71.         } 
  72.     } 

          这里还是有吐槽:根据 Bounds 的定义 b.extens.x - b.center.x + b.min.x == 0 ,也就是这部完全是没有必要的,看来UIGrid 的 developer,不光代码逻辑不清晰,难道连大脑都是豆腐花做的么。

C#代码 复制代码 收藏代码
  1. pos.x = xOffset + b.extents.x - b.center.x; 
  2. pos.x += b.min.x - br.min.x + padding.x; 

           类似计算 y 的值也其他更直接方法:

C#代码 复制代码 收藏代码
  1. (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f 

使用经验

      1.UIGrid 没有考虑Bounds ,根据UIGrid 的计算公式可以知道:UIGrid 的第一个元素的 localPosition 的 x 和 y 一定都是 0 ,所以要位置,必须调整UIGrid 的localPosition ,但是实际在有可能调整的是 子对象,然后再 Scene 窗口看是没问题的(注意此时还没有重排),一运行就会出现位置的偏移。

      2.UITable 的子组件的 x 总是以 每列最左为 起始基准点的, y 则是每行居中对齐 :(b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f 这行代码起始就是计算当前组件和所在行中心点的偏移。

   下图是将NGUI 其中一个组件的 sprite 左移了,就出现下面的排列:


       虽然 NGUI 提供了 UIGrid 和 UITable ,起始是非常之不完善,几乎做不了什么功能。这里分享两条经验:

               1.使用UIGrid时,调整界面的时候让 UIGrid 的 transform 和 其第一个子组件的 transform 相同,这样经过计算之后,位置就是之前调整想要的。或者将 第一个子组件 transform 重置,这样调整UIGrid 的              transform 位置看到的效果就是真实的。

               2.使用UITable 让每列元素的左边界都相同,即左对齐。可以看到 NGUI Example 的 Question Log 的 Table 的所有组件(UILable UISprite)的 pivot 都调整为 left 。

        总之,就是根据UIGrid 和UITable 的排列原理做相应的调整。

小结:

       这篇文章相对于NGUI所见即所得系列其他文章来说,简单很多。最近要做一个界面根据内容自适应,挺复杂的,一堆莫名其妙的问题。之前一直觉得 Unity 的 UI 没有Window MFC 等开发直接拖拽方式那么直观。NGUI 虽然很庞大,但NGUI越来越容易让我吐槽了,可能是对NGUI的家底多少掌握的缘故吧。

        很久就听说Unity要出自己的 UI 了,其实D.S.Qiu 也一直有想尝试自己写一点UI的可视化编辑工具(Visual Editor)。昨天不经意看到 Cocos2D 的 UI 编辑器 CocoStudio 感觉很强大,然后顺手google Unity有没有这方面的工具,果然还是发现了 UIToolkit , bitverse RagePixel 和 EWS editor ,也就说 Unity其实也有些 UI 可视化工具的。尤其 bitverse 支持的组件特别丰富,很强大,只可惜没有集成 Batch  DrawCall 的功能。

       如果您对D.S.Qiu有任何建议或意见可以在文章后面评论,或者发邮件(gd.s.qiu@gmail.com)交流,您的鼓励和支持是我前进的动力,希望能有更多更好的分享。

       转载请在文首注明出处:http://dsqiu.iteye.com/blog/2034883

更多精彩请关注D.S.Qiu的博客和微博(ID:静水逐风)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值