Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TMP_InputField Demo 添加 #926

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Demo/API_V2/Assets/API/APISO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ MonoBehaviour:
- {fileID: 11400000, guid: 9977a581037b84833a32b508e00eb1a2, type: 2}
- {fileID: 11400000, guid: 56f316e0e10ba419bbf19bd7a68bfc4c, type: 2}
- {fileID: 11400000, guid: 6f0972f5fdc56c543b23c9873d760bf5, type: 2}
- {fileID: 11400000, guid: a461b8cd70d9e4e23ad1cc953bec31e9, type: 2}
- {fileID: 11400000, guid: 7ef06699cee7846b7823e4cc421418eb, type: 2}
- {fileID: 11400000, guid: a461b8cd70d9e4e23ad1cc953bec31e9, type: 2}
- {fileID: 11400000, guid: 55de20d536f8c4689bbd80553d87fe46, type: 2}
- {fileID: 11400000, guid: f2c56d751bb7c4c398db7c1db352517d, type: 2}
- {fileID: 11400000, guid: b4a6196f623dd4435a4f3bd70af92d06, type: 2}
Expand Down
4 changes: 2 additions & 2 deletions Demo/API_V2/Assets/API/Facility/FacilitySO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ MonoBehaviour:
categoryName: "\u8BBE\u5907"
categorySprite: {fileID: 21300000, guid: 53dd2b1922e5142ec8a53037f362fc56, type: 3}
entryList:
- {fileID: 11400000, guid: bbbc9b983d6d34ad3bac2921509f612f, type: 2}
- {fileID: 11400000, guid: c17ae0ce755f04419b700825b20fd386, type: 2}
- {fileID: 11400000, guid: a24af43ae51914d26b0bf637c283dab2, type: 2}
- {fileID: 11400000, guid: 4eba2c9f2f2064e4081be74302ca4c33, type: 2}
- {fileID: 11400000, guid: bbbc9b983d6d34ad3bac2921509f612f, type: 2}
- {fileID: 11400000, guid: c17ae0ce755f04419b700825b20fd386, type: 2}
- {fileID: 11400000, guid: ac09abff5d8bc48bbabd08bde820b58b, type: 2}
- {fileID: 11400000, guid: 3bf339b994c544db3860ddf0a0dc8e20, type: 2}
- {fileID: 11400000, guid: e92d94924be504167a342e42e1162f21, type: 2}
Expand Down
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Facility/Screen/ScreenSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ MonoBehaviour:
- buttonText: "\u67E5\u8BE2\u7528\u6237\u662F\u5426\u5728\u5F55\u5C4F"
- buttonText: "\u83B7\u53D6\u5C4F\u5E55\u4EAE\u5EA6"
initialResultList: []
entryOrder: 0
2 changes: 1 addition & 1 deletion Demo/API_V2/Assets/API/FileSystem/FileSystemSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ MonoBehaviour:
- {fileID: 11400000, guid: 65ab1b01a722b4542a14fcf9decca3de, type: 2}
- {fileID: 11400000, guid: 19b80cb144f0c4ea28a4ddca7a2c2f09, type: 2}
- {fileID: 11400000, guid: 0aa5d76237c844275add1612c47bddaa, type: 2}
- {fileID: 11400000, guid: 73c05fc1ffae94ec1b69a00ee261d529, type: 2}
- {fileID: 11400000, guid: 65a826f5f949544d8990e673231bb80f, type: 2}
- {fileID: 11400000, guid: 73c05fc1ffae94ec1b69a00ee261d529, type: 2}
- {fileID: 11400000, guid: 3a901e80b59ad4aafbe373ee6a8df2d8, type: 2}
- {fileID: 11400000, guid: e7ea0be62dc6543b0a8dc629489c5e7e, type: 2}
categoryOrder: 11
42 changes: 42 additions & 0 deletions Demo/API_V2/Assets/API/InputField/TmpTextInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using UnityEngine;
using TMPro;

// 添加 Text 组件的依赖
[RequireComponent(typeof(TMP_Text))]
public class TMPTextInit : MonoBehaviour
{
private TMP_Text _text;


private void Awake()
{
// 获取 Text 组件
_text = GetComponent<TMP_Text>();
}

private void Start()
{
// 如果 GameManager 的字体已经加载,直接设置 Text 的字体
if (GameManager.Instance.font != null)
{
_text.font = GameManager.Instance.fonts;
}
else
{
// 如果字体还未加载,添加字体加载事件监听器
GameManager.Instance.OnTMPFontLoaded += OnFontLoaded;
}
}

private void OnDestroy()
{
// 移除字体加载事件监听器
GameManager.Instance.OnTMPFontLoaded -= OnFontLoaded;
}

// 当字体加载完成时,设置 Text 的字体
private void OnFontLoaded(TMP_FontAsset fonts)
{
_text.font = fonts;
}
}
11 changes: 11 additions & 0 deletions Demo/API_V2/Assets/API/InputField/TmpTextInit.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions Demo/API_V2/Assets/API/InputField/WXInputFieldTmpAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using UnityEngine;
using WeChatWASM;
using TMPro;
using UnityEngine.EventSystems;

// 要求该组件必须附加 TMP_InputField 组件
[RequireComponent(typeof(TMP_InputField))]
public class WXInputFieldTmpAdapter : MonoBehaviour, IPointerClickHandler, IPointerExitHandler
{
private TMP_InputField _inputField; // 存储 TMP_InputField 组件的引用
private bool _isShowKeyboard = false; // 标记键盘是否显示

private void Start()
{
// 获取挂载在同一游戏对象上的 TMP_InputField 组件
_inputField = GetComponent<TMP_InputField>();
}

// 当指针点击该组件时调用
public void OnPointerClick(PointerEventData eventData)
{
ShowKeyboard(); // 显示键盘
}

// 当指针离开该组件时调用
public void OnPointerExit(PointerEventData eventData)
{
// 如果 TMP_InputField 没有被聚焦,则隐藏键盘
if (!_inputField.isFocused)
{
HideKeyboard();
}
}

// 输入法输入回调
private void OnInput(OnKeyboardInputListenerResult v)
{
// 如果 TMP_InputField 被聚焦,则将输入值赋给 TMP_InputField
if (_inputField.isFocused)
{
_inputField.text = v.value;
}
}

// 输入法确认回调
private void OnConfirm(OnKeyboardInputListenerResult v)
{
HideKeyboard(); // 隐藏键盘
}

// 输入法完成回调
private void OnComplete(OnKeyboardInputListenerResult v)
{
HideKeyboard(); // 隐藏键盘
}

// 显示键盘的方法
private void ShowKeyboard()
{
// 如果键盘已经显示,则直接返回
if (_isShowKeyboard) return;

// 调用 WeChat API 显示键盘
WX.ShowKeyboard(new ShowKeyboardOption()
{
defaultValue = _inputField.text,//传入当前文本作为默认值
maxLength = 20, // 最大输入长度
confirmType = "go" // 确认按钮类型
});

// 绑定键盘事件回调
WX.OnKeyboardConfirm(this.OnConfirm);
WX.OnKeyboardComplete(this.OnComplete);
WX.OnKeyboardInput(this.OnInput);
_isShowKeyboard = true; // 更新键盘显示状态
}

// 隐藏键盘的方法
private void HideKeyboard()
{
// 如果键盘未显示,则直接返回
if (!_isShowKeyboard) return;

// 调用 WeChat API 隐藏键盘
WX.HideKeyboard(new HideKeyboardOption());
// 移除事件监听
WX.OffKeyboardInput(this.OnInput);
WX.OffKeyboardConfirm(this.OnConfirm);
WX.OffKeyboardComplete(this.OnComplete);
_isShowKeyboard = false; // 更新键盘显示状态
}
}
11 changes: 11 additions & 0 deletions Demo/API_V2/Assets/API/InputField/WXInputFieldTmpAdapter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ MonoBehaviour:
- buttonText: "\u8DF3\u8F6C"
- buttonText: "\u83B7\u5F97\u4E0B\u4E00\u5E27"
initialResultList: []
entryOrder: 0
Binary file not shown.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ MonoBehaviour:
initialButtonText: "\u4E8B\u4EF6\u4E0A\u62A5"
extraButtonList: []
initialResultList: []
entryOrder: 0
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Share/ShareEvent/ShareEventSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ MonoBehaviour:
- buttonText: "\u76D1\u542Csharetofriend"
- buttonText: "\u76D1\u542C\u53F3\u4E0A\u89D2\u83DC\u5355\u7684\u8F6C\u53D1"
initialResultList: []
entryOrder: 0
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ MonoBehaviour:
- buttonText: "\u83B7\u53D6\u8F6C\u53D1\u8BE6\u7EC6\u4FE1\u606F"
- buttonText: "\u9A8C\u8BC1\u79C1\u5BC6\u6D88\u606F"
initialResultList: []
entryOrder: 0
Loading
Loading