Author Topic: Event Messages being delayed  (Read 9242 times)

loopyllama

  • Guest
Event Messages being delayed
« on: April 22, 2012, 04:05:50 AM »
I have a UISlider. I attach a script that has this method :
void OnPress (bool isPressed)
  1. {
  2.     float theTime = Time.time;
  3.     print ("received at time: " +theTime);
  4. }
then in UICamera.cs I place a line that says
  1. print ("sending OnPress message at time: "+ Time.time);
in the if (unpressed) block on method Process Touch (bool, bool)
After this I click and drag my slider around a number of times, wait a little, then drag it a few more times.
What I is that the print is UICamera is always correct and happens exactly when I release the slider. It is supposed to be sending out the message to the "currentTouch". My OnPress method ALWAYS gets the OnPress (false) message, but often there is a significant time gap between when the message was "sent" in UICamera and when it was received in my OnPress method.

It appears to me that the something is going on with your currentTouch.SendMessage and sometimes it "forgets" to send the message right away if you stop interacting with the slider. The next time you interact with the slider, it will finally send the message, plus a second message that it has been touched. For example, this is reproducible most of the time:

--drag the slider once, let's say at time 15.0F.
--you will see the UICamera print at time 15.0F, but not the OnPress print..it will not get the message.
--wait five seconds
--drag the slider a second time
--you will see now the first OnPress print happen late at time 20.0F, the second print from OnCamera print at time 20.0F, and the second OnPress messaage print at 20.0F.

The message is always delivered but not always sent right away.

How is this affecting me? I want to reset the slider to the middle when it is released. Since I do not always get the OnPress message right away, my slider does not always reset to the middle.

You might say, hey! This post is very confusing and this guy seems lost, how can I possibly read this post and be expected to reproduce this so-called error? Easy: Make a slider that always snaps back to the center after being used. You will run into the delayed message problem.

I can hack right into the UICamera script and add my code right there to get my message instantly. Any better ideas?
« Last Edit: April 22, 2012, 04:14:21 AM by loopyllama »

loopyllama

  • Guest
Re: Event Messages being delayed
« Reply #1 on: April 22, 2012, 04:33:09 AM »
More info:
There is indeed a logic bug in the NGUI code. If I hack into UICamera.cs in if (unpressed) in method ProcessTouch (bool,bool) with this code:
  1. gameObject.SendMessage("OnPress", false, SendMessageOptions.DontRequireReceiver);
and add my onPress script to the NGUI camera game object then the message is always delivered right away. ugh.
« Last Edit: April 22, 2012, 04:35:51 AM by loopyllama »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Event Messages being delayed
« Reply #2 on: April 22, 2012, 03:05:43 PM »
All I have to say is... huh?

There is no delay. Messages arrive right away, and I see the Debug.Log("OnPress " + isPressed) at the same time.

What version of NGUI and what version of Unity are you using?

loopyllama

  • Guest
Re: Event Messages being delayed
« Reply #3 on: April 22, 2012, 11:20:02 PM »
I spent my entire Sunday on this bug. Please believe that it is there and is reproducible! I was able to recreate the bug on both a windows and mac machine using Unity 3.5.1f2 using an old version of NGUI as well as 2.0.3d. I can reproduce it using an NGUI example in a default scene with a default project. If you can give me a place to send it then I can give you this test case. I do not think that is necessary because I can provide the exact steps to reproduce below. Please bear with me. Here are the steps to reproduce the bug (there are two versions of this bug. I will show both):

1. Open New Unity project. Import latest version of NGUI. Open NGUI scene called "Tutorial 7 - Slider".
2. create a new .cs script called "ResetSliderOnRelease"
3. paste these contents into above ResetSliderOnRelease .cs script:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ResetSliderOnRelease : MonoBehaviour
  5. {
  6.         public UISlider theSlider;
  7.         float fMiddle = 0.5F;
  8.        
  9.         void OnPress (bool isPressed)
  10.         {
  11.                 float theTime = Time.time;
  12.                 print ("got message, isPressed is: " + isPressed + " at " + theTime);
  13.                
  14.                 if (theSlider != null && isPressed == false)
  15.                 {
  16.                         theSlider.sliderValue = fMiddle;
  17.                         theSlider.ForceUpdate ();
  18.                 }
  19.         }
  20. }

BUG ONE:
1. Place ResetSliderOnRelease.cs on "Slider - Horizontal"
2. Drag game object "Slider - Horizontal" onto "UISlider" slot of "Slider - Horizontal" game object.
3. In UICamera.cs, insert this code on line 863, which  is the "if (unpressed){" code block in method "ProcessTouch (bool, bool)"
  1. print ("In UICamera, sending slider is unpressed message at time " + Time.time + "...you should see the slider snap back to the middle immediately...");
4. Press Play in Unity.
5. Drag the slider and release. Notice the console prints the sent unpressed message in UICamera.
6. Notice there is no print in ResetSliderOnRelease.cs because it has not received the unpressed message that was sent.
8. Wait 10 to 30 seconds. Notice the message is still not received.
7. Click anywhere on the slider. The slider now snaps back to center.
9. Inspect the prints in the console and notice the OnPressed message was finally received! As well as the second message!
10. Notice how dragging "breaks" the snapping back to center while clicking works fine.

BUG TWO:
1. Press Stop in Unity. Remove ResetSliderOnRelease.cs from "Slider - Horizontal" and place on its child "UISlicedSprite - Thumb".
2. Drag the "UISlicedSprite - Thumb" game object onto the "UISlider" slot of "UISlicedSprite - Thumb".
3. Press Play in  Unity.
4. Notice how the bug is now inverted... clicking is "broken" and dragging works fine.



Current Workarounds for bug:
1. Hack into UICamera.cs. Add a custom bool called HACKOnPressToThisGOOnly. If bool is true, send message to gameObject.SendMessage. Apply ResetSliderOnRelease.cs to NGUI Camera game object.
-OR-
2. Place ResetSliderOnRelease.cs on both the slider and thumb.

I would greatly appreciate if you could try to reproduce this bug and fix it!
« Last Edit: April 22, 2012, 11:23:30 PM by loopyllama »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Event Messages being delayed
« Reply #4 on: April 23, 2012, 01:11:48 AM »
I followed your instructions, pasted your script on the slider in Tutorial 7. Works fine. Created a brand new scene with a slider, put that script there. Works fine. Opened the project in 3.4.2 and 3.5.1... both work fine. Your script resets the position to 0.5 when the touch ends.

Whatever is happening, it ain't on NGUI's side.

loopyllama

  • Guest
Re: Event Messages being delayed
« Reply #5 on: April 23, 2012, 01:24:39 AM »
When you paste the code on the THUMB object of the slider and CLICK (NOT DRAG BUT SINGLE CLICK SOMEWHERE ON THE SLIDER), the slider does not snap back to the middle because it does not receive the message right away. Please take a minute and try this again.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Event Messages being delayed
« Reply #6 on: April 23, 2012, 01:35:56 AM »
Sigh.

The thumb?

The thumb doesn't receive the OnPress event. Never did! The Slider gets the OnPress event, since that's what you're pressing on, not the thumb.

You're placing your script on the wrong object. Place it on the slider.

loopyllama

  • Guest
Re: Event Messages being delayed
« Reply #7 on: April 23, 2012, 01:46:00 AM »
Please do not get upset with me. In my post you can see under BUG 2 I place it on the thumb. This means you did not read my post. I have taken the time to explore this. I have taken the time to document the issue. You could at least read my post.

The bug is still reproducible on the slider itself. Please take a minute and try. Again I repeat myself but please read this carefully:

Place the code on the slider itself. Reference the UISlider in the inspector for the added component. NOW CLICK SOMEWHERE ON THE SLIDER. Dragging works-- the slider snaps back to center. BUT CLICKING FAILS TO GET THE EVENT RIGHT AWAY

Since I can reproduce this in a default project with NGUI examples on two different computers I am sure you can reproduce this.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Event Messages being delayed
« Reply #8 on: April 23, 2012, 01:51:37 AM »
I read your post. I followed your instructions, but you don't seem to believe me, so here:

http://www.tasharen.com/temp/sigh/

loopyllama

  • Guest
Re: Event Messages being delayed
« Reply #9 on: April 23, 2012, 02:15:17 AM »
That is awesome of you to do thank you! I see the difference of what you are trying. You are doing everything correct except one thing: when you drag in the video you do not drag from where the thumb is... you hover over the thumb, but then you click slightly to the right of the slider, then drag. That does work. But if you click directly on the thumb then slide, it breaks. I promise you!
Please try that!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Event Messages being delayed
« Reply #10 on: April 23, 2012, 02:17:49 AM »
Right, and I explained why what happens. When you click on the thumb, the slider doesn't get the OnPress. Your thumb has a collider of its own. It intercepts the OnPress event. Either disable the collider, or attach another script to the thumb in order to handle it.

loopyllama

  • Guest
Re: Event Messages being delayed
« Reply #11 on: April 23, 2012, 02:32:01 AM »
Yes, if you disable the collider on the thumb OR attach the same script to the thumb, it indeed works!
Thank you for solving this. It was not obvious to me but I understand it now.

nsxdavid

  • Guest
Re: Event Messages being delayed
« Reply #12 on: April 23, 2012, 10:07:50 AM »
A picture is worth a thousand words.  Turns out a video is worth a thousand pictures too.

paulygon

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Event Messages being delayed
« Reply #13 on: May 17, 2012, 11:22:33 AM »
This was a valuable conversation for me as the way NGUI and C# works, slowly dawns a bit more every day. I understand where the sighs come from, so I want ArenMook to know, your time was well spent in answering this.