Author Topic: adding list to UIGrid  (Read 5830 times)

Vid

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
adding list to UIGrid
« on: March 27, 2014, 09:57:01 AM »
Hello.
Have some problem with adding information to UIGrid.
In my class i create 3 variables UIInput type, getting from this inputs values and add this values to list.
So, i want to add this list of info to my UIGrid as line. But i have no idea how to. The easiest way to do this, using NGUITools.AddChild(Grid, prefab) - but in this case i don`t know how to create prefab by my list of received info.
Thanks.
p.s.:Sry for my english.
  1. public class Add_btn1 : MonoBehaviour
  2. {
  3.  
  4.     public GameObject prefab;
  5.  
  6.     public UIInput name;
  7.     public UIInput age;
  8.     public UIInput date;
  9.     public Transform checkBox;
  10.  
  11.     public class Person
  12.     {
  13.         public string name;
  14.         public int age;
  15.         public string date;
  16.         public string gender;
  17.         public DateTime dt;
  18.         public bool check = false;
  19.     }
  20.  
  21.     public static List<Person> list = new List<Person>();
  22.  
  23.     private string Male = "Male";
  24.     private string Female = "Female";
  25.     private string datePattern = @"^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$";
  26.  
  27.     public GameObject Grid;
  28.     public GameObject ListItem;
  29.  
  30.     private void OnClick()
  31.     {
  32.  
  33.         //Create variables and checking condtions
  34.         Person p = new Person ( );
  35.  
  36.         var checkGender = checkBox.GetComponent<UIToggle>().isChecked;
  37.         string gender = !checkGender ? Female : Male;
  38.         DateTime pastDateTime = DateTime.ParseExact(date.value, "dd/MM/yyyy", null);
  39.         var intAge = Int32.Parse(age.value);
  40.  
  41.             p.name = name.value;
  42.             p.age = Int32.Parse(age.value);
  43.             p.date = pastDateTime.ToShortDateString();
  44.             p.gender = gender;
  45.  
  46.             //Add information to list
  47.             list.Add(p);
  48.  
  49.            foreach (var person in list) {}
  50.  
  51.         NGUITools.AddChild ( Grid, ListItem); // ListItem - my prepared test prefab
  52.  
  53.         GetComponent<UIGrid> ( ).Reposition ( );
  54.     }
  55. }
  56.  
  57.  
« Last Edit: March 27, 2014, 11:02:10 AM by Vid »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: adding list to UIGrid
« Reply #1 on: March 28, 2014, 12:41:47 AM »
"How to create prefab by my list of received info"? Not sure what you mean.

NGUITools.AddChild returns a game object -- the instantiated object you can modify like anything else. If you had content on it, such as a label, you can GetComponent<> it and modify it for example.

Vid

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: adding list to UIGrid
« Reply #2 on: March 31, 2014, 06:11:35 AM »
Tnx, ArenMook, i used your advice and it`s worked fine.

But now i have another issue. I need to delete parentObject which contains UIToggle from UIGrid. In my grid i toggled needed records and after clicking on button should to delete their parentObject.

I`m write the following script but it works not as i expected. It works only if i toggle first record in grid and then it remove all records from current Grid.
  1. public GameObject Grid;
  2.  
  3. private void OnClick() {
  4.         GameObject parentItem = GameObject.Find ( "ListItem" );
  5.         var childItem =  parentItem.transform.FindChild ( "Checkbox" ).GetComponent<UIToggle> ( );
  6.         print ( childItem.isChecked );
  7.  
  8.         var obj = new List<GameObject> ( );
  9.  
  10.         foreach (Transform child in Grid.transform)
  11.         {
  12.             if ( childItem.isChecked )
  13.             {
  14.                 obj.Add(child.gameObject);
  15.                 obj.Remove(child.gameObject);
  16.             }
  17.  
  18.         }
  19.  
  20.  Grid.GetComponent<UIGrid> ( ).Reposition ( );
  21. }
  22.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: adding list to UIGrid
« Reply #3 on: March 31, 2014, 06:56:26 AM »
First... never use GameObject.Find.

Finding anything by name is terrible practice, and it's SLOW.

Next I don't get what your script is supposed to do. You iterate through the children, then you add and immediately remove the content. What's the point of this?

Vid

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: adding list to UIGrid
« Reply #4 on: March 31, 2014, 07:54:36 AM »
My script must to iterate through the grid, find out what childrens have active status "on" and delete this childrens.

UPD:i have modified the last piece of code a little and now it works, but not so right. By click, it removing only the half of checked children.
  1.     private void OnClick()
  2.     {
  3.         foreach (Transform child in Grid.transform)
  4.         {
  5.             var childNew = child.transform.FindChild ( "Checkbox" ).GetComponent<UIToggle> ( );
  6.             if ( childNew.isChecked )
  7.             {
  8.                 NGUITools.Destroy ( child.gameObject );
  9.             }
  10.         }
  11. }
  12.  

And i don`t know why this is happend, but my checkbox always visible when another gameObjects aren`t.
« Last Edit: March 31, 2014, 11:50:28 AM by Vid »

Vid

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: adding list to UIGrid
« Reply #5 on: April 01, 2014, 05:47:38 AM »
Have someone any idea about why it remove only the half "checked" childrens?

Vid

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: adding list to UIGrid
« Reply #6 on: April 01, 2014, 06:19:33 AM »
Well, i used
  1. Destroy ( child.gameObject );
  2.  
and it works fine. But my Grid.GetComponent<UIGrid> ( ).Reposition ( ) - doesn`t work and my table doesn`t refresh. I tried like this
  1.             yield return new WaitForEndOfFrame ( );
  2.             Grid.GetComponent<UIGrid> ( ).Reposition ( );
  3.             Grid.transform.parent.GetComponent<UIPanel> ( ).Refresh ( );
  4.             yield return new WaitForEndOfFrame ( );
  5.  
but still no effect.
Any suggestions?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: adding list to UIGrid
« Reply #7 on: April 01, 2014, 07:28:24 AM »
When you Destroy() an object, it won't actually be destroyed until next frame. If you check the transform's list of children, it will still be there as if nothing happened. You need to use NGUITools.Destroy -- it removes the object from the list of children properly.

And there is no need to yield.

However note that you can't use foreach like you're using. You should be using:

  1. while (Grid.transform.childCount > 0)
  2. {
  3.     Transform child = Grid.transform.GetChild(0);
  4.     ...
  5.     NGUITools.Destroy(child.gameObject);
  6. }

Vid

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: adding list to UIGrid
« Reply #8 on: April 01, 2014, 08:08:07 AM »
but in this case it will be infinity loop or i`ve missed something?
  1. while (Grid.transform.childCount > 0)  - childCount always would be > 0
  2. {
  3.     Transform child = Grid.transform.GetChild(0);  - i don`t need get the first child i need to loop through all
  4.  
  5.     var childNew = child.transform.FindChild ( "Checkbox" ).GetComponent<UIToggle> ( );
  6.  
  7.     if ( childNew.value ) {
  8.                 NGUITools.Destroy ( child.gameObject );
  9.     }
  10.  
  11. }
  12.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: adding list to UIGrid
« Reply #9 on: April 01, 2014, 09:57:08 AM »
In the code I posted all children get destroyed, so the while loop is not endless. If you need to do it selectively, then it's different. Bottom line is you can't iterate through a list that you're modifying.