Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Mamawalter on June 18, 2014, 11:02:10 AM

Title: Hide object in UIGrid
Post by: Mamawalter on June 18, 2014, 11:02:10 AM
Hi,
for a builder game, i'm trying to show a menu under the selected building. I used a UIPanel + UIGrid + UIButtons for that.

When i select a building, i enable/disable buttons depending the type or state of the building (info, upgrade, produce unit, etc..) then positioning it under the selected building and finally active the panel.

The problem is that if i disable some buttons, the grid did'nt reposition buttons (grid's pivot set to center). Same thing for the sprite background with the "envelop content script". it has not resized.

So i'm wondering if my approach is correct. Can i use the same panel for all my buildings and just disable buttons or should i instanciate a empty panel and  add buttons to it by script.

Also is it possible to tween scale the panel when i active it? grid will handle it ?
Title: Re: Hide object in UIGrid
Post by: ArenMook on June 18, 2014, 05:05:50 PM
You need to call the grid's Reposition() function. It won't reposition content without you explicitly calling it.
Title: Re: Hide object in UIGrid
Post by: Mamawalter on June 19, 2014, 08:18:46 AM
Thank you, i choose to "addchild" the buttons each time the menu is open and then call "Reposition()". Buttons are destroyed when the menu is hidden. I had to use "DestroyImmediate" otherwise sometimes reposition was not completely correct.

  1. void removeButtons()
  2.     {
  3.         while (transform.childCount > 0)
  4.         {
  5.             DestroyImmediate(transform.GetChild(0).gameObject);
  6.         }          
  7.     }
Title: Re: Hide object in UIGrid
Post by: raydenuni on June 19, 2014, 12:09:35 PM
Use

  1. NGUITools.Destroy(card.gameObject);
  2.  

The problem is that GameObject.Destroy() doesn't happen until the following frame. So you need to remove it from the transform this frame so that the grid will reposition properly. Unity documentation says you should *NOT* use DestroyImmediate except for editor code. (http://docs.unity3d.com/ScriptReference/Object.DestroyImmediate.html)