Welcome,
Guest
. Please
login
or
register
.
April 19, 2026, 10:40:39 PM
Home
Help
Search
Login
Register
Tasharen Entertainment Forum
»
Support
»
NGUI 3 Support
»
UICenterOnChild feature request
« previous
next »
Print
Pages: [
1
]
Author
Topic: UICenterOnChild feature request (Read 7578 times)
Markov
Newbie
Thank You
-Given: 0
-Receive: 0
Posts: 27
UICenterOnChild feature request
«
on:
December 06, 2013, 08:26:00 AM »
Hi!
I'm using UICenterOnChild to swipe between map lists in our game. This objects are big and if I want to see the next list I have to slide too much range (red arrow on picture). This is very invonvinient on tablets and PC.
May I ask you to add functionality in UICenterOnChild to set this range?(like on blue arrow)
«
Last Edit: December 06, 2013, 08:38:49 AM by Markov
»
Logged
UncleAcid
Jr. Member
Thank You
-Given: 0
-Receive: 0
Posts: 51
Re: UICenterOnChild feature request
«
Reply #1 on:
December 06, 2013, 12:40:47 PM »
Add the following variables to UICenterOnChild:
public
bool
allowFlickToNext
=
false
;
public
float
flickThreshold
=
50f
;
// 50f has seemed to work well in my cases
and then replace the Recenter function with the following:
public
void
Recenter
(
)
{
if
(
mDrag
==
null
)
{
mDrag
=
NGUITools
.
FindInParents
<
UIScrollView
>
(
gameObject
)
;
if
(
mDrag
==
null
)
{
Debug
.
LogWarning
(
GetType
(
)
+
" requires "
+
typeof
(
UIScrollView
)
+
" on a parent object in order to work"
,
this
)
;
enabled
=
false
;
return
;
}
else
{
mDrag
.
onDragFinished
=
OnDragFinished
;
if
(
mDrag
.
horizontalScrollBar
!=
null
)
mDrag
.
horizontalScrollBar
.
onDragFinished
=
OnDragFinished
;
if
(
mDrag
.
verticalScrollBar
!=
null
)
mDrag
.
verticalScrollBar
.
onDragFinished
=
OnDragFinished
;
}
}
if
(
mDrag
.
panel
==
null
)
return
;
// Calculate the panel's center in world coordinates
Vector3
[
]
corners
=
mDrag
.
panel
.
worldCorners
;
Vector3 panelCenter
=
(
corners
[
2
]
+
corners
[
0
]
)
*
0
.
5f
;
// Offset this value by the momentum
Vector3 pickingPoint
=
panelCenter
-
mDrag
.
currentMomentum
*
(
mDrag
.
momentumAmount
*
0
.
1f
)
;
mDrag
.
currentMomentum
=
Vector3
.
zero
;
float
min
=
float
.
MaxValue
;
Transform closest
=
null
;
Transform trans
=
transform
;
int
index
=
0
;
// Determine the closest child
for
(
int
i
=
0
, imax
=
trans
.
childCount
;
i
<
imax
;
++
i
)
{
Transform t
=
trans
.
GetChild
(
i
)
;
float
sqrDist
=
Vector3
.
SqrMagnitude
(
t
.
position
-
pickingPoint
)
;
if
(
sqrDist
<
min
)
{
min
=
sqrDist
;
closest
=
t
;
index
=
i
;
}
}
if
(
allowFlickToNext
&&
UICamera
.
currentTouch
!=
null
)
{
if
(
UICamera
.
currentTouch
.
totalDelta
.
x
>
flickThreshold
&&
centeredObject
.
transform
==
trans
.
GetChild
(
index
)
)
{
if
(
index
-
1
>=
0
)
{
closest
=
trans
.
GetChild
(
index
-
1
)
;
}
}
else
if
(
UICamera
.
currentTouch
.
totalDelta
.
x
<
-
flickThreshold
&&
centeredObject
.
transform
==
trans
.
GetChild
(
index
)
)
{
if
(
index
+
1
<
trans
.
childCount
)
{
closest
=
trans
.
GetChild
(
index
+
1
)
;
}
}
}
CenterOn
(
closest, panelCenter
)
;
}
All these changes do is check to see if you "flicked" the panel in either direction but didn't swipe (swiping far enough to center on a different child will result in default behaviour). If you "flicked"; the closest child will be the current child and so it sets the closest to the next or previous depending on flick direction.
Logged
Markov
Newbie
Thank You
-Given: 0
-Receive: 0
Posts: 27
Re: UICenterOnChild feature request
«
Reply #2 on:
December 06, 2013, 01:38:26 PM »
Thank you UncleAcid!
I hope this changes will be in the next update too.
Logged
ArenMook
Administrator
Hero Member
Thank You
-Given: 337
-Receive: 1171
Posts: 22,128
Toronto, Canada
Re: UICenterOnChild feature request
«
Reply #3 on:
December 07, 2013, 12:25:16 AM »
Thanks, UncleAcid. I've used your code as a guide to implement a similar solution. 3.0.7 will have a 'next page threshold' value you can set that works same as your flick threshold. If something other than zero, it will behave like your solution.
Logged
UncleAcid
Jr. Member
Thank You
-Given: 0
-Receive: 0
Posts: 51
Re: UICenterOnChild feature request
«
Reply #4 on:
December 09, 2013, 10:40:58 AM »
Cool.
Glad I could contribute, no matter how small
Logged
Print
Pages: [
1
]
« previous
next »
Tasharen Entertainment Forum
»
Support
»
NGUI 3 Support
»
UICenterOnChild feature request