Author Topic: UICheckbox OnEnable  (Read 5233 times)

getluky

  • Guest
UICheckbox OnEnable
« on: May 01, 2012, 04:48:25 PM »
I'm using multiple UICheckboxes with UICheckboxControlledObject, and noticed that if I use NGUITools.SetActive to open, close, then open a panel with a radio group of these, the code in Start() doesn't get executed again, so it only respects the startsChecked property once. I'm using the checkbox group as a tab bar of sorts, where each tab controls the visibility of a separate panel.

When the entire hierarchy is activated the first time, the radio group works as expected and only the panel controlled by the UICheckbox is active. However, that second open doesn't run Start() again on the checkboxes, so all of the panels become visible and show up on top of one another, which is a mess. To get around this, I copied the initialization code from Start() to OnEnable(), which seems to work. It seems like the UICheckboxes in a radio group should respect startsSelected whenever it is enabled, which is very common when working with multiple panels.

UICheckboxControlledComponent doesn't have this problem because deactivated UIWidgets don't get re-enabled when SetActiveRecursively gets called on a parent GameObject.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UICheckbox OnEnable
« Reply #1 on: May 01, 2012, 08:18:04 PM »
I'm not sure I understand the problem. The start function gets called only once, and that's fine. Why would you want it to execute every time it's enabled? What changes while it's disabled, and why does it change?

getluky

  • Guest
Re: UICheckbox OnEnable
« Reply #2 on: May 01, 2012, 08:50:02 PM »
Upon further examination, I think I understand my problem better, and it is actually a different issue (possibly user error).

The issue is that when a radio button group and its UICheckboxControlledComponent target(s) all get activated at once (such as with a large UI prefab that gets NGUITools.SetActive called on it), on the first activation, the UICheckbox.Set that gets called within Start also cause UICheckboxControlledComponents to "respect" the checkbox setting immediately.

But let's say you take that big hierarchy and deactivate then reactivate again, Start has already been called, and all of those UICheckboxControlledObjects will be activated at once (because I use NGUITools.SetActive on a common parent).

Arguably, this is a case where I should place the targets and the radio group in separate hierarchy, but because the behavior works "as expected" on Start, it was confusing to me. Instead of separating things, or modifying UICheckbox, I actually can achieve the desired result by modifying UICheckboxControlledObject to cache the last call to OnActivate and using it to enable/disable the target within OnEnable there. This is a smaller modification and does seem more sensible to me.

Hope that makes more sense now!


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UICheckbox OnEnable
« Reply #3 on: May 02, 2012, 08:15:24 AM »
Yep it's just how you're using the components. Using SetActive will enable everything under the object you specified, whether those objects were enabled before or not. CheckboxControlledObject script is just a tiny example script, and it just doesn't have enough code to work properly in a situation like yours. I suggest you changing it to the observer pattern: place a custom script on your objects with a reference to the checkbox, and when that script gets OnEnable, check the state of the checkbox -- should it really be enabled? If not, disable it.