Welcome,
Guest
. Please
login
or
register
.
February 12, 2025, 05:21:23 AM
Home
Help
Search
Login
Register
Tasharen Entertainment Forum
»
Support
»
NGUI 3 Support
»
How to implement OnDown/LongPress?
« previous
next »
Print
Pages: [
1
]
Author
Topic: How to implement OnDown/LongPress? (Read 5841 times)
Shifty Geezer
Full Member
Thank You
-Given: 7
-Receive: 9
Posts: 226
How to implement OnDown/LongPress?
«
on:
January 18, 2014, 12:56:35 PM »
The behaviour I want is to fire an action based on x amount of time with the finger/mouse held down. I can create a script to read the length of a click when released just fine, for differentiating between clicks and presses based on time to release, but the NGUI events are only triggered on a change - there's no continuous feedback.
What's the best change (or even any, as I haven't found one solution yet!) I can make to add 'time_touch_is_down' to the event system. I can add time values to MouseOrTouch events but I can't figure out the notification system to send time notifications as long as a finger is down, nor can I figure out how to add a longPress parameterisation in NGUI, which seems heavily geared towards boolean events. Another approach I've tried is to get the current object's pressed time from my custom control script.
Logged
ArenMook
Administrator
Hero Member
Thank You
-Given: 337
-Receive: 1171
Posts: 22,128
Toronto, Canada
Re: How to implement OnDown/LongPress?
«
Reply #1 on:
January 18, 2014, 10:12:36 PM »
Use your own events, not NGUI. In OnPress(true), set some local flag. In OnPress(false), clear it. In Update(), check the flag -- if true, do whatever you need to do. Record the time in OnPress(true) if you need a delta, etc.
Logged
Shifty Geezer
Full Member
Thank You
-Given: 7
-Receive: 9
Posts: 226
Re: How to implement OnDown/LongPress?
«
Reply #2 on:
January 19, 2014, 09:45:46 AM »
Of course. Thanks.
Logged
Ugur
Jr. Member
Thank You
-Given: 0
-Receive: 1
Posts: 60
Re: How to implement OnDown/LongPress?
«
Reply #3 on:
January 19, 2014, 11:33:56 AM »
try this:
UIRepeatButton.cs :
using
UnityEngine
;
using
System.Collections
;
using
System.Collections.Generic
;
public
class
UIRepeatButton
:
MonoBehaviour
{
public
float
interval
=
0
.
0001525F
;
bool
mIsPressed
=
false
;
float
mNextClick
=
0f
;
public
List
<
EventDelegate
>
onPressListeners
=
new
List
<
EventDelegate
>
(
)
;
void
OnPress
(
bool
isPressed
)
{
mIsPressed
=
isPressed
;
mNextClick
=
Time
.
realtimeSinceStartup
+
interval
;
}
void
Update
(
)
{
if
(
mIsPressed
&&
Time
.
realtimeSinceStartup
>
mNextClick
)
{
mNextClick
=
Time
.
realtimeSinceStartup
+
interval
;
// Do what you need to do, or simply:
//SendMessage("OnClick", SendMessageOptions.DontRequireReceiver);
//cleaner:
EventDelegate
.
Execute
(
onPressListeners
)
;
}
}
}
UIRepeatButtonEditor.cs (put into Editor folder in project assets, allows to more nicely choose the event handler for the button..)
using
UnityEngine
;
using
UnityEditor
;
[
CanEditMultipleObjects
]
[
CustomEditor
(
typeof
(
UIRepeatButton
)
)
]
public
class
UIRepeatButtonEditor
:
UIWidgetContainerEditor
{
public
override
void
OnInspectorGUI
(
)
{
serializedObject
.
Update
(
)
;
UIRepeatButton button
=
target
as
UIRepeatButton
;
//this allows to more conveniently set the event handler:
NGUIEditorTools
.
DrawEvents
(
"On Repeated Press"
, button, button
.
onPressListeners
)
;
}
enum
Highlight
{
DoNothing,
Press,
}
}
«
Last Edit: January 21, 2014, 05:57:25 PM by Ugur
»
Logged
Print
Pages: [
1
]
« previous
next »
Tasharen Entertainment Forum
»
Support
»
NGUI 3 Support
»
How to implement OnDown/LongPress?