UI Toolbox 徽标UI Toolbox
支持

本页为译文。技术支持和视频教程主要使用英语,内容以英文页面为准。 英文版

通知 (NotificationArea)

NotificationArea 填满其父级,并在一个角落堆叠提示条气泡。

NotificationArea 中的三个提示条:信息、成功和警告,各带关闭按钮和剩余时间条NotificationArea 中的三个提示条:信息、成功和警告,各带关闭按钮和剩余时间条

视频教程:Notifications。所有视频:视频教程。

每个工作区各有一个,在首次使用时创建:

workspace.Notifications.Show("Layout saved", Severity.Success);

var bubble =
    workspace.Notifications.Show("Connection lost", Severity.Error,
        durationMs: 0);
bubble.Title = "Network";
bubble.AddAction("Retry", Reconnect);
bubble.Clicked += _ => OpenLog();

在工作区之外,请自己添加一个区域:

<UIToolbox.NotificationArea
    name="toasts"
    corner="BottomRight"
    max-visible="3"
    stack-duplicates="true" />

在 UI Builder 中,该区域会显示带其名称的虚线轮廓,方便你找到它。轮廓在 Play 模式和构建中不显示。

成员 说明
NotificationArea.Show(message, severity, durationMs) 添加一个气泡并返回它。消息是富文本。
NotificationArea.Show(bubble) 添加你构建的气泡。
NotificationArea.ShowBanner(title, message, severity, durationMs) 添加一个 Banner 气泡(大标题加较小的一行,2500 ms)并返回它。参见下面的"显示横幅"。
NotificationArea.ShowTask(task, loading, success, error) 为一个 Task 添加加载气泡并返回它。参见下面的"显示任务"。
NotificationArea.Corner (corner) TopRight(默认)、TopLeft、BottomRight、BottomLeft、TopCenter、BottomCenter 或 Center(父级的中间,用于横幅)。
NotificationArea.MaxVisible (max-visible) 同时显示的最大气泡数(5)。超过时,新气泡在队列中等待。
NotificationArea.Queue (queue) 把超过 MaxVisible 的气泡保留在队列中(true)。等待的气泡在已显示的气泡离开时显示,它的计时器此时才开始。false 则关闭最旧的气泡来腾出空间。
NotificationArea.StackDuplicates (stack-duplicates) 把相同的消息显示为一个带计数的气泡(false)。Show 会给消息、标题和严重程度相同的气泡增加 Count,重新开始其倒计时,并返回该气泡。
NotificationArea.ShowProgress (show-progress) 在每个气泡上显示倒计时条(true)。
NotificationArea.SlideBubbles (slide-bubbles) 气泡到来、离开或改变高度时,其他气泡滑动到新位置(true)。false 则让它们直接跳过去。
NotificationArea.EnterAnimation (enter-animation)、ExitAnimation (exit-animation) 气泡的进入和退出方式:Preset(默认,气泡的 Preset 的外观)、SlideFromEdge、Fade、Zoom 或 Rise。参见下面的"改变气泡的进入和退出方式"。
NotificationArea.EnterEasing (enter-easing)、ExitEasing (exit-easing) USS 缓动名称(NotificationEasing):Default(主题的缓动)、Linear、Ease、EaseInOutBack 等。
NotificationArea.EnterDurationMs (enter-duration)、ExitDurationMs (exit-duration) 进入或退出的毫秒数。0(默认)沿用预设的时长。
NotificationArea.EnterCurve (enter-curve)、ExitCurve (exit-curve) 一条从 0 到 1 的 AnimationCurve,代替 USS 过渡来驱动进入或退出。超过 1 的曲线会冲过终点。null(默认)使用 USS 过渡。
NotificationArea.QueuedCount 在队列中等待的气泡数。
NotificationArea.DismissAll() 关闭每个气泡,队列中的气泡也包括在内。
NotificationBubble.Title (title) 消息上方的粗体行。为空(默认)则不显示这一行。
NotificationBubble.Description (description) 消息(富文本)。
NotificationBubble.Icon 文本前面的图片,类型为 Background(纹理、精灵或矢量图)。null(默认)不显示图片。
NotificationBubble.IconAnimation (icon-animation)、IconAnimationClip (icon-animation-clip) 气泡显示期间在图标中播放的 SpriteSheet,例如拾取时旋转的金币,以及所播放的剪辑。它覆盖 Icon。无(默认)则保持静态图标。
NotificationBubble.Severity (severity) Info(默认)、Success、Warning 或 Error。
NotificationBubble.Preset (preset) 外观和入场方式:Toast(默认)、Banner、Achievement 或 KillFeed。参见下面的"显示横幅"和"显示成就或击杀信息"。
NotificationBubble.EnterAnimation、ExitAnimation、EnterEasing、ExitEasing、EnterDurationMs、ExitDurationMs、EnterCurve、ExitCurve 与区域相同的成员,用来为单个气泡覆盖区域的设置。Inherit(动画)、Default(缓动)、0(时长)和空曲线(默认)都取区域的值。
NotificationBubble.DurationMs (duration) 气泡自行关闭之前的游戏时间毫秒数(5000)。Time.timeScale 为 0 期间倒计时停止。0 或更小则保留到用户关闭它。
NotificationBubble.RemainingMs 气泡自行关闭之前的毫秒数。
NotificationBubble.Loading (loading) 在文本前显示加载指示(false)。加载中的气泡不会自行关闭。
NotificationBubble.Count 气泡所代表的相同消息数(1)。大于 1 时,气泡将它显示为 "x3"。
NotificationBubble.ShowProgress 在此气泡上显示倒计时条。null(默认)采用区域的 ShowProgress。
NotificationBubble.Closable (closable) 显示关闭按钮(true)。
NotificationBubble.AddAction(text, callback) 添加一个按钮。点击会运行回调,然后关闭气泡。
NotificationBubble.Detail (detail) 消息下方的更多文本(富文本)。指针位于气泡上时才显示。在此之前,带详情的气泡在消息下方显示 "..."。
NotificationBubble.Expanded 从代码显示详情。指针进入时设置它,离开时清除它。
NotificationBubble.Dismiss() 播放退出动画,然后移除气泡。
NotificationBubble.Clicked 在按钮之外的点击,以及气泡获得焦点时的确认键上触发。
NotificationBubble.Dismissed 气泡离开层级之后触发。

更新已显示的气泡

保留 Show 返回的气泡并设置它的属性。气泡会原地改变,它的倒计时重新开始。

var bubble = workspace.Notifications.Show("3 files left");
// Later
bubble.Description = "2 files left";
bubble.Severity = Severity.Warning;

显示任务

ShowTask 在任务运行期间显示加载气泡。任务结束时,同一个气泡会变为成功或错误,然后自行关闭。

workspace.Notifications.ShowTask(SaveAsync(), "Saving...", "Saved",
    "Cannot save");

// The success message from the result of the task
workspace.Notifications.ShowTask(LoadAsync(), "Loading...",
    (int count) => $"{count} items loaded", "Cannot load");

要自己控制气泡,请设置 Loading,并在工作结束时清除它:

var bubble =
    workspace.Notifications.Show(
        new NotificationBubble { Description = "Connecting...",
            Loading = true });
// Later
bubble.Description = "Connected";
bubble.Severity = Severity.Success;
bubble.Loading = false;

点击时打开窗口

Clicked 在点击气泡时触发,不包括其关闭按钮或操作按钮。OpenWindow 打开窗口,窗口已打开时则让它获得焦点。

var bubble = workspace.Notifications.Show("New message: click to open");
// Keyboard and gamepad users can move to the bubble and press Submit
bubble.focusable = true;
bubble.Clicked += clicked =>
{
    workspace.OpenWindow("chat");
    clicked.Dismiss();
};

在游戏中显示拾取

给每次拾取一个图标和一个计数。使用 stack-duplicates 时,消息相同的拾取会把它的计数加到正在显示的气泡上,所以十次拾取木头显示为一行 "Wood x10"。

<tb:NotificationArea
    name="feed"
    corner="BottomLeft"
    stack-duplicates="true"
    show-progress="false"
    queue="false" />
feed.Show(new NotificationBubble
{
    Description = "Wood",
    Count = amount,
    Icon = Background.FromTexture2D(woodIcon),
    Severity = Severity.Success,
    DurationMs = 2500,
    Closable = false,
});

显示横幅

横幅是屏幕中间的大幅消息,例如 "Wave 3" 或 "Quest complete"。请使用 corner="Center" 和 max-visible="1" 的区域,这样横幅会一个接一个出现。

<tb:NotificationArea name="banners" corner="Center" max-visible="1" />
var banners = root.Q<NotificationArea>("banners");
banners.ShowBanner("WAVE 3", "Hold the gate", Severity.Warning);
// waits until "WAVE 3" leaves
banners.ShowBanner("QUEST COMPLETE", "The lost caravan");

显示成就或击杀信息

Preset 给气泡游戏消息的外观和入场方式。可在任何气泡上设置它。

// Achievement: a gold edge, a larger icon, and the card rises into place
workspace.Notifications.Show(new NotificationBubble
{
    Preset = NotificationPreset.Achievement,
    Title = "ACHIEVEMENT",
    Description = "Pathfinder: find 10 hidden places.",
    Icon = Background.FromTexture2D(trophyIcon),
    DurationMs = 6000,
});

// Kill feed: one small line with no close button, from the right edge
killFeed.Show(new NotificationBubble
{
    Preset = NotificationPreset.KillFeed,
    Description = "<b>Mira</b>  [Bow]  Raider",
    DurationMs = 3000,
});

改变气泡的进入和退出方式

进入和退出可以设置在区域上,也可以设置在单个气泡上。进入和退出分开设置。没有设置时,每个预设(Toast、Banner、Achievement、KillFeed)保持自己的外观。

var area = workspace.Notifications;
area.EnterAnimation = NotificationAnimation.SlideFromEdge;
area.EnterEasing = NotificationEasing.EaseOutBack;
area.EnterDurationMs = 350;
area.ExitAnimation = NotificationAnimation.Fade;

// A curve drives one bubble instead of the easing
var bubble = new NotificationBubble
{
    Description = "Quest complete",
    EnterAnimation = NotificationAnimation.Zoom,
    EnterCurve = NotificationCurves.Overshoot(),
};
area.Show(bubble);

提示

样式

规则位于 Assets/UIToolbox/Runtime/Resources/UIToolbox/Styles/Controls.uss。

类 元素
tb-notification-area 覆盖层。修饰类:--top-right、--top-left、--bottom-right、--bottom-left、--top-center、--bottom-center、--center。
tb-notification 一个气泡。修饰类:--info、--success、--warning、--error。
tb-notification--banner、tb-notification--achievement、tb-notification--killfeed Preset 的类。横幅宽且居中,带严重程度颜色的边框,并缩放进入。成就带金色边缘并升到位。击杀信息的行很小,没有关闭按钮,从右侧滑入。主题可以重新设置它们的样式。
tb-notification__body、__title、__message、__actions、__action、__close 气泡的各个部分。
tb-notification__detail、tb-notification__more 详情文本(弱化、小字、隐藏)和 "..." 标记。
tb-notification__icon Icon 的图片(32 x 32 像素)。
tb-notification__count 相同消息的计数,如 "x3"。它是一个 Badge,所以它还带有 tb-badge;tb-notification__count 的规则保持气泡的外观。
tb-notification__spinner 加载中气泡的旋转指示(一个 ProgressRing)。
tb-notification__progress 倒计时条。
tb-notification--loading 状态类:气泡显示旋转指示。
tb-notification--expanded 状态类:显示详情并隐藏标记。
tb-notification--hidden 状态类:进入动画的起点和退出动画的终点。
tb-notification--anim-in、tb-notification--anim-out 进入和退出动画的过渡类。
tb-notification--motion、tb-notification--motion-slide、--motion-fade、--motion-zoom、--motion-rise 进入或退出样式播放期间的状态类。与 tb-notification--hidden 一起,决定进入从哪里开始、退出在哪里结束。
tb-notification--slide 过渡类:在它之前的气泡到来、离开或改变高度时,气泡滑动到新位置。