【问题标题】:NGUI: How to reset / update a UIgrid & UIScrollBar when a new item is added in real timeNGUI:实时添加新项目时如何重置/更新 UIgrid 和 UIScrollBar
【发布时间】:2014-09-16 10:27:25
【问题描述】:

我有一个 UIScroll Bar 和一个包含 UIGrid 的 UIScroll View,如下所示:

  • 滚动窗口
    • 滚动条(UI滚动条)
    • scrollView(UIScroll 视图)
    • 网格 (UIGrid)

我使用它们来显示项目列表并上下滚动以查看所有项目, 当开始添加一些项目时它可以工作,但我有一个小问题 当我在最后一个地方实时添加新项目时。

当我开始场景时,我将一些项目放在网格中,我有这样的东西 在层次结构视图中:

  • 滚动窗口
    • 滚动条(UI滚动条)
    • scrollView(UIScroll 视图)
    • 网格 (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。

有没有什么办法可以让网格或者滚动条在没有用户交互的情况下实时向下显示最后添加的项目??

谢谢!

【问题讨论】:

    标签: android ios unity3d ngui


    【解决方案1】:

    我做到了!

    itemsGrid .GetComponent().Reposition(); -> 它使网格显示所有项目并更新滚动条。

    希望它对其他人有用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 2018-02-20
      相关资源
      最近更新 更多