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 }