1
+ using UnityEngine ;
2
+ using WeChatWASM ;
3
+ using UnityEngine . UI ;
4
+ using UnityEngine . EventSystems ;
5
+
6
+ // 添加 InputField 组件的依赖
7
+ [ RequireComponent ( typeof ( InputField ) ) ]
8
+ public class WXInputFieldAdapter : MonoBehaviour , IPointerClickHandler , IPointerExitHandler
9
+ {
10
+ private InputField _inputField ;
11
+ private bool _isShowKeyboard = false ;
12
+
13
+ private void Start ( )
14
+ {
15
+ _inputField = GetComponent < InputField > ( ) ;
16
+ }
17
+
18
+ public void OnPointerClick ( PointerEventData eventData )
19
+ {
20
+ Debug . Log ( "OnPointerClick" ) ;
21
+ ShowKeyboard ( ) ;
22
+ }
23
+
24
+ public void OnPointerExit ( PointerEventData eventData )
25
+ {
26
+ Debug . Log ( "OnPointerExit" ) ;
27
+ if ( ! _inputField . isFocused )
28
+ {
29
+ HideKeyboard ( ) ;
30
+ }
31
+ }
32
+
33
+ private void OnInput ( OnKeyboardInputListenerResult v )
34
+ {
35
+ Debug . Log ( "onInput" ) ;
36
+ Debug . Log ( v . value ) ;
37
+ if ( _inputField . isFocused )
38
+ {
39
+ _inputField . text = v . value ;
40
+ }
41
+ }
42
+
43
+ private void OnConfirm ( OnKeyboardInputListenerResult v )
44
+ {
45
+ // 输入法confirm回调
46
+ Debug . Log ( "onConfirm" ) ;
47
+ Debug . Log ( v . value ) ;
48
+ HideKeyboard ( ) ;
49
+ }
50
+
51
+ private void OnComplete ( OnKeyboardInputListenerResult v )
52
+ {
53
+ // 输入法complete回调
54
+ Debug . Log ( "OnComplete" ) ;
55
+ Debug . Log ( v . value ) ;
56
+ HideKeyboard ( ) ;
57
+ }
58
+
59
+ private void ShowKeyboard ( )
60
+ {
61
+ if ( _isShowKeyboard ) return ;
62
+
63
+ WX . ShowKeyboard ( new ShowKeyboardOption ( )
64
+ {
65
+ defaultValue = "xxx" ,
66
+ maxLength = 20 ,
67
+ confirmType = "go"
68
+ } ) ;
69
+
70
+ //绑定回调
71
+ WX . OnKeyboardConfirm ( this . OnConfirm ) ;
72
+ WX . OnKeyboardComplete ( this . OnComplete ) ;
73
+ WX . OnKeyboardInput ( this . OnInput ) ;
74
+ _isShowKeyboard = true ;
75
+ }
76
+
77
+ private void HideKeyboard ( )
78
+ {
79
+ if ( ! _isShowKeyboard ) return ;
80
+
81
+ WX . HideKeyboard ( new HideKeyboardOption ( ) ) ;
82
+ //删除掉相关事件监听
83
+ WX . OffKeyboardInput ( this . OnInput ) ;
84
+ WX . OffKeyboardConfirm ( this . OnConfirm ) ;
85
+ WX . OffKeyboardComplete ( this . OnComplete ) ;
86
+ _isShowKeyboard = false ;
87
+ }
88
+ }
0 commit comments