Author Topic: Grid - Prefabs are positioned on the same position  (Read 13935 times)

giosolo

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Grid - Prefabs are positioned on the same position
« on: September 08, 2012, 03:53:07 AM »
I'm trying to have a list of check boxes with a vertical scrolling (Based on Example 7  -Scroll view panel).
I'm using a prefab of a check box. Everything is working fine when I add prefabs as children of the Grid via the editor. 
I'm missing out something when it comes to dynamically instantiate: all the check boxes are positioned on the same position.

Below is the script attached to the draggable panel.
  1. public class checkboxList: MonoBehaviour
  2. {      
  3.         public GameObject checkbox;
  4.         UIGrid grid;
  5.        
  6.         void Awake()
  7.         {
  8.                 grid = ((UIGrid)((transform.Find("UIGrid")).GetComponent("UIGrid")) );
  9.         }
  10.        
  11.  
  12.         void Start ()
  13.         {      
  14.                 List<Game.contact> MyContactList =  Game.ContactList.get();
  15.  
  16.                 for(int i = 0; i < MyContactList.Count; i++)
  17.                 {
  18.                         GameObject item = NGUITools.AddChild( transform.Find("UIGrid").gameObject,checkbox);
  19.                         ((UILabel)item.transform.Find("Label").GetComponent("UILabel")).text = MyContactList[i].fullname;
  20.                         item.transform.name = "item" + i.ToString();
  21.                 }
  22.  
  23.                 GameObject.Find("UIPanel (Clipped View)") .GetComponent<UIDraggablePanel>().ResetPosition();
  24.         }
  25.        
  26.         void Update ()
  27.         {
  28.                         grid.Reposition();
  29.         }
  30. }


Please help.

AndyGFX

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 24
    • View Profile
    • AndyGFX
Re: Grid - Prefabs are positioned on the same position
« Reply #1 on: September 08, 2012, 03:58:16 AM »
grid.Reposition();

add it at and of void Start()

giosolo

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Grid - Prefabs are positioned on the same position
« Reply #2 on: September 08, 2012, 04:21:16 AM »
Thanks but it didn't fix it.
 

AndyGFX

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 24
    • View Profile
    • AndyGFX
Re: Grid - Prefabs are positioned on the same position
« Reply #3 on: September 08, 2012, 05:49:07 AM »
1) ... and remove update void
2) set sorted to true on UIGrid
3) call reposition on grid
4) ResetPosition on panel

i have similar method used with Table and works.

  1.  
  2.    // Use this for initialization
  3.     void Start ()
  4.     {
  5.         GlobalEditorData.cells = new TSourceCell[this.cellWidth,this.cellHeight];
  6.        
  7.         this.table = NGUITools.FindInParents<UITable>(this.gameObject);
  8.         this.panel = NGUITools.FindInParents<UIDraggablePanel>(table.gameObject);
  9.         table.sorted = true;
  10.         this.table.columns = this.cellWidth;
  11.        
  12.         // rename objects to standard matrix direction from left -> right and from top -> down
  13.         int idx = 0;
  14.         for (int y=0;y<this.cellHeight;y++)                    
  15.         for (int x=0;x<this.cellWidth;x++)
  16.         {
  17.            
  18.             int cx = x;
  19.             int cy = y;
  20.             GlobalEditorData.cells[cx,cy] = new TSourceCell();                 
  21.              
  22.             GameObject cell = NGUITools.AddChild(this.gameObject,this.itemPrefab);
  23.            
  24.             string name_cell = string.Format("{0:0000}",idx);
  25.            
  26.             GlobalEditorData.cells[cx,cy].name = name_cell;
  27.             GlobalEditorData.cells[cx,cy].item = cell;
  28.             GlobalEditorData.cells[cx,cy].item.name = name_cell;
  29.            
  30.             string path = "/UI Root (2D)/Camera/Anchor/Source_Window/SourcePanelEmpty/SourceTable/"+GlobalEditorData.cells[cx,cy].item.name+"/ButtonEmpty_CMD";
  31.            
  32.             GameObject o = GameObject.Find(path);
  33.            
  34.             GlobalEditorData.cells[cx,cy]._cmd = (OnClick_SourceCell)o.GetComponent<OnClick_SourceCell>();                             
  35.             GlobalEditorData.cells[cx,cy]._cmd.parentName = GlobalEditorData.cells[cx,cy].item.name;
  36.             GlobalEditorData.cells[cx,cy]._cmd.label.text = cx.ToString()+"x"+cy.ToString();
  37.             GlobalEditorData.cells[cx,cy].icon = (UISlicedSprite)GlobalEditorData.cells[cx,cy]._cmd.GetComponentInChildren<UISlicedSprite>();
  38.            
  39.             idx++;
  40.            
  41.         }
  42.        
  43.        
  44.         this.table.Reposition();
  45.         this.panel.ResetPosition();
  46.        
  47.     }
  48.  
« Last Edit: September 08, 2012, 05:55:43 AM by AndyGFX »

giosolo

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Grid - Prefabs are positioned on the same position
« Reply #4 on: September 08, 2012, 11:08:24 AM »
Thanks.
Apparently the code was correct but the UIGrid was disabled   :-[