I have an UIScroll Bar and a UIScroll View that contains a UIGrid, like this:
- scrollWindow
- scrollBar (UIScroll Bar)
- scrollView (UIScroll View)
- Grid (UIGrid)
I use them to display a list of items and scroll up and down to see all of them, and it works when some items are added at the beginning, but i have a little problem when I add a new item in the last place in real time.
When i start the scene, i put some items in the Grid and I have something like this in the Hierarchy View:
- scrollWindow
- scrollBar (UIScroll Bar)
- scrollView (UIScroll View)
- Grid (UIGrid)
- item01
- item02
- item03
All three items are displayed without any problem and sorted alphabetically. I also have a function that adds a new one called item04 in the grid when I press some button:
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();
}
The problem is that although the item04 is added, the grid and the scroll bar do not move and the item04 does not appear till the user moves the scroll bar up and down.
Is there any way to make the grid or the scroll bar to go down and display the last item added in real time without the user interaction??
Thank you!