Author Topic: Hide object in UIGrid  (Read 3194 times)

Mamawalter

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 2
    • View Profile
Hide object in UIGrid
« 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 ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Hide object in UIGrid
« Reply #1 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.

Mamawalter

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Hide object in UIGrid
« Reply #2 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.     }

raydenuni

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 18
    • View Profile
Re: Hide object in UIGrid
« Reply #3 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)