chicken_dy/Assets/HCMiniSdk/Scripts/Other/HCInput.cs

87 lines
2.1 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
#if WEBGL_WX && !UNITY_EDITOR
using WeChatWASM;
#endif
namespace HCMiniSdk.Scripts.Other
{
public class HCInput : MonoBehaviour, IPointerClickHandler, IPointerExitHandler
{
private InputField input;
private bool isShowKeyboard = false;
private void Start()
{
input = gameObject.GetComponent<InputField>();
}
public void OnPointerClick(PointerEventData eventData)
{
#if WEBGL_WX && !UNITY_EDITOR
ShowKeyboard();
#endif
}
public void OnPointerExit(PointerEventData eventData)
{
#if WEBGL_WX && !UNITY_EDITOR
if (!input.isFocused)
{
HideKeyboard();
}
#endif
}
#if WEBGL_WX && !UNITY_EDITOR
public void OnInput(OnKeyboardInputListenerResult v)
{
if (input.isFocused)
{
input.text = v.value;
}
}
public void OnConfirm(OnKeyboardInputListenerResult v)
{
HideKeyboard();
}
public void OnComplete(OnKeyboardInputListenerResult v)
{
input.text = v.value;
HideKeyboard();
}
private void ShowKeyboard()
{
if (isShowKeyboard) return;
WX.ShowKeyboard(new ShowKeyboardOption()
{
defaultValue = $"{input.text}",
maxLength = 1024,
confirmType = "go"
});
//绑定回调
WX.OnKeyboardConfirm(OnConfirm);
WX.OnKeyboardComplete(OnComplete);
WX.OnKeyboardInput(OnInput);
isShowKeyboard = true;
}
private void HideKeyboard()
{
if (!isShowKeyboard) return;
WX.HideKeyboard(new HideKeyboardOption());
//删除掉相关事件监听
WX.OffKeyboardInput(OnInput);
WX.OffKeyboardConfirm(OnConfirm);
WX.OffKeyboardComplete(OnComplete);
isShowKeyboard = false;
}
#endif
}
}