Author Topic: Changing the "value" of a disabled UIToggle problems  (Read 4071 times)

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Changing the "value" of a disabled UIToggle problems
« on: September 17, 2014, 07:55:43 AM »
Hey,

I have a tabbed interface for an in-game market. There are 4 tabs (UIToggle with same group index) each activating their respective UIScrollView. These scroll views contain their own set of UIToggle 's for the items being sold in the game. This works great with no problems.

What I want to do is; I want to select the first item of the related scroll view when the market tab changes. I want to set it's value to true. I tried two ways, both didn't work completely.

1. I programmed a method which finds the first item of the given category and set it's value to true. I invoked this method through the each category UIToggle's OnValueChange by hooking it through it's inspector. Upon testing it I realized the first item is accepted as the "activated" one after doing this because I can't select it anymore unless I select something else first. But it's animation doesn't play (The animation it should play when it's the activated UIToggle in the group). And it's own OnValueChange is never called.

2. I added a bool to the UIScrollView called "activateFirstItemOnEnable". I've also added it to the UIScrollViewEditor so I can see this bool in the inspector. What it does is, it calls my above mentioned method of finding and activating the first item in the UIScrollView 's OnEnable() if activateFirstItemOnEnable is true. This way the animation of the first item 's UIToggle plays nicely. But if I have selected -for example- item5, changed category, and came back to this category, item5 's dismiss animation doesn't play, it just stays with the "active" animation so it looks like both item1 and item5 are active at the same time. Also the item1 's OnValueChange is not called this way either (I want it because I refresh some labels containing the details of the selected item).

TL;DR So how can I make it so that when I enable a scroll view with a button, it's first UIToggle item get's selected, plays it's animation smoothly, invokes it's OnValueChanged method and disbles all other UIToggle's in the same group under this scroll view.

Hope it makes sense, thanks in advance for all the help!
« Last Edit: September 17, 2014, 08:01:14 AM by Sarper »

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Changing the "value" of a disabled UIToggle problems
« Reply #1 on: September 17, 2014, 09:32:11 AM »
Upon further testing, if I call a coroutine to activate the first item in the activated scroll list with a little delay it works correctly. So I'm sure it happens because the scroll list or the items beneath it are disabled at the time of calling this method. Any elegant solutions?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing the "value" of a disabled UIToggle problems
« Reply #2 on: September 17, 2014, 12:50:44 PM »
Disabled object would certanly do it. You need to interact with enabled and active components, not disabled ones. Enable the game object, then change the state of its components.

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Changing the "value" of a disabled UIToggle problems
« Reply #3 on: September 17, 2014, 01:43:07 PM »
Disabled object would certanly do it. You need to interact with enabled and active components, not disabled ones. Enable the game object, then change the state of its components.

Calling it on the first item of the scroll view's OnEnable and it still doesn't produce the desired result.

EDIT: More specifically, OnValueChanged of the UIToggle is not getting invoked when I change it's "value" to "true" via scripting.
EDIT2: If I call the below code right after I set the value to true in OnEnable, it works, why would this be?

  1. if(EventDelegate.IsValid(GetComponent<UIToggle>().onChange)) EventDelegate.Execute(GetComponent<UIToggle>().onChange);

EDIT3: I changed a code block in the UIToggle to fix this. I changed this to

  1. if (current == null)
  2. {
  3.         current = this;
  4.  
  5.         if (EventDelegate.IsValid(onChange))
  6.         {
  7.                 EventDelegate.Execute(onChange);
  8.         }
  9.         else if (eventReceiver != null && !string.IsNullOrEmpty(functionName))
  10.         {
  11.                 // Legacy functionality support (for backwards compatibility)
  12.                 eventReceiver.SendMessage(functionName, mIsActive, SendMessageOptions.DontRequireReceiver);
  13.         }
  14.         current = null;
  15. }
  16.  

this

  1. if (current == null) current = this;
  2.  
  3. if (EventDelegate.IsValid(onChange)) EventDelegate.Execute(onChange);
  4. else if (eventReceiver != null && !string.IsNullOrEmpty(functionName)) {
  5.         // Legacy functionality support (for backwards compatibility)
  6.         eventReceiver.SendMessage(functionName, mIsActive, SendMessageOptions.DontRequireReceiver);
  7. }
  8.  
  9. current = null;
  10.  
« Last Edit: September 17, 2014, 02:09:47 PM by Sarper »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing the "value" of a disabled UIToggle problems
« Reply #4 on: September 18, 2014, 03:14:30 PM »
You need to update your NGUI...

Sarper

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Changing the "value" of a disabled UIToggle problems
« Reply #5 on: September 18, 2014, 03:30:09 PM »
You need to update your NGUI...

Haha yeah, the updates are so frequent, it's hard to keep a track of them! (No complaints here)

I'm currently using 3.7.1 in this project, just a single update away from the latest.

The problem with updating it is I have a variety of additions and changes made on it and I don't want to go through comparing and merging code files and I don't have my changes noted somewhere.

I'm also not sure if you would be willing to take a look at my changes if I compile them in an email as they come up. I think it would be good to see what other people need and couldn't accomplish without playing with the codebase.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing the "value" of a disabled UIToggle problems
« Reply #6 on: September 18, 2014, 05:18:50 PM »
Your chances are not necessary because that part of the code has already been modified to accommodate for similar changes.

You wouldn't have encountered this issue in 3.7.2 :P