Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: laurentl on April 03, 2013, 03:37:02 AM

Title: Trying to populate a draggable list by code yields to giant Content
Post by: laurentl on April 03, 2013, 03:37:02 AM
Out of an array I instantiate Draggable Content one by one and parent them under a grid. The grid is under the Draggable Panel.
When I do this process by hand the draggable content lines up properly and preserves its dimensions.
When I do it in Start however the elements are about 100x their size.
It seems I need to trigger some calculation by hand to resize these elements properly, I tried grid.Reposition and it doesn't change a thing.

What's the secret sauce ?

  1.         [System.Serializable]
  2.         public class Decal
  3.         {
  4.                 public string name;
  5.                 public Texture decal;
  6.                 public DraggableButton draggableButton;
  7.         }
  8.        
  9.         public List<Decal> decals;
  10.        
  11.         public DraggableButton buttonPrefab;
  12.         public UIGrid gridParent;
  13.        
  14.         void Start ()
  15.         {
  16.                 for (int i=0; i<decals.Count; i++) {
  17.                         Decal d = decals [i];
  18.                         GameObject dg = (GameObject)Instantiate (buttonPrefab.gameObject);
  19.                         dg.name = "item " + i.ToString ();
  20.                         dg.transform.parent = gridParent.transform;
  21.                         d.draggableButton = dg.GetComponent<DraggableButton> ();
  22.                         // init the butten
  23.                         d.draggableButton.SetDecal (d.decal);
  24.                         d.draggableButton.SetName (d.name);
  25.                         d.draggableButton.SetCallBack (gameObject, "ChooseDecal");
  26.                 }
  27.                 gridParent.Reposition ();
  28.         }


PS: amazing customer service !
Title: Re: Trying to populate a draggable list by code yields to giant Content
Post by: ArenMook on April 03, 2013, 02:57:08 PM
Never use Instantiate. Use NGUITools.AddChild.
Title: Re: Trying to populate a draggable list by code yields to giant Content
Post by: laurentl on April 03, 2013, 03:32:37 PM
Perfect! simplifies code.

And it seems I still need to
Reposition () the grip and Recenter the UICenterOnChild