- Home /
Dynamic Scrollable List with NGUI
I am trying to make a GUI element which contains a list of inventory items, it is basically just like a big table. Now looking at the NGUI scrollbar tutorial they use a clipping area and have another panel with a scrollbar, but I cannot see how they link the scrollbar from one panel to the other panel? Is this some magic step which isn't documented too well or do you have to manually just adjust the scrollbar values based on the amount of elements within the list yourself?
Forgetting the scrollbar problem for a moment I am also a bit unsure as to how I should add each row in the list. I made a prefab which contains a dummy GameObject which then contains 4 labels which are displayed in-line. Now assuming the list container element is a panel with the UIGrid
script applied (the tutorial said to include this), then I have a c# script applied which takes the list of items and tries to instantiate the (lets call it) ItemRowPrefab
for each item in the list. So 10 items in the inventory should create 10 ItemRowPrefabs
and then add them to the panel acting as the list container. The problem here is that to call the instantiate method for ItemRowPrefab
it requires me to have an existing object to copy from, so should this just be a call to var dummyItemRowPrefab = AddComponent
then pass that in as the instantiate argument for existing object? If that is the case, how do I then add the instantiated object to the panel? I can get hold of the panel but there doesn't seem to be any simple methods to AddObject(object)
.
Sorry for all the waffle but wanted to give as much context as possible as to what the problems are that I am facing.
I'm having the same issue. No idea why this isn't a simple part of the GUI, but making a simple list is tedious. Did you ever figure out a good way to do it?
Answer by harko12 · Jun 10, 2013 at 04:06 AM
I'm trying to make sense of this one myself. I think the magic step you're missing is that the UIPanel(clipped view) that is the panel with the grid attached has a script on it called UIDraggablePanel. In that panel there is a link to the horizontal scrollbar object. I think that script handles the updating if the scrollbar position.
As for your items, I think you're on the right track. You should make a prefab, and put all the scripts on it that it needs, then iterate through your list and create instances of it for each item that you have. You can set the parent of them as you go, I think.
I know that may not be super helpful, I'm still trying to put it all together too. There are good forums on the NGUI site (tasharen.net ?) You may want to try there.
Answer by SteveKouts · Feb 20, 2014 at 03:02 AM
This guy shows you how to do it even though its in Russian. Its about halfway through the video where he shows you how. https://www.youtube.com/watch?v=AjzJrbiTJgE