有关ngui grid中去除一项后的排序问题

遇到这个问题,是在实现一个公告栏界面的时候,公告栏可以新增一条公告,也可以删除一条公告。

新增很简单,这里不做多的介绍;

关于删除,之前的代码是:

GameObject go = is_parent.transform.FindChild(name).gameObject;
UIGrid grid = is_parent.GetComponent<UIGrid>();
Destroy(go);
grid.Reposition();

但是没有效果,选择任何排序模式都没效果。一直都是在grid中有一个  单元元素大小的 空缺位置。

随后百度n次之后,有人说使用 grid.repositionNow = true;

随后代码为:

GameObject go = is_parent.transform.FindChild(name).gameObject;
UIGrid grid = is_parent.GetComponent<UIGrid>();
Destroy(go);

grid.repositionNow = true;

依然无效,很是纠结。

最终一想,这里的销毁只是销毁当前 单元元素对象,并没有在grid中做处理,换而言之,grid中还是把这个删除的元素当做正常存在的元素在处理,有了这个想法之后,代码就成了现在这个样子:

GameObject go = is_parent.transform.FindChild(name).gameObject;
UIGrid grid = is_parent.GetComponent<UIGrid>();
grid.RemoveChild(go.transform);
Destroy(go);
grid.sorting = UIGrid.Sorting.Vertical;
grid.repositionNow = true;

先在grid中移除当前元素

再销毁这个元素

最后设置排序(在前面的几种方案中,设置排序是在面板上做的。)

最后刷新排序

OK!

到这里算是完整结束了,希望能帮助到遇到这个问题正在找方法的人。

原文地址:https://www.cnblogs.com/dinggf/p/4778510.html