WX-Game1/Assets/Scripts/WXInputFieldAdapter.cs

146 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using UnityEngine;
// using WeChatWASM;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using DefaultNamespace;
using Random = UnityEngine.Random;
// 添加 InputField 组件的依赖
[RequireComponent(typeof(InputField))]
public class WXInputFieldAdapter : MonoBehaviour, IPointerClickHandler, IPointerExitHandler
{
private InputField _inputField;
private bool _isShowKeyboard = false;
public ChatPanelManager chatPanelManager;
private void Start()
{
_inputField = GetComponent<InputField>();
_inputField.onEndEdit.AddListener(OnInputEnd);
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("OnPointerClick");
// ShowKeyboard();
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("OnPointerExit");
if (!_inputField.isFocused)
{
// HideKeyboard();
}
}
// private void OnInput(OnKeyboardInputListenerResult v)
// {
// Debug.Log("onInput");
// Debug.Log(v.value);
// if (_inputField.isFocused)
// {
// _inputField.text = v.value;
// }
//
// }
/// <summary>
/// 按下回车键
/// </summary>
/// <param name="value"></param>
private void OnInputEnd(string value)
{
Debug.Log("回车键按下,输入内容:" + value);
if (value != "")
{
SendMessageMy(value);
// chatPanelManager.AddBubble(value, true);
_inputField.text = ""; // 清空_inputField内文字
}
}
// private void OnConfirm(OnKeyboardInputListenerResult v)
// {
// // 输入法confirm回调
// Debug.Log("onConfirm");
// Debug.Log(v.value);
// chatPanelManager.AddBubble(v.value,Random.Range(0,2)>0);
// HideKeyboard();
// }
//
//
//
// private void OnComplete(OnKeyboardInputListenerResult v)
// {
// // 输入法complete回调
// Debug.Log("OnComplete");
// Debug.Log(v.value);
//
// // chatPanelManager.AddBubble(v.value,Random.Range(0,2)>0);
//
// HideKeyboard();
// }
//
// private void ShowKeyboard()
// {
// if (_isShowKeyboard) return;
//
// WX.ShowKeyboard(new ShowKeyboardOption()
// {
// defaultValue = "",
// maxLength = 100,
// confirmType = "send"
// });
//
// //绑定回调
// WX.OnKeyboardConfirm(this.OnConfirm);
// WX.OnKeyboardComplete(this.OnComplete);
// WX.OnKeyboardInput(this.OnInput);
// _isShowKeyboard = true;
// }
//
// private void HideKeyboard()
// {
// if (!_isShowKeyboard) return;
//
// WX.HideKeyboard(new HideKeyboardOption());
// //删除掉相关事件监听
// WX.OffKeyboardInput(this.OnInput);
// WX.OffKeyboardConfirm(this.OnConfirm);
// WX.OffKeyboardComplete(this.OnComplete);
// _isShowKeyboard = false;
// }
/// <summary>
/// 发送消息
/// </summary>
/// <param name="value"></param>
async void SendMessageMy(string value)
{
try
{
Debug.Log("发送消息...");
// 使用UserDataNetworkManager获取用户信息
SendMessageRoot response = await UserDataNetworkManager.Instance.SendMessage(value);
// 根据code判断发送是否成功code为0表示成功
if (response != null && response.code == 0)
{
Debug.Log($"发送消息成功!响应信息: {response.data}");
}
else
{
Debug.LogError($"发送消息失败code: {response?.code}, msg: {response?.msg}");
}
}
catch (Exception ex)
{
Debug.LogError($"发送消息发生错误: {ex.Message}");
// 可以在这里添加错误处理逻辑,比如显示错误提示给用户
}
}
}