Skip to content

Commit f327128

Browse files
authored
Merge pull request #926 from VincentWSZ/TMP_InputField
TMP_InputField Demo 添加
2 parents 00372e2 + 6cfc713 commit f327128

File tree

83 files changed

+14935
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+14935
-83
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using UnityEngine;
2+
using TMPro;
3+
4+
// 添加 Text 组件的依赖
5+
[RequireComponent(typeof(TMP_Text))]
6+
public class TMPTextInit : MonoBehaviour
7+
{
8+
private TMP_Text _text;
9+
10+
11+
private void Awake()
12+
{
13+
// 获取 Text 组件
14+
_text = GetComponent<TMP_Text>();
15+
}
16+
17+
private void Start()
18+
{
19+
// 如果 GameManager 的字体已经加载,直接设置 Text 的字体
20+
if (GameManager.Instance.font != null)
21+
{
22+
_text.font = GameManager.Instance.fonts;
23+
}
24+
else
25+
{
26+
// 如果字体还未加载,添加字体加载事件监听器
27+
GameManager.Instance.OnTMPFontLoaded += OnFontLoaded;
28+
}
29+
}
30+
31+
private void OnDestroy()
32+
{
33+
// 移除字体加载事件监听器
34+
GameManager.Instance.OnTMPFontLoaded -= OnFontLoaded;
35+
}
36+
37+
// 当字体加载完成时,设置 Text 的字体
38+
private void OnFontLoaded(TMP_FontAsset fonts)
39+
{
40+
_text.font = fonts;
41+
}
42+
}

Demo/API_V2/Assets/API/InputField/TmpTextInit.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using UnityEngine;
2+
using WeChatWASM;
3+
using TMPro;
4+
using UnityEngine.EventSystems;
5+
6+
// 要求该组件必须附加 TMP_InputField 组件
7+
[RequireComponent(typeof(TMP_InputField))]
8+
public class WXInputFieldTmpAdapter : MonoBehaviour, IPointerClickHandler, IPointerExitHandler
9+
{
10+
private TMP_InputField _inputField; // 存储 TMP_InputField 组件的引用
11+
private bool _isShowKeyboard = false; // 标记键盘是否显示
12+
13+
private void Start()
14+
{
15+
// 获取挂载在同一游戏对象上的 TMP_InputField 组件
16+
_inputField = GetComponent<TMP_InputField>();
17+
}
18+
19+
// 当指针点击该组件时调用
20+
public void OnPointerClick(PointerEventData eventData)
21+
{
22+
ShowKeyboard(); // 显示键盘
23+
}
24+
25+
// 当指针离开该组件时调用
26+
public void OnPointerExit(PointerEventData eventData)
27+
{
28+
// 如果 TMP_InputField 没有被聚焦,则隐藏键盘
29+
if (!_inputField.isFocused)
30+
{
31+
HideKeyboard();
32+
}
33+
}
34+
35+
// 输入法输入回调
36+
private void OnInput(OnKeyboardInputListenerResult v)
37+
{
38+
// 如果 TMP_InputField 被聚焦,则将输入值赋给 TMP_InputField
39+
if (_inputField.isFocused)
40+
{
41+
_inputField.text = v.value;
42+
}
43+
}
44+
45+
// 输入法确认回调
46+
private void OnConfirm(OnKeyboardInputListenerResult v)
47+
{
48+
HideKeyboard(); // 隐藏键盘
49+
}
50+
51+
// 输入法完成回调
52+
private void OnComplete(OnKeyboardInputListenerResult v)
53+
{
54+
HideKeyboard(); // 隐藏键盘
55+
}
56+
57+
// 显示键盘的方法
58+
private void ShowKeyboard()
59+
{
60+
// 如果键盘已经显示,则直接返回
61+
if (_isShowKeyboard) return;
62+
63+
// 调用 WeChat API 显示键盘
64+
WX.ShowKeyboard(new ShowKeyboardOption()
65+
{
66+
defaultValue = _inputField.text,//传入当前文本作为默认值
67+
maxLength = 20, // 最大输入长度
68+
confirmType = "go" // 确认按钮类型
69+
});
70+
71+
// 绑定键盘事件回调
72+
WX.OnKeyboardConfirm(this.OnConfirm);
73+
WX.OnKeyboardComplete(this.OnComplete);
74+
WX.OnKeyboardInput(this.OnInput);
75+
_isShowKeyboard = true; // 更新键盘显示状态
76+
}
77+
78+
// 隐藏键盘的方法
79+
private void HideKeyboard()
80+
{
81+
// 如果键盘未显示,则直接返回
82+
if (!_isShowKeyboard) return;
83+
84+
// 调用 WeChat API 隐藏键盘
85+
WX.HideKeyboard(new HideKeyboardOption());
86+
// 移除事件监听
87+
WX.OffKeyboardInput(this.OnInput);
88+
WX.OffKeyboardConfirm(this.OnConfirm);
89+
WX.OffKeyboardComplete(this.OnComplete);
90+
_isShowKeyboard = false; // 更新键盘显示状态
91+
}
92+
}

Demo/API_V2/Assets/API/InputField/WXInputFieldTmpAdapter.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Demo/API_V2/Assets/API/Render/SetFont/TencentSans-W7.subset.ttf.meta

-21
This file was deleted.

0 commit comments

Comments
 (0)