mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-20 05:17:13 +08:00
74 lines
3.1 KiB
C#
74 lines
3.1 KiB
C#
|
|
using System;
|
|||
|
|
using Base;
|
|||
|
|
using Newtonsoft.Json;
|
|||
|
|
|
|||
|
|
namespace EventWorkClass
|
|||
|
|
{
|
|||
|
|
public class Event_OpenUI : EventWorkClassBase
|
|||
|
|
{
|
|||
|
|
// 新增:私有数据类,用于JSON反序列化
|
|||
|
|
private class OpenUIEventData
|
|||
|
|
{
|
|||
|
|
public string uiName { get; set; } // 对应JSON中的 "uiName" 字段
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 新增:私有字段,用于存储解析出的UI名称
|
|||
|
|
private string _uiName;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 初始化事件,解析JSON字符串获取UI名称。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="value">包含UI名称的JSON字符串,例如: {"uiName": "MainMenuUI"}</param>
|
|||
|
|
/// <exception cref="System.ArgumentException">当value为null或空时抛出。</exception>
|
|||
|
|
/// <exception cref="System.FormatException">当JSON格式无效或缺少'uiName'属性时抛出。</exception>
|
|||
|
|
/// <exception cref="System.InvalidOperationException">当发生其他意外错误时抛出。</exception>
|
|||
|
|
public override void Init(string value)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(value))
|
|||
|
|
{
|
|||
|
|
throw new System.ArgumentException(
|
|||
|
|
"Event_OpenUI Init: value cannot be null or empty. It must contain the UI configuration JSON.",
|
|||
|
|
nameof(value));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 反序列化JSON字符串为OpenUIEventData对象
|
|||
|
|
OpenUIEventData eventData = JsonConvert.DeserializeObject<OpenUIEventData>(value);
|
|||
|
|
// 检查反序列化结果是否有效
|
|||
|
|
if (eventData == null || string.IsNullOrEmpty(eventData.uiName))
|
|||
|
|
{
|
|||
|
|
throw new System.FormatException(
|
|||
|
|
$"Event_OpenUI Init: Invalid JSON format or missing 'uiName' property in value: '{value}'");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 存储UI名称
|
|||
|
|
_uiName = eventData.uiName;
|
|||
|
|
}
|
|||
|
|
catch (Newtonsoft.Json.JsonSerializationException ex)
|
|||
|
|
{
|
|||
|
|
// JSON反序列化失败的特定处理
|
|||
|
|
throw new System.FormatException(
|
|||
|
|
$"Event_OpenUI Init: Failed to deserialize JSON for value: '{value}'. Please ensure it is a valid JSON string with a 'uiName' property. Error: {ex.Message}",
|
|||
|
|
ex);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
// 捕获其他所有潜在异常,提供更通用的错误信息
|
|||
|
|
throw new System.InvalidOperationException(
|
|||
|
|
$"Event_OpenUI Init: An unexpected error occurred during initialization for value: '{value}'. Error: {ex.Message}",
|
|||
|
|
ex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 运行事件,打开UI界面。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="dimensionID">维度ID (当前未被使用,但保留接口)。</param>
|
|||
|
|
/// <param name="initiator">触发此事件的实体 (当前未被使用,但保留接口)。</param>
|
|||
|
|
public override void Run(string dimensionID, Entity.Entity initiator = null)
|
|||
|
|
{
|
|||
|
|
UIInputControl.Instance.Show(_uiName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|