目前的UGUI在InputField里即使设置了Navigation也是不可以用键盘切换上下,所以自己写了个组件实现这个功能
前提条件是要在InputField里设置Navigation,也就是必须能找到上一个控件和下一个控件
然后将以下脚本挂在需要切换的InputField上,用Tab和Shift+Tab切换:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
using
UnityEngine
;
using
UnityEngine
.
UI
;
using
UnityEngine
.
EventSystems
;
public
class
InputNavigator
:
MonoBehaviour
,
ISelectHandler
,
IDeselectHandler
{
EventSystem
system
;
private
bool
_isSelect
=
false
;
void
Start
(
)
{
system
=
EventSystem
.
current
;
}
void
Update
(
)
{
if
(
Input
.
GetKeyDown
(
KeyCode
.
Tab
)
&&
_isSelect
)
{
Selectable
next
=
null
;
if
(
Input
.
GetKey
(
KeyCode
.
LeftShift
)
||
Input
.
GetKey
(
KeyCode
.
RightShift
)
)
{
next
=
system
.
currentSelectedGameObject
.
GetComponent
<
Selectable
>
(
)
.
FindSelectableOnUp
(
)
;
if
(
next
==
null
)
next
=
system
.
lastSelectedGameObject
.
GetComponent
<
Selectable
>
(
)
;
}
else
{
next
=
system
.
currentSelectedGameObject
.
GetComponent
<
Selectable
>
(
)
.
FindSelectableOnDown
(
)
;
if
(
next
==
null
)
next
=
system
.
firstSelectedGameObject
.
GetComponent
<
Selectable
>
(
)
;
}
if
(
next
!=
null
)
{
InputField
inputfield
=
next
.
GetComponent
<
InputField
>
(
)
;
system
.
SetSelectedGameObject
(
next
.
gameObject
,
new
BaseEventData
(
system
)
)
;
}
else
{
Debug
.
LogError
(
"找不到下一个控件"
)
;
}
}
}
public
void
OnSelect
(
BaseEventData
eventData
)
{
_isSelect
=
true
;
}
public
void
OnDeselect
(
BaseEventData
eventData
)
{
_isSelect
=
false
;
}
}
|