- 第一种思路:在实例化生成预制体的时候,把所有物体全部禁用,并加入集合当中,然后设置currentIndex,即当前要显示的物体编号,先把上一个物体禁用,然后把下一个物体启用,并把编号赋给currentIndex
private List<GameObject> list;
private int currentIndex = -1 //初始化0显示
Start()
{
ChangeItem(0);
}
private void CreateAllItems()
{
//实例化物品
item.SetActive(false);
list.Add(item);
}
private void ChangeItem(int index )
{
if( currentIndex != -1){ list[currentIndex ].SetActive(false); }
currentIndex = index;
list[currentIndex].SetActive(true);
}
- 第二种思路,当对象被点击的时候,每次传给函数一个index,然后把该index的物体设置为true , 其余全部设置为false
private List<GameObject> list;
Start()
{
ChangeItem(0);
}
private void CreateAllItems()
{
//实例化物品
list.Add(item);
}
private void ChangeItem(int index )
{
for(int i = 0; i< list.Count; i++)
{
list[i].SetActive(false); //把所有物品禁用
}
list[index].SetActive(true); //把当前物体启用即可
}