NGUI UIGrid 动态刷新布局 && BUG FIX

 1     /// <summary>
 2     /// "1" => 对应的一个UISpirte,"1234" => 对应四个预设
 3     /// </summary>
 4     /// <param name="scoreGrid"> 设置中间为锚点 </param>
 5     /// <param name="curScore">like "1234"</param>
 6     /// <param name="numberPrefab"> 对应 的 预设</param>
 7     /// <returns></returns>
 8     IEnumerator ShowScore(UIGrid scoreGrid, string curScore, GameObject numberPrefab)
 9     {
10         foreach (Transform item in scoreGrid.transform)
11         {
12             Destroy(item.gameObject);
13         }
14 
15         //一定要 加上 这一局  不然 设置 grid 中间为锚点 Reposition() 不是预期的那样
16         yield return new WaitForEndOfFrame();
17 
18 
19         char[] number = curScore.ToCharArray();
20         for (int i = 0; number != null && i < number.Length; i++)
21         {
22             GameObject child = NGUITools.AddChild(scoreGrid.gameObject, numberPrefab);
23             UISprite showNum = child.GetComponent<UISprite>();
24             showNum.spriteName = number[i].ToString();
25         }
26 
27         scoreGrid.Reposition();
28         scoreGrid.repositionNow = true;
29     }

 

2015-4-27 

用上面的方法删除所有子物体 Edit模式 不行,试试用下面的方法,可能是 Edit模式 下删除子物体 的时候引发了重新内部元素排序。暂时还没有测试 Run 模式

1 List<Transform> list = new List<Transform>();
2 foreach (Transform item in transform)
3 {
4     list.Add(item);
5 }
6 foreach (var item in list)
7 {
8     NGUITools.Destroy(item.gameObject);
9 }

 

posted @ 2015-01-27 15:52  灵魂重新  阅读(3068)  评论(0编辑  收藏  举报