Author Topic: Do not understand Scrollview  (Read 5562 times)

Shemamforash

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Do not understand Scrollview
« on: February 04, 2014, 01:41:39 PM »
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

  1. for(int i = 0; i < techTreeScript.listOfImprovements.Count; ++i)
  2.                 {                      
  3.                         GameObject improvement = NGUITools.AddChild(scrollviewWindow, scrollviewButton); //Scrollviewwindow is gameobject containing scrollview, scrollviewbutton is the button prefab
  4.  
  5.                         improvement.GetComponent<UISprite>().depth = 20; //Depth set to 20 to ensure I can see it, will be changed when scrollview actually works
  6.  
  7.                         improvement.GetComponent<UILabel>().depth = 20;
  8.  
  9.                         improvement.GetComponent<UIDragScrollView>().scrollView = scrollviewWindow.GetComponent<UIScrollView>(); //Assigning scrollview variable of prefab
  10.  
  11.                         improvement.name = techTreeScript.listOfImprovements[i].improvementName; //Just naming the object in the hierarchy
  12.  
  13.                         improvement.GetComponent<UILabel>().text = techTreeScript.listOfImprovements[i].improvementName + "\n" + techTreeScript.listOfImprovements[i].improvementCost; //Add label text
  14.  
  15.                         improvementsList.Add (improvement); //Add improvement into a list so I can enable/disable improvements as needed
  16.  
  17.                         NGUITools.SetActive(improvement, false); //Default set improvement to false so it won't be shown in scrollview unless needed
  18.  
  19.                         scrollviewWindow.GetComponent<UIGrid> ().Reposition (); //Reposition in grid
  20.                         scrollviewWindow.GetComponent<UIScrollView>().ResetPosition(); //Reset scrollview
  21.                 }

And here is the code when I set the scrollview object to active:

  1. if(systemListConstructor.systemList[selectedSystem].planetsInSystem[selectedPlanet].planetColonised == true)
  2.                 {
  3.                         NGUITools.SetActive(scrollviewWindow, true); //Display the scrollview
  4.  
  5.                         Vector3 position = new Vector3(planetElementList[selectedPlanet].spriteObject.transform.position.x - 0.5f, //Move it to an easy to see position
  6.                                                        planetElementList[selectedPlanet].spriteObject.transform.position.y,
  7.                                                        planetElementList[selectedPlanet].spriteObject.transform.position.z);
  8.  
  9.                         scrollviewWindow.transform.position = position;
  10.  
  11.                         scrollviewWindow.GetComponent<UIScrollView> ().Scroll (Time.deltaTime); //How does this even work?
  12.  
  13.                         for(int i = 0; i < techTreeScript.listOfImprovements.Count; ++i)
  14.                         {
  15.                                 if(techTreeScript.listOfImprovements[i].hasBeenBuilt == true || techTreeScript.listOfImprovements[i].improvementLevel > techTreeScript.techTier
  16.                                    || techTreeScript.listOfImprovements[i].improvementCategory == enemyOneTurnScript.playerRace
  17.                                    || techTreeScript.listOfImprovements[i].improvementCategory == enemyTwoTurnScript.playerRace)
  18.                                 {
  19.                                         if(improvementsList[i].activeInHierarchy == true)
  20.                                         {
  21.                                                 NGUITools.SetActive(improvementsList[i], false); //If tech has been built it doesnt need to be shown in the scrollview
  22.                                         }
  23.                                         continue;
  24.                                 }
  25.  
  26.                                 NGUITools.SetActive(improvementsList[i], true); //If tech has not been built, set it to active so it can be shown in the scrollview
  27.                                 scrollviewWindow.GetComponent<UIGrid> ().Reposition (); //Reposition grid contents
  28.                                 scrollviewWindow.GetComponent<UIScrollView>().ResetPosition(); //Reset scrollview position
  29.                         }
  30.                 }

Finally I have included some images to show how my scrollview and stuff is set up.
Thanks v much :)
« Last Edit: February 04, 2014, 02:04:24 PM by Shemamforash »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Do not understand Scrollview
« Reply #1 on: February 04, 2014, 02:11:31 PM »
I would split the prefabs up in a hierarchy, with

*GameObject
**Sprite
**Label

And keep your logic and collider on the GameObject with serializedfields to the sprite and label for any changes you need to do .

NGUI gets a little dodgy when you put both labels and sprites on the same game object.

Shemamforash

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Do not understand Scrollview
« Reply #2 on: February 04, 2014, 02:31:26 PM »
Ah okay cool, I'll see how that turns out :)

kitgui

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Do not understand Scrollview
« Reply #3 on: February 04, 2014, 03:40:06 PM »
NGUITools.AddChild is recommended instead of gameObject.AddChild, as it makes sure your hierarchy still works for NGUI

Shemamforash

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Do not understand Scrollview
« Reply #4 on: February 05, 2014, 04:02:53 AM »
Yep that worked! Thanks