android - NGUI:如何在实时添加新项目时重置/更新 UIgrid 和 UIScrollBar

标签 android ios unity-game-engine ngui

我有一个 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/

相关文章:

android - Android 中用于 RTL 的 AutoCompleteTextView

android - 在 4.3 android 版本的 uiautomator 中使用仪器类长按问题

java - 当我尝试从 SQL 数据库检索日期和时间时,时间段丢失

javascript - WKWebView 在加载任何 URL 时阻止某些脚本(阻止任何网站中的跟踪或广告)

unity-game-engine - 脚本在 Unity3d 中不起作用

java - Android Studio 的 Unity 插件。

android - 指针事件 : "auto" in child element not reversing parents "none" on mobile

iphone - 如何从 iPhone 上的录音中获取线性 PCM?

iPhone:如何知道 UISlider 是否在滑动?

javascript - EntryPointNotFoundException 外部 JS 库错误