工作的时候,需要做一个让子物体随机排列的功能,因为父物体有HorizontalGrid,自动对齐功能。

让0~9位置随机

随机效果
代码块
//进行子物体随机
public void RamdomChild()
{
//随机数列表
List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> randomList = new List<int>();
while(list.Count>0)//获取一个随机数在9以内的列表
{
int randomIndex = Random.Range(0, list.Count);
randomList.Add(list[randomIndex]);
list.RemoveAt(randomIndex);
}
//开始排序
for(int i=0;i<content.childCount;i++)
{
content.GetChild(i).transform.SetSiblingIndex(randomList[i]);
}
}
使用transform里面的SetSiblingIndex即可排序,cocos里面也是这个API。
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删