/// #pkgName FGUI包名 /// #panelName UIPanel名字 /// #UIName = $"{#pkgName}{#panelName}" UIKey名字 /// 该脚本由模板创建 /// created by cb 2024 using FairyGUI; using FL.Data; using System; using System.Collections.Generic; using System.Reflection; using XGame.Framework.Network; using XGame.Framework.UI; namespace FL.FGUI { public struct GmToolFieldData { public string filedName; // 字段名称 public Action changeValueCallback; // 回调函数 } public struct GmFieldParam { public string fieldName; public string fieldValue; } /// /// UI逻辑处理类 /// /// public partial class GmToolSendMsgPanelCtrl : UIController { private IMsgRequest _queryMsg; // 要查询的消息 private List _fieldDataList = new List(); protected override void OnEnable(object intent) { AddUIListenres(); InitUI(); } protected override void OnDisable() { RemoveUIListenres(); if (_fieldDataList?.Count > 0) { _fieldDataList.Clear(); } _queryMsg = null; } #region UI事件 private void AddUIListenres() { VM.QueryBtn.onClick.Add(OnClickQueryBtn); VM.SendBtn.onClick.Add(OnClickSendBtn); VM.MaskBtn.onClick.Add(OnClickMaskBtn); } private void RemoveUIListenres() { VM.QueryBtn.onClick.Remove(OnClickQueryBtn); VM.SendBtn.onClick.Remove(OnClickSendBtn); VM.MaskBtn.onClick.Remove(OnClickMaskBtn); } private void OnClickMaskBtn(EventContext context) { Context.ClosePanel(); } private void OnClickQueryBtn(EventContext context) { if (string.IsNullOrEmpty(VM.MsgIdInput.text)) { Context.ShowTips("请先输入消息ID"); return; } ShowQueryMsgUI(); } private void OnClickSendBtn(EventContext context) { SendGmMsg(); } #endregion private void InitUI() { VM.MsgIdInput.text = string.Empty; VM.FieldNameList.BindDatas(_fieldDataList); } private void ResetField() { if (_fieldDataList?.Count > 0) { _fieldDataList.Clear(); } VM.FieldNameList.BindDatas(_fieldDataList); } private int GetMsgId() { return int.Parse(VM.MsgIdInput.text); } /// /// 查询消息相关字段信息 /// private void ShowQueryMsgUI() { ResetField(); _queryMsg = GmToolsService.Instance.QueryMessage(GetMsgId()); if (_queryMsg == null) { Context.ShowTips("请先输入正确的消息ID"); return; } var fields = _queryMsg.GetType().GetFields(); foreach (var item in fields) { _fieldDataList.Add(new GmToolFieldData() { filedName = item.Name, changeValueCallback = OnChangeFiledValue, }); } foreach (var item in _fieldDataList) { XGame.Log.Info($"字段名:{item}"); } VM.FieldNameList.BindDatas(_fieldDataList); } /// /// 修改字段数据监听 /// /// /// private void OnChangeFiledValue(GmFieldParam fieldData) { //使用默认值来判断结构体是否被初始化 if (!fieldData.Equals(default(GmFieldParam))) { string fieldName = fieldData.fieldName; if (!string.IsNullOrEmpty(fieldName)) { // 获取字段的信息 FieldInfo fieldInfo = _queryMsg.GetType().GetField(fieldName); XGame.Log.Debug($"字段:{fieldName}的值类型:{fieldInfo.FieldType}"); Type ValueType = fieldInfo.FieldType; if (ValueType == typeof(int)) { fieldInfo.SetValue(_queryMsg, int.Parse(fieldData.fieldValue)); } else if (ValueType == typeof(string)) { fieldInfo.SetValue(_queryMsg, fieldData.fieldValue); } else if (ValueType == typeof(bool)) { fieldInfo.SetValue(_queryMsg, bool.Parse(fieldData.fieldValue)); } else if (ValueType == typeof(int[]) || ValueType == typeof(string[])) { string[] arr = fieldData.fieldValue.Split(","); if (ValueType == typeof(int[])) { var valArray = new int[arr.Length]; for (int i = 0; i < arr.Length; i++) { valArray[i] = string.IsNullOrEmpty(arr[i]) ? 0 : int.Parse(arr[i]); } fieldInfo.SetValue(_queryMsg, valArray); } else { fieldInfo.SetValue(_queryMsg, arr); } } } } } /// /// 发送修改数据的消息 /// private void SendGmMsg() { GmToolsService.Instance.SendMessage(_queryMsg); Context.ClosePanel(); } } }