Author Topic: Button OnLongPress  (Read 6822 times)

rganeyev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Button OnLongPress
« on: October 03, 2012, 07:25:49 AM »
Does button handle long press? If yes, is it possible to use it inside scrollview, so it will work properly?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button OnLongPress
« Reply #1 on: October 03, 2012, 07:37:26 AM »
There is no such event. If you want something like that, record time in OnPress(true), and keep checking it in Update.

silversteez

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
    • MattSilverstein.com
Re: Button OnLongPress
« Reply #2 on: March 01, 2013, 03:00:27 PM »
Hey,

Sorry to necro this post, but I'm dealing with something along these lines...

I have an OnPress UIeventlistener on several gameObjects that i'm listening to in a manager. I'm trying to figure out how to detect a long press as described above...

I receive the initial OnPress event and would then like to keep checking if OnPress is true on this gameObject, but i can't figure out how to do it.

- Is there a bool stored in the gameObject's UIeventlistener that i can access via my update method that maintains it's OnPress value? 
- Is there some other way to continue 'checking' if the gameObject is still pressed each frame?

Sorry if this is obvious/dumb stuff and thanks very much!

edit:
P.S. I know one obvious way to achieve a similar effect would be to start a timer on OnPress(true) and cancel it on OnPress(false) - I was using that method previously, but I had some issues where the OnPress(false) wouldn't fire in some circumstances so I figured it would be nice to have another method to try out.
« Last Edit: March 01, 2013, 03:06:06 PM by silversteez »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Button OnLongPress
« Reply #3 on: March 01, 2013, 04:49:48 PM »
You manager can save a list (or array) of game gameObjects which have been OnPress (true) since the UIEventListener sends the whole gameObject and a list of TimeStamps (Time.realTimeSinceStartup) corresponding with it.

Then when you receive a OnPress(false), you can compare which GO it was that was released and do your logic based on the current realtimesincestartup - savedRealTimeSinceStartup.

Does that make sense?

silversteez

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
    • MattSilverstein.com
Re: Button OnLongPress
« Reply #4 on: March 01, 2013, 06:29:11 PM »
Thanks Nicki. Yes, that does make sense, but it doesn't really solve another part of the problem I'm having....

The GO's I'm tracking move around. The user can touch an area with no GO, but a GO might appear there after a few seconds at which point I'd like to now start tracking the length of time they are pressing the GO. I want to avoid forcing the user to lift their finger and repress the same GO that is already beneath their finger.

Perhaps I'm coming at this the wrong way. Is there a way to continually check what GO is beneath the user's finger when pressed? Something I can fire every frame in Update()? Something like UICamera.hoveredObject, but for pressed/clicked objects. Perhaps if I can filter UICamera.hoveredObject to return a GO only if there is a touch or click detected...

Oh hey...hmm, I just tried this in Update:
  1. if(UICamera.touchCount >= 1 && UICamera.hoveredObject != null)
  2.         {
  3.                 print (UICamera.hoveredObject.ToString());
  4.         }
  5.  
 

It seems to work. Is this an acceptable way to check what GO a user is touching at any given point in time?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Button OnLongPress
« Reply #5 on: March 02, 2013, 09:12:28 AM »
Oh now that is tricky. But that means you won't have your colliders on the affected gameobject, but instead have them "somewhere else" otherwise you can't click them right?

It's a matter of using a different game object as an intermediary - so the one you actually pressed on is captured and this should get a reference to the one it should affect when it appears on screen. The affected game object should timestamp when it's turned on, so you can compare from that time, instead of press down time, when you calculate how long you've held on top of the affected game object.

You use one game object with a collider as you touch catcher(A), and a different one as your "show stuff on screen" one(B). A has a script on it that can be accessed from wherever you activate B, so that you can set up a reference when it happens. B contains a time stamp from when it is shown on screen. When the OnPress is released on A, you check the time on B - currentTime and see if you should doStuff().

There are other ways to do it, but this seems.. ironically.. the most straight forward to me. :)

silversteez

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
    • MattSilverstein.com
Re: Button OnLongPress
« Reply #6 on: March 02, 2013, 03:16:57 PM »
Hmm...That actually seems like a really smart way to do it. Would take some overhauling of my current stuff to implement, but it might be worth it. i'll marinate on it for a bit...

Thanks for taking the time to help, Nicki! Verrrry much appreciated.