我有一个 UIScroll Bar 和一个包含 UIGrid 的 UIScroll View ,如下所示:
- 滚动窗口
- 滚动条(UIS滚动条)
- ScrollView (UIScroll View )
- 网格(UIGrid)
我用它们来显示项目列表并上下滚动以查看所有项目, 在开始时添加一些项目时它会起作用,但我有一个小问题 当我在最后一个地方实时添加一个新项目时。
当我开始场景时,我将一些项目放在网格中,我有这样的东西 在层次结构 View 中:
- 滚动窗口
- 滚动条(UIS滚动条)
- ScrollView (UIScroll View )
- 网格 (UIGrid)
- item01
- item02
- item03
所有三个项目都显示没有任何问题,并按字母顺序排序。 我还有一个功能,当我按下某个按钮时,它会在网格中添加一个名为 item04 的新功能:
public void addItem(){
// create a new instance of a prefab
GameObject prefab = Instantiate(Resources.Load("MyItems/TestItem")) as GameObject;
// get the grid
GameObject itemsGrid = GameObject.Find("scrollWindow/scrollView/Grid");
// add the new item in the grid
itemsGrid .GetComponent<UIGrid>().AddChild(prefab.transform);
// set the item name
prefab.name = "item04";
// set the item position
prefab.transform.localScale = new Vector3(1f, 1f, 1f);
prefab.transform.localPosition = new Vector3(0f, 0f, 0f);
itemsGrid .GetComponent<UIGrid>().repositionNow = true;
// From here I try to update the grid and the scroll bar in order to make the
grid to show the last item that is below all of them
GameObject.Find("scrollWindow/scrollBar").GetComponent<UIScrollBar>().value = 1;
GameObject.Find("scrollWindow/scrollView").GetComponent<UIScrollView>().ResetPosition();
}
问题是虽然添加了item04,但是网格和滚动条并没有移动,直到用户上下移动滚动条,item04才出现。
有什么办法可以让网格或者滚动条在没有用户交互的情况下实时向下显示最后添加的项目??
谢谢!
最佳答案
我做到了!
itemsGrid .GetComponent().Reposition(); -> 它使网格显示所有项目并更新滚动条。
希望它对其他人有用:)
关于android - NGUI:如何在实时添加新项目时重置/更新 UIgrid 和 UIScrollBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25866364/