So I've read as much documentation/forum posts & class info as I can find, but I simply cannot get my scrollview to work. A summary of how it is supposed to work:
- I have a list that has a variable size depending on the race of the player.
- I want to be able to display each of these items in a scrollview, so I figured I would get the length of the list and then do gameobject.AddChild(myprefab) to add as many labels as I needed into the scrollview. That works fine, I can see in the inspector that it does indeed add the correct number of object at start up.
- The prefab itself has a sprite for a background, a label to display text, a UIButton & Box Collider (since it needs to be clickable), and finally a UIDrag Scroll View where the scroll view variable is set to the parent (which contains the scrollview) at runtime.
For some reason when I hit play, the scrollview will be populated by the labels, but all the sprites will merge onto eachother and overlap, I am also unable to scroll through the list with the scroll wheel, and the button functionality does not exist. I am slightly at a loss as to how all this is supposed to work, as I have managed to get scrollviews to work before, and I've probably watched that walkthrough video about 5 or 6 times now.
Here is my Start() code
for(int i = 0; i < techTreeScript.listOfImprovements.Count; ++i)
{
GameObject improvement = NGUITools.AddChild(scrollviewWindow, scrollviewButton); //Scrollviewwindow is gameobject containing scrollview, scrollviewbutton is the button prefab
improvement.GetComponent<UISprite>().depth = 20; //Depth set to 20 to ensure I can see it, will be changed when scrollview actually works
improvement.GetComponent<UILabel>().depth = 20;
improvement.GetComponent<UIDragScrollView>().scrollView = scrollviewWindow.GetComponent<UIScrollView>(); //Assigning scrollview variable of prefab
improvement.name = techTreeScript.listOfImprovements[i].improvementName; //Just naming the object in the hierarchy
improvement.GetComponent<UILabel>().text = techTreeScript.listOfImprovements[i].improvementName + "\n" + techTreeScript.listOfImprovements[i].improvementCost; //Add label text
improvementsList.Add (improvement); //Add improvement into a list so I can enable/disable improvements as needed
NGUITools.SetActive(improvement, false); //Default set improvement to false so it won't be shown in scrollview unless needed
scrollviewWindow.GetComponent<UIGrid> ().Reposition (); //Reposition in grid
scrollviewWindow.GetComponent<UIScrollView>().ResetPosition(); //Reset scrollview
}
And here is the code when I set the scrollview object to active:
if(systemListConstructor.systemList[selectedSystem].planetsInSystem[selectedPlanet].planetColonised == true)
{
NGUITools.SetActive(scrollviewWindow, true); //Display the scrollview
Vector3 position
= new Vector3
(planetElementList
[selectedPlanet
].spriteObject.transform.position.x - 0
.5f,
//Move it to an easy to see position planetElementList[selectedPlanet].spriteObject.transform.position.y,
planetElementList[selectedPlanet].spriteObject.transform.position.z);
scrollviewWindow.transform.position = position;
scrollviewWindow.GetComponent<UIScrollView> ().Scroll (Time.deltaTime); //How does this even work?
for(int i = 0; i < techTreeScript.listOfImprovements.Count; ++i)
{
if(techTreeScript.listOfImprovements[i].hasBeenBuilt == true || techTreeScript.listOfImprovements[i].improvementLevel > techTreeScript.techTier
|| techTreeScript.listOfImprovements[i].improvementCategory == enemyOneTurnScript.playerRace
|| techTreeScript.listOfImprovements[i].improvementCategory == enemyTwoTurnScript.playerRace)
{
if(improvementsList[i].activeInHierarchy == true)
{
NGUITools.SetActive(improvementsList[i], false); //If tech has been built it doesnt need to be shown in the scrollview
}
continue;
}
NGUITools.SetActive(improvementsList[i], true); //If tech has not been built, set it to active so it can be shown in the scrollview
scrollviewWindow.GetComponent<UIGrid> ().Reposition (); //Reposition grid contents
scrollviewWindow.GetComponent<UIScrollView>().ResetPosition(); //Reset scrollview position
}
}
Finally I have included some images to show how my scrollview and stuff is set up.
Thanks v much
