mHeroInfoUI.Herorder.Clear();
List<HeroInfo> heroList = new List<HeroInfo>(); //heroList = HeroDataManager.Instance.GetHeroListBySort(selectHero, classify, toggle); heroList = HeroDataManager.Instance.GetNewHeroSort(classify, toggle);
if (itemPool.Count != 0) for (int i = 0; i < itemPool.Count; i++) itemPool[i].SetActive(false); if ( heroList == null || heroList.Count <= 0) //meiyou { if(itemPool.Count!=0) for(int i=0;i<itemPool.Count;i++) itemPool[i].SetActive(false); return; } int r = heroList.Count/ 5; //行 int h = heroList.Count % 5;//餘數 if (h != 0 && r != 0) r = r + 1; if(r==0) { r=1; } for (int i = 0; i < r; i++) { if (i > itemPool.Count-1) { itemPool.Add(GameObject.Instantiate(prefab) as GameObject); } Transform row = itemPool[i].transform; row.parent = heroItemParent.transform; row.localPosition = new Vector3(0, 220-i * (192), 0); row.localScale = Vector3.one; row.localEulerAngles = Vector3.zero; row.gameObject.SetActive(true);//prefab池 的一個元素激活 for(int j=0;j<5;j++) { if (h != 0&&i==r-1) //最後一行 { if (j > h - 1) { row.GetChild(j).gameObject.SetActive(false); //一個行prefab 裏面5個元素有多餘的 隱藏 continue; } } // heroList[i * 5 + j] 是第幾個heroinfo InitAHeroItems(row.GetChild(j).gameObject, heroList[i * 5 + j], i * 5 + j); mHeroInfoUI.Herorder.Add(i * 5 + j, heroList[i * 5 + j].heroID); } } if (r < itemPool.Count - 1) //初始化完成後 隱藏池中多餘的 { for (int b = r + 1; b < itemPool.Count; b++) { itemPool[b].SetActive(false); } }
精簡後的spa
mHeroInfoUI.Herorder.Clear(); List<HeroInfo> heroList = new List<HeroInfo>(); //heroList = HeroDataManager.Instance.GetHeroListBySort(selectHero, classify, toggle); heroList = HeroDataManager.Instance.GetNewHeroSort(classify, toggle); #region newLogic CreateGridTools<HeroInfo> createGridTools = new CreateGridTools<HeroInfo>(); createGridTools.InitData(heroItemParent, Item, heroList, InitAHeroItems);