91 lines
3.0 KiB
C#
91 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System;
|
|
|
|
namespace Bansonic
|
|
{
|
|
/// <summary>
|
|
/// 全局通知系统入口
|
|
/// 使用方式:gNotice.message.display("内容");
|
|
/// </summary>
|
|
public static class gNotice
|
|
{
|
|
// 用于通知 UI 层
|
|
/// <summary>
|
|
/// 当任何通知被显示时触发
|
|
/// 参数1:类型前缀(如 [Info])
|
|
/// 参数2:消息内容
|
|
/// </summary>
|
|
public static event Action<string, string, float?, float?> OnNoticeDisplay;
|
|
|
|
/// <summary>
|
|
/// 当任何通知被中断显示时触发
|
|
/// </summary>
|
|
public static event Action OnNoticeInterrupt;
|
|
|
|
|
|
// --- 静态实例定义 (直接注入行为,无需枚举) ---
|
|
|
|
/// <summary>普通消息</summary>
|
|
public static readonly NoticeChannel message = new NoticeChannel("[Info]", Debug.Log);
|
|
|
|
/// <summary>建议</summary>
|
|
public static readonly NoticeChannel recommendation = new NoticeChannel("[Recommendation]", Debug.Log);
|
|
|
|
/// <summary>警告</summary>
|
|
public static readonly NoticeChannel warning = new NoticeChannel("[Warning]", Debug.LogWarning);
|
|
|
|
/// <summary>错误</summary>
|
|
public static readonly NoticeChannel error = new NoticeChannel("[Error]", Debug.LogError);
|
|
|
|
/// <summary>警报</summary>
|
|
public static readonly NoticeChannel alarm = new NoticeChannel("[Alarm]", Debug.LogWarning);
|
|
|
|
|
|
// --- 内部核心实现 ---
|
|
|
|
/// <summary>
|
|
/// 通知通道类:封装了通用的 display 和 interrupt 逻辑
|
|
/// </summary>
|
|
public class NoticeChannel
|
|
{
|
|
private readonly string _prefix;
|
|
private readonly Action<object> _logAction;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="prefix">日志前缀,如 [Info]</param>
|
|
/// <param name="logAction">具体的日志输出方法,如 Debug.Log</param>
|
|
internal NoticeChannel(string prefix, Action<object> logAction)
|
|
{
|
|
_prefix = prefix;
|
|
_logAction = logAction;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 显示消息
|
|
/// </summary>
|
|
public void display(object message, float? waitTime = null, float? fadeTotalTime = null)
|
|
{
|
|
string content = message?.ToString() ?? "null";
|
|
content = LocalizationService.LocalizeLiteral(content);
|
|
_logAction($"{_prefix} {content}");
|
|
|
|
// 触发 UI 事件
|
|
OnNoticeDisplay?.Invoke(_prefix, content, waitTime, fadeTotalTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 立即终止当前正在展示的全部警告prefab
|
|
/// </summary>
|
|
public void interrupt()
|
|
{
|
|
// 触发 UI 事件
|
|
OnNoticeInterrupt?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|