Author Topic: OnClick() problems on WP8 and Windows Store apps  (Read 12180 times)

Lexile

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
OnClick() problems on WP8 and Windows Store apps
« on: August 01, 2013, 01:43:29 AM »
Heyah. Tried searching if anybody had already asked about this problem but didn't find anything. So here goes nothing:

Whenever I build & deploy my program either as a Windows Phone 8 app (to Lumia 920) or as a Windows Store app (to a Samsung touchscreen tablet), the OnClick()-function executes it's code twice per tap. This behaviour doesn't happen a) in the editor, or b) with a mouse (on the tablet).

Only noticed this problem when I found out (heard) that my camera app took a picture twice every time I pressed the "shutter button" (basically a full screen collider). Tested on both the phone & the tablet in the same app and by creating a new project and deploying from two different PCs. Tapping on a Unity built-in GUI.Button detects only one click but tapping on buttons created by NGUI creates two clicks (using OnClick()). Once again, this only happens with touches. With mouse clicks, both buttons work as they should.

Any ideas what could be causing this?

woodlee

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: OnClick() problems on WP8 and Windows Store apps
« Reply #1 on: August 01, 2013, 04:36:57 AM »
i has same problems on lumia 520. :(

Goepfie

  • Guest
Re: OnClick() problems on WP8 and Windows Store apps
« Reply #2 on: August 01, 2013, 02:01:31 PM »
We are having similar problems with NGUI and Windows 8 input handling on our latest built. Regardless of an ARM or x68 built. Double triggered Events would explain a lot of the strange behaviour, going to run some tests today.

Would appreciate support from the devs!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnClick() problems on WP8 and Windows Store apps
« Reply #3 on: August 01, 2013, 05:40:46 PM »
Turn off mouse events on the UICamera.

Goepfie

  • Guest
Re: OnClick() problems on WP8 and Windows Store apps
« Reply #4 on: August 01, 2013, 06:13:11 PM »
Disabling Mouse Events is not an option for us. So I decided to write a little hack into UICamera.Notify which ignores OnClick Messages being send in a time frame of a 1/10 second since the last one:

  1. public static float _lastClickTime = 0f
  2. ...
  3. #if UNITY_METRO //HACKING
  4.             if (funcName == "OnClick")
  5.             {
  6.                 if (Time.time <= _lastClickTime + 0.1)
  7.                     return;
  8.  
  9.                 _lastClickTime = Time.time;
  10.             }
  11. #endif

Might have some ugly consequences, but for now it does the trick.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnClick() problems on WP8 and Windows Store apps
« Reply #5 on: August 01, 2013, 10:29:07 PM »
And why is disabling the mouse not an option? It's a touch device. React to touch events.

Lexile

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: OnClick() problems on WP8 and Windows Store apps
« Reply #6 on: August 02, 2013, 01:17:47 AM »
And why is disabling the mouse not an option? It's a touch device. React to touch events.

I'd guess because on x86 Win 8 devices you have the option to use both mouse & touch, unlike normal tablets (ie. iPad).

For me, disabling mouse functionality works (as I don't need mouse for other than debugging purposes). Thank you!

Lycean

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: OnClick() problems on WP8 and Windows Store apps
« Reply #7 on: October 13, 2013, 11:42:41 PM »
I ran into this issue too, and though this is an old thread I though it might be worth providing my fix. This fix simply detects whether an touches were processed and if so skips mouse processing for this update. The line numbers for this fix are for NGUI 3.0.0, but same process works fine for 2.6.3 too.

In UICamera.cs:

Replace lines 803-807 (in the Update() method) with the following code (note that touch inputs are now processed before mouse inputs):
        bool haveTouches = false;
        // Process touch input
        if (useTouch) haveTouches = ProcessTouches();

        // Update mouse input
        if (!haveTouches && (useMouse || (useTouch && mIsEditor))) ProcessMouse();


Change line 968 "public void ProcessTouches ()" to "public bool ProcessTouches ()".

Before the closing brace for the ProcessTouches method (now line 1015) insert this code:
        return Input.touchCount > 0;

Hope this helps someone.

flavoredCoffee

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 15
    • View Profile
    • CWGTech Unity & Programming blog
Re: OnClick() problems on WP8 and Windows Store apps
« Reply #8 on: October 14, 2013, 03:14:21 PM »
I turned off the touch events on the NGUI UICamera -- Unity will emulate the mouse with Win8 touch events, so it works fine with a mouse or with touch.  Runs on WinRT devices, Win8 Desktops & Win8 Laptops with touch.

skullthug

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 37
    • View Profile
Re: OnClick() problems on WP8 and Windows Store apps
« Reply #9 on: June 21, 2015, 03:51:46 PM »
Hello, I'm writing to say this problem still exists and I've been running into this problem with my own project that is being ported to Windows8 Universal.
Goepfie's solution no longer seems to work, so I'm in the middle of attempting Lycean's- though the code has changed enough since then to make it difficult to interpret.

But ANYWHO, fyi this still happens in NGUI 3.7.7 and Unity 4.6.4.
Fortunately in my setup it doesn't seem to have any major consequences, however it does create a weird visual bug where a button press (with touch) in a previous screen will cause a button in the next screen (that ends up in the same space of the previous button) to be put in the OnHover state of UIButton

I've discovered disabling Mouse in the UICamera stops it from happening- but this is not an acceptable solution since mentioned previously the mouse is required for Windows8 PCs/tablets that can utilize it.

Disabling touch events seems to have no effect, though that's probably not an option for me anyways since I utilize multiple finger tracking in certain screens.

It looks like I'm a bit behind on versions at this point so I'll attempt to see if upgrading fixes anything.

EDIT:
Upgrading to 3.9.0 fixed it!
It looks like in 3.8.2 there was some re-working to the way touches interact with the UICamera, which may have sorted this out. Just a FYI for anyone else running into my problem.
« Last Edit: June 21, 2015, 04:49:24 PM by skullthug »