Author Topic: Problem with events on UISlider  (Read 4978 times)

Darkmax

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 93
    • View Profile
Problem with events on UISlider
« on: March 27, 2014, 09:23:15 PM »
Hi I'm making a switch behavior with the slider prefab, and making it of two steps, and I have a script attached to the object that catch when the slider change of value to play the sound "swipe" that is on ngui.
The problem is that I have the slider is hided at first and the when the user press a button it shows the slider and it appears that it triggers a the event of the slider, and plays the sound, but I didn't use the switch.

So how I prevent the slider triggers the onchange method when I show the switch?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem with events on UISlider
« Reply #1 on: March 28, 2014, 12:57:58 AM »
What switch? I don't understand what you are trying to do here. If you're trying to play a sound while dragging a slider, simply have a looping sound that you start playing in OnPress (true) and stop in OnPress(false).

Darkmax

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 93
    • View Profile
Re: Problem with events on UISlider
« Reply #2 on: March 28, 2014, 01:11:52 AM »
Sorry if I didn't my self clear, I'm making a switch, using the UISlider but with steps = 2, and it works great, and I want to play a sound when the value is changed, to accomplish this I call a method the value change from 0 to 1 or viceversa, the problem when I enable the GameObject the event onValueChanged is called and is played the sound when I didn't change the value.

I hope I make self more clear, if doesn't tell me and I will tried to make a video or mini example of what I'm talking.

To reproduce the problem that I'm having Just create a slider, and add this script:
  1. void Awake()
  2.     {
  3.         EventDelegate.Add(GetComponent<UISlider>().onChange, OnValueChange);
  4.     }
  5.  
  6.     public void OnValueChange()
  7.     {
  8.         Debug.Log("Hello");
  9.     }
  10.  

then disable the gameObject of the slider, hit play, and enable the slider again, and you will see the hello printed when one didn't move the slider.
« Last Edit: March 28, 2014, 01:46:20 AM by Darkmax »

rain

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 79
    • View Profile
Re: Problem with events on UISlider
« Reply #3 on: March 28, 2014, 05:56:35 AM »
Have you tried delaying your event subscription with a frame or two? Or you could have something like this:
  1. [SerializeField] UISlider Slider;
  2. void Awake()
  3. {
  4.      EventDelegate.Add(Slider.onChange, HandleFirstValueChange, true);
  5. }
  6.  
  7. // This callback runs at the first value change when you don't want to hear your swipe sound,
  8. // adds the sound playing method to onChange and then HandleFirstValueOnChange immediately gets unsubscribed
  9. // so only your sound playing method is called from now on.
  10. // You could also make this method as lambda if you wanted.
  11. void HandleFirstValueChange()
  12. {
  13.      EventDelegate.Add(Slider.onChange, PlaySoundOnValueChange);
  14. }
  15.  
  16. void PlaySoundOnValueChange()
  17. {
  18.      // Play sound or do something funky here
  19. }
  20.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem with events on UISlider
« Reply #4 on: March 28, 2014, 09:12:31 AM »
Note that Awake() is not the right place to do this. Start() is the proper place to start adding content.

Awake() is triggered as soon as the script is added, which may actually before other Awakes() had a chance to call or even before certain things were initialized. This is bad. Don't use it for anything that involves touching stuff outside the class you're in.

Darkmax

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 93
    • View Profile
Re: Problem with events on UISlider
« Reply #5 on: March 28, 2014, 08:01:12 PM »
I changed to start and happens the same thing, also if I did what rain said, the first time when I enable the GameObject the method is called like I said before

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem with events on UISlider
« Reply #6 on: March 29, 2014, 06:05:51 AM »
The callback is called on startup for consistency. If you want your Start() function to execute after the slider, modify your script's script execution options.

Darkmax

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 93
    • View Profile
Re: Problem with events on UISlider
« Reply #7 on: March 30, 2014, 05:48:17 AM »
Ok I resolve my problem, I just need to modify the script UIProgessBar of ngui and comment this lines that are on the Start() of this script
  1. if (onChange != null)
  2.             {
  3.                 current = this;
  4.                 EventDelegate.Execute(onChange);
  5.                 current = null;
  6.             }

This was causing the call of the method onChange when I enable the progress bar(switch) the first time (When the start is executed of this script), I just wanted to call this method onChange when the value is changed not also when I enable the progress bar.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem with events on UISlider
« Reply #8 on: March 30, 2014, 07:49:01 AM »
I wouldn't recommend doing this.

First, because it will be wiped as soon as you update, and second -- because this breaks consistency. Any callback associated with the slider must be set to the correct state whenever the slider is enabled. If you don't, then the slider may show one thing and the data associated with the callback may show another. This is much worse.

Darkmax

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 93
    • View Profile
Re: Problem with events on UISlider
« Reply #9 on: March 30, 2014, 08:27:18 AM »
Ok, I will change it back then