UI Toolbox 徽标UI Toolbox
支持

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

Callout

Callout 是对话气泡:一个带尖角的方框,指向某个控件。它会一直保持打开,直到你的代码、Escape 或在外部按下将其关闭。可用于提示、教程步骤、字段旁的警告,或带控件的小型弹出框。对于指针悬停在控件上时才显示的文字,请使用 ToolTip。对于悬停时打开的标注,请使用 HoverCard。对于包含多个步骤、并在每个控件周围压暗屏幕的导览,请使用 TourOverlay。

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

CalloutDemo 的 Tutorial 标签页:一个标注从下方指向 HUD 的生命条,带有连接线、步骤计数以及 Back 和 Next 按钮CalloutDemo 的 Tutorial 标签页:一个标注从下方指向 HUD 的生命条,带有连接线、步骤计数以及 Back 和 Next 按钮

在 UXML 中添加标注的方法:

  1. 在窗口的 UXML 中声明标注。把 target 设为它所指向的控件的名称。
  2. 在代码中找到标注,并从按钮调用 Open 或 Toggle。
<ui:Button name="rename" text="Rename" />
<tb:Callout
    name="renamePopover"
    target="rename"
    placement="Right"
    close-on-outside-click="true"
    description="Type the new name.">
    <ui:TextField label="Name" />
    <ui:Button name="ok" text="OK" />
</tb:Callout>
var popover = root.Q<Callout>("renamePopover");
root.Q<Button>("rename").clicked += () => popover.Toggle();
root.Q<Button>("ok").clicked += popover.Close;
popover.Closed += () =>
    Debug.Log($"Name: {popover.Q<TextField>().value}");

在代码中创建标注的方法:

var hint = new Callout
{
    Description = "Click here to save.",
    Placement = CalloutPlacement.Right
};
hint.Open(saveButton);

在字段旁显示警告的方法:

var warning = new Callout
{
    Description = "The name is in use.",
    Severity = Severity.Warning
};
nameField.RegisterValueChangedCallback(evt =>
{
    if (names.Contains(evt.newValue)) warning.Open(nameField);
    else warning.Close();
});

让标注停在固定位置的方法(角落里的教程文字,先指向一个控件,再指向下一个):

var step = new Callout
{
    Placement = CalloutPlacement.Free,
    TipSize = 18,
    Description = "First, pick a class."
};
step.style.left = 40;
step.style.top = 170;
step.style.width = 220;
step.Open(classDropdown);
// The box stays. The tip turns to the new target.
nextButton.clicked += () =>
{
    step.Description = "Then type a name.";
    step.Target = nameField;
};

教程提示:用户可以移动的标注,带有指向控件的连线和围绕控件的框:

<ui:Button name="continue" text="Continue" />
<tb:Callout
    name="tutorial"
    target="continue"
    placement="Free"
    style="left: 40px; top: 170px;"
    title="Step 1"
    description="Click the button to continue."
    connector="true"
    highlight-target="true"
    draggable="true" />
var tutorial = root.Q<Callout>("tutorial");
tutorial.Open();
root.Q<Button>("continue").clicked += tutorial.Close;

// The same callout in code
var hint = new Callout
{
    Placement = CalloutPlacement.Free,
    Title = "Step 1",
    Description = "Click the button to continue."
};
hint.Connector = true;
hint.HighlightTarget = true;
// the user can move the hint off the controls that it covers
hint.Draggable = true;
hint.style.left = 40;
hint.style.top = 170;
hint.Open(continueButton);

让一个标注指向多个元素的方法:

// The box is next to the first element. The other two get a line from
// the box and a frame.
var step = new Callout
{
    Title = "Edit the label",
    Description = "Select the series, then change its Label.",
    HighlightTarget = true
};
step.Open(labelField, seriesRow, chart);

让标注呈现对话气泡或思考气泡外观的方法:

<tb:Callout
    target="npc"
    look="Talk"
    placement="Above"
    tip-size="18"
    description="Hello, traveler!" />
<tb:Callout
    target="hero"
    look="Thought"
    placement="Above"
    tip-size="28"
    description="I need a key." />

Talk 使方框带有圆角,并用弯曲的尾巴取代尖角。Thought 在方框周围画一朵云,尾巴由三个朝目标逐渐变小的圆组成。这两种外观使用方框的背景和边框。

让标注指向 3D 场景中的物体的方法:

// A tag that follows an enemy on the screen, with lock-on brackets
// around it
var tag = new Callout
{
    Title = "TARGET",
    Description = "Orc",
    TargetMark = TargetMark.Brackets,
    ClampToEdge = true
};
tag.TargetRenderer(enemy.GetComponent<Renderer>());
tag.Open(hudRoot);

// A speech bubble over the head of an NPC
var talk = new Callout
{
    Description = "Hello!",
    Look = CalloutLook.Talk,
    Placement = CalloutPlacement.Above
};
talk.TargetWorld(npc.transform.Find("Head"));
talk.Open(hudRoot);

// A label at a point of the world
var label = new Callout
{
    Description = "Exit",
    Connector = true,
    TipSize = 40
};
label.TargetWorldPosition(new Vector3(12, 3, 40));
label.Open(hudRoot);

标注每帧计算目标的屏幕位置。你交给 Open 的元素决定标注显示在哪个工作区(或面板)之上。在 UXML 中声明的标注可以不带元素地使用 Open()。

标注使用 WorldAnchor 进行投影。你也可以用它让自己的元素跟随 3D 物体。对于许多物体上的标记,请使用 WorldMarkerLayer。

当摄像机绘制到由某个元素显示的渲染纹理中时,请同时给出摄像机和该元素:

tag.TargetRenderer(enemy.GetComponent<Renderer>(), minimapCamera,
    minimapView);
tag.Open();

标注随后会把摄像机的视图映射到该元素,元素的边缘就是屏幕的边缘。

把编辑器固定在 3D 物体上,并带有可关闭它的 “×” 按钮的方法:

// One callout for each object. Several can be open at once.
var editor = new Callout
{
    Title = crate.name,
    Connector = true,
    TipSize = 48
};
editor.Closable = true;  // stays open until the user clicks the ×
editor.Draggable = true; // the user can move it off the scene
editor.dataSource = crate; // the fields bind to the crate
editor.Add(new Slider("Size", 0.2f, 3) { bindingPath = "Size" });
editor.TargetRenderer(crate.GetComponent<Renderer>());
editor.Open(workspace);

连线从方框通向物体,并且每帧跟随它。在标注上按下,会让它显示在其他标注之上。Bindings 示例会为用户点击的每个形状打开这样一个标注。

成员 说明
Title (title) 文字上方的粗体第一行。为空(默认值)则不显示标题。
Description (description) 标题下方、内容上方的文字。为空(默认值)则不显示文字。
TargetName (target) 目标元素的名称。标注在拥有它的最近祖先之下查找第一个匹配项。未设置目标时,Open() 使用它。
Target 尖角所指向的元素。已打开的标注会移到新的目标。设置新的元素会移除 3D 目标。
MoreTargets 标注在 Target 之外所指向的更多元素。每个可见的元素都会得到一条从方框最近边缘出发的线,样式与 Connector 相同;设置 HighlightTarget 时还有一个框。方框仍停在 Target 旁边。默认为空。
TargetWorld(transform, camera, viewport) 让标注每帧指向某个 Transform 的位置。camera 默认为 null:标注使用 Camera.main。viewport 是显示摄像机渲染纹理的元素;摄像机绘制到屏幕上时为 null。
TargetWorldPosition(position, camera, viewport) 让标注指向世界中的一个固定点。
TargetRenderer(renderer, camera, viewport), TargetCollider(collider, camera, viewport) 让标注指向 Renderer.bounds 或 Collider.bounds 的 8 个角在屏幕上围成的矩形。TargetMark 绘制在这个矩形周围。
HasWorldTarget 目标在 3D 场景中时为 true。
TargetInView 目标在屏幕上,或在视口中时为 true。目标在摄像机后方或超出屏幕时为 false。
ClampToEdge (clamp-to-edge) 对于超出视野的 3D 目标:true 让标注停在屏幕边缘,尖角朝向目标的方向。False(默认值)会隐藏标注,直到目标再次进入视野。
TargetMark (target-mark) None(默认值)、Box 或 Brackets。在目标周围绘制一个矩形或四个角括号,对元素或 3D 目标都适用。标记以一段短动画进入,尖角止于它。
AnimationDuration (animation-duration) TargetMark 的标记进入时的动画时间,单位为毫秒。默认 250。0 则立即显示标记。
AnimationEasing (animation-easing) 标记动画的曲线,是缓动曲线之一。默认 OutCubic。使用 OutBack 或 OutElastic 时,标记会收拢到超过它的位置,再退回来。
Look (look) Default、Talk(对话气泡)或 Thought(思考气泡)。它会设置类 tb-callout--talk 或 tb-callout--thought。
Placement (placement) Below(默认值)、Above、Right、Left 或 Free。工作区没有空间时,一侧会翻转到对侧。Free 保留标注样式中的 left 和 top。
Alignment (alignment) 标注沿目标一侧的位置:Start、Center(默认值)或 End。Start 让左边缘对齐成一条线,对于位于右侧或左侧的标注则让上边缘对齐。End 对右边缘或下边缘做同样的事。对 Free 无效。类型是 CalloutAlignment。
TipSize (tip-size) 尖角的长度,单位为像素,也是标注与目标之间的间距。默认 10。0 则不显示尖角。
Connector (connector) 从标注的边缘到目标最近的边缘绘制一条直线。这条线取代尖角。默认关闭。
HighlightTarget (highlight-target) 标注打开时,在目标周围显示一个框。用户可以透过这个框点击目标。默认关闭。
Draggable (draggable) 用户可以拖动标注的方框。它会停在用户放置的位置,直到下一次 Open。默认关闭。
Severity (severity) Info(默认值)、Success、Warning 或 Error。它会设置类 tb-callout--{severity}:边框和尖角的颜色。
CloseOnOutsideClick (close-on-outside-click) 在标注及其目标之外按下会将其关闭。默认关闭。
Closable (closable) 在右上角显示一个 “×” 按钮,用来关闭标注。默认关闭。CloseOnOutsideClick 关闭时,标注保持打开,直到用户点击该按钮,或在其中按下 Escape。
Open(target) 在 target 旁边显示标注,或在 Target 旁边,或在名称为 TargetName 的元素旁边。已打开的标注只会移到新的目标。
Open(target, moreTargets...) 设置 MoreTargets,然后在 target 旁边打开。
Close(), Toggle(target) 隐藏标注,或切换其状态。
Side 翻转之后,标注当前所在的一侧。用户拖动之后为 Free。
IsOpen, Opened, Closed 状态及其事件。Closed 在关闭开始时触发,早于退出动画结束。

提示:

样式

类 元素
tb-callout 方框:position: absolute,最宽 320 px,背景为 --tb-color-surface-alt,边框为 --tb-color-accent,圆角为 --tb-radius-md。尖角具有方框的背景颜色,以及上边框的颜色和宽度。
tb-callout--info, --success, --warning, --error 严重程度。它设置边框颜色。Info 保持强调色;error 使用 --tb-color-danger。
tb-callout__title Title 的粗体 Label,即第一行。
tb-callout__text Description 的 Label,位于内容上方。
tb-callout__close Closable 的 “×” Button:右上角 20 乘 20 px,外观与其他关闭按钮一样扁平。
tb-callout--closable 带 “×” 按钮的标注。它在右侧为按钮留出空间。
tb-callout__connector 绘制连接线的元素。它位于工作区中标注的旁边、方框之下,不接收指针事件。
tb-callout__highlight 目标周围的框:2 px 边框,圆角为 --tb-radius-md。它位于工作区中标注的旁边,不接收指针事件。标注赋予它连接线的颜色。
tb-callout__target-mark 绘制 TargetMark 的方框或括号的元素。它覆盖工作区,位于标注之下,不接收指针事件。
tb-callout--talk Talk 外观:圆角 18 px,两侧内边距更大。
tb-callout--thought Thought 外观:两侧内边距更大,圆角较小。云超出方框 10 px。
tb-callout--out-of-view 3D 目标超出视野:位于边缘的标注不透明度为 0.75。
tb-callout--hidden 不透明度为 0,缩放为 0.8:进入动画的起点和退出动画的终点。
tb-callout--anim-in, --anim-out opacity 和 scale 的过渡,使用 --tb-animation-duration-fast。

在标注上设置这些 USS 变量,可以为连接线和目标框设置样式:

变量 说明
--tb-callout-connector-color 线和框的颜色。默认是标注的边框颜色。
--tb-callout-connector-width 线的宽度,单位为像素。默认 2。
--tb-callout-connector-cap 线在目标处的末端:"dot" 或 "arrow"。默认没有端帽。
--tb-callout-box-color TargetMark 的方框或括号的颜色。默认是连接线的颜色。
--tb-callout-box-width 方框线条的宽度,单位为像素。默认 2。
--tb-callout-box-padding 目标与方框之间的间距,单位为像素。默认 6。
--tb-callout-box-corner 括号每条线的长度,单位为像素。默认 14。
.enemy-tag {
    --tb-callout-box-color: rgb(255, 196, 0);
    --tb-callout-box-width: 3;
    --tb-callout-box-corner: 20;
}
.tb-callout {
    --tb-callout-connector-color: var(--tb-color-warning);
    --tb-callout-connector-width: 3;
    --tb-callout-connector-cap: "arrow";
}
.tb-callout__highlight { border-width: 3px; border-radius: 0; }

要让每个标注都变成带粗边框的黄色便条:

.tb-callout {
    background-color: #fff3b0;
    border-color: #c9a400;
    border-width: 2px;
    border-radius: 2px;
}
.tb-callout__text { color: #3b2f1e; }

请使用不透明的背景。尖角画在方框的边框之上,透明的背景会露出重叠的部分。