Author Topic: How to add item to the scroll list by code?  (Read 6660 times)

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
How to add item to the scroll list by code?
« on: April 13, 2012, 07:51:44 AM »
normally data are from other source or service and have to dynamic add to the scroll list item....May I know how to do? Thx a lot

KeithT

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 55
    • View Profile
Re: How to add item to the scroll list by code?
« Reply #1 on: April 13, 2012, 08:58:19 AM »
      GameObject theDropDownListGO  = GameObject.Find("theListGameObject");
      UIPopupList theDropDownListScript  = theDropDownListGO  .GetComponent<UIPopupList>();
      theDropDownListScript  .items.Clear();

      foreach (item iteem in you List)
      {
         theDropDownListScript  .items.Add(item );
      }

Something like that should work

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: How to add item to the scroll list by code?
« Reply #2 on: April 13, 2012, 10:32:58 AM »
Thx, but what I mean is how can I add child item in UIGrid...?? The item is a game object prefab...which is a group of game object containing s a few UILabels, UISplite, etc)

Post edit***********************************
I guess I just found the answer..not tried yet: just set the parent property should be fine. ;)

GameObject GridItem = Instantiate(GameObject.FindGameObjectWithTag("listitem")) as GameObject;       
GameObject Grid = GameObject.FindGameObjectWithTag("list");
ListItem.transform.parent = List.transform;

Post Post edit***********************************
The code above nearly work...but two thinks to remind...
1. you will not see the sprite sometimes, you have to set the prefab depth of some item higher...donno why, the original item in the list is 3 and I can see, while on the fly new item have to be 4 or 5 in order to be seen. luckily you can easily preset it on the prefab.
2. Have to run UIGridInstance.Reposition(); one times to update the graph but after update,
3. The scale of the newly added listitem will changed weirdly after ....have to set back to 1 after reposition.

GameObject GridItem = Instantiate(GameObject.FindGameObjectWithTag("listitem")) as GameObject;       
GameObject Grid = GameObject.FindGameObjectWithTag("list");
ListItem.transform.parent = List.transform;
thegrid.Reposition();
ListItem.transform.localScale = new Vector3(1, 1, 1);
« Last Edit: April 13, 2012, 11:15:08 AM by cheuksh »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to add item to the scroll list by code?
« Reply #3 on: April 13, 2012, 03:18:55 PM »
Assuming you have a script attached to the same object as UIGrid, just use NGUITools.AddChild(parent, prefab) to populate it with children (modifying their data as you need it), then call the UIGrid's Reposition() function.