oddgames / UIToolkit

Single draw call UI solution for Unity with multi resolution support and more.
517 stars 153 forks source link

Scrollable Container unable to Destroy() or Clear() etc. #134

Open maconbot opened 11 years ago

maconbot commented 11 years ago

So far (few hours spent) I have not been able to find a way to destroy or remove a scrollable container.

I am using the UIScrollableHorizontalLayout and have tried many work arounds including: 1.) UI.Destroy(); 2.) First hiding the scrollable layout and then attempting to .Clear()....Remove Children...make scrollable = null; and many other attempts.

I have had some success with making the scrollable layout cease to function, and then it seems that I can't clear the buttons from the UICamera. Ultimately I am stuck.

I currently am looking at building 5-10 different UIScrollableHorizontalLayout's and leaving them hidden until use, as a work around. I am not sure how efficient this is and would prefer to be able to create my horizontal scroller new each time it is called (this way I can add and subtract what is in the scroller) and then be able to delete/destroy it once I am done with it.

Thanks for any help. Cheers, Chad.

maconbot commented 11 years ago

For anyone else who runs into this...I figured out a solution. Not sure if it is a work around or how it is supposed to be done.

ScrollableHorizontalContainerManager.cs:

Create an array to hold the buttons: private UITouchableSprite[] _currentButtons;

In the for loop where you add the buttons store the touchable UIButton: if(i==0) { touchable = UIButton.create( "1-Up.png", "1-Down.png", 0, 0 ); _currentButtons[i] = touchable; }

Remove them when touched (this is the method pointed to in your touch handler - button.onTouchUpInside += ( sender ) => Method(scrollable);)... void Method(UIScrollableHorizontalLayout scrollable) { var buttons = scrollable as UIScrollableHorizontalLayout; for( var i = 0; i < length; i++ ) { buttons.removeChild(_currentButtons[i], true); _currentButtons[i]=null; } }

The UIScrollableHorizontalLayout remains as a child under the UI GameObject, but now with no children. This lets me dynamically create the scroller every time. Given that you instance() the ScrollableHorizontalContainerManager and access it that way.

Cheers gang, Chad