1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 前端时间轴的那根线_如何使用时间轴信号

前端时间轴的那根线_如何使用时间轴信号

时间:2022-04-14 23:16:02

相关推荐

前端时间轴的那根线_如何使用时间轴信号

前端时间轴的那根线

Since Timeline’s introduction in , we know that you’ve been patiently waiting for a way to send events. Well, wait no more! Starting in Unity .1, a new feature called Signals helps you send events. Let’s dive in and see what this new feature is all about.

自Timeline推出以来,我们知道您一直在耐心等待发送事件的方式。 好吧,别再等了! 从Unity .1开始,名为Signals的新功能可帮助您发送事件。 让我们深入研究一下此新功能的含义。

讯号 (Signals)

Here’s what Signals look like in the Timeline window:

这是“时间轴”窗口中的“信号”外观:

We’ve built Signals to establish a communication channel between Timeline and outside systems. But what does that mean? Why did we decide on this approach?

我们已经构建了Signals,以在时间轴和外部系统之间建立通信通道。 但是,这是什么意思? 我们为什么要决定采用这种方法?

Let’s say that you are using Timeline to create a cutscene. When the cutscene ends, you want a new scene to be loaded and you want to enable a physics system. There are a couple of ways to implement this scenario:

假设您正在使用时间轴创建过场动画。 过场动画结束时,您希望加载新场景并启用物理系统。 有两种方法可以实现此方案:

When your cutscene ends, use custom scripts to make your Timeline instance interact directly with a scene manager and your physics system.

场景结束后,请使用自定义脚本使“时间轴”实例直接与场景管理器和物理系统交互。

When your cutscene ends, create markers and signals to send a ‘’Cutscene finished’’ signal and have interested systems react to the signal.

当场景结束时,创建标记和信号以发送“场景完成”信号,并使感兴趣的系统对该信号做出React。

The Timeline team went with the second approach because it keeps the emitter independent from the receiver. Keeping them independent adds a lot of flexibility, power, and reusability. To understand this more, let’s see how the different pieces work together.

时间轴团队采用了第二种方法,因为它可以使发射器独立于接收器。 使它们保持独立会增加很多灵活性,功能和可重用性。 为了进一步了解这一点,让我们看看不同的部分如何协同工作。

它如何一起工作? (How does it work together?)

To use signals to communicate with outside systems, you need three pieces: a Signal Asset, a Signal Emitter, and a Signal Receiver.

要使用信号与外部系统进行通信,您需要三部分:信号资产,信号发射 器和信号接收器。

Signal Emitter: A Signal Emitter contains a reference to a Signal Asset. In Timeline, a Signal Emitter is represented visually by a marker. You can place a marker on a track, or in the Markers area under the Timeline ruler.

信号发射器: 信号发射器包含对信号资产的引用。 在时间轴中,信号发射器由标记直观地表示。 您可以在轨道上或时间轴标尺下的“标记”区域中放置标记。

Signal Receiver: A Signal Receiver is a component with a list of reactions. Each reaction is linked to a Signal Asset.

信号接收器: 信号接收器是具有React列表的组件。 每个React都链接到信号资产。

Signal Asset: A Signal Asset is the association between a Signal Emitter and a Signal Receiver. You can reuse the same Signal Asset in many Timeline instances.

信号资产: 信号资产是信号发射器和信号接收器之间的关联。 您可以在许多时间轴实例中重复使用相同的信号资产。

例 (Example)

Here’s a simple game where you defeat a bunny zombie by pressing the arrow keys that match the directional images shown on-screen:

这是一个简单的游戏 ,您可以通过按与屏幕上显示的方向图像匹配的箭头键来击败兔子僵尸:

演示地址

The directional image randomly changes with each musical beat. If you don’t press the right arrow key before the image changes, you lose. If you press a certain number of arrow keys, you win.

方向性图像随每个音乐节拍随机变化。 如果在图像更改之前不按右箭头键,则会丢失。 如果按一定数量的箭头键,您将获胜。

In the game, theGameTimelineinstance includes the gameplay. It uses Signals to display new directional images with each musical beat, as shown in the finishedGameTimelineinstance below:

在游戏中,GameTimeline实例包含游戏玩法。 它使用信号来显示每个音乐节拍的新方向性图像,如 下面 完成的GameTimeline实例所示:

To demonstrate how to create and set up signals, let’s start from a project where none of the Signals have been created. If you want to follow along, you can download the project here.

为了演示如何创建和设置信号,让我们从没有创建任何信号的项目开始。 如果您想继续,可以在 此处 下载项目 。

First, to view the Markers area where you add Signals, click the Marker icon beside the Clip Edit modes. The Markers area appears beneath the Timeline ruler:

首先,要查看在其中添加信号的标记区域,请单击剪辑编辑模式旁边的标记图标。 标记区域显示在时间轴标尺下方:

To add a Signal Emitter, right-click in the Markers area and selectAdd Signal Emitter.

要添加信号发射器,请在“标记”区域中单击鼠标右键,然后选择“添加信号发射器”。

A Signal Emitter appears in the Markers area:

信号发射器出现在“标记”区域中:

The Signal Emitter you just added is selected and the Inspector window show its properties. The Inspector window also provides buttons for creating the other pieces of a signal:

您刚刚添加的信号发射器被选中,“检查器”窗口将显示其属性。 检查器窗口还提供用于创建信号的其他部分的按钮:

To link the Signal Emitter to a Signal Receiver, you need to add a new Signal Asset. In the Inspector window, clickCreate Signal…. Name the Signal Asset “ShowKey” because this Signal Asset will be used to link and emitter with a receiver to change the directional image. Click Save and the Signal Asset is added to the project.

要将信号发射器链接到信号接收器,您需要添加新的信号资产。 在检查器窗口中,单击“创建信号…”。 将信号资产命名为“ ShowKey”,因为此信号资产将用于与接收器链接和发射,以更改方向性图像。 单击保存,然后将信号资产添加到项目中。

You also want the Signal Emitter to be associated with a Signal Receiver, so click Add Signal Receiver. Your Inspector window should now look like this:

您还希望将信号发射器与信号接收器关联,因此请单击添加信号接收器。 现在,“检查器”窗口应如下所示:

Before continuing, let’s stop and describe what’s going on. When you click the Add Signal Receiver button, two things happen: a newSignal Receivercomponent is added to the bound GameObject and a new reaction is added and linked to theSignal Assetthat you just created. You can see they are linked because “ShowKey” appears as the Emit Signal and “ShowKey” is added to the list of reactions:

在继续之前,让我们停止并描述正在发生的事情。 当您单击“添加信号接收器”按钮时,会发生两件事:将新的信号接收器组件添加到绑定的GameObject,并添加新的React并将其链接到 刚创建 的信号资产。 您可以看到它们已链接,因为“ ShowKey”显示为发射信号,并且“ ShowKey”已添加到React列表中 :

The Inspector window shows two things: the Signal Emitter properties and the reactions defined by the Signal Receiver component:

“ 检查器”窗口显示两件事:“信号发射器”属性和“信号接收器”组件定义的React:

Although the Signal Receiver component is linked to the GameObject that is associated with the Timeline instance, you can edit the Signal Receiver in the Inspector window. The reaction is invoked when the Signal Receiver receives the signal.

尽管Signal Receiver组件已链接到与时间轴实例关联的GameObject,但是您可以在Inspector窗口中编辑Signal Receiver。 当信号接收器接收到信号时,将调用该响应。

The last step is to specify what the reaction does. In this example, there is a component named Manager with aShowRandomKeymethod. This method displays a new random arrow key. To have the reaction call theShowRandomKeymethod, select this method for the Unity Event, as shown below:

最后一步是指定React的作用。 在此示例中,有一个名为Manager的组件,带有ShowRandomKey方法。 此方法显示一个新的随机箭头键。 要使响应调用ShowRandomKey方法,请为Unity Event选择此方法,如下所示:

And that’s it! After adding the Signal Emitter and defining its Signal Receiver and the reaction, you now have an example of a Timeline instance that communicates with the scene.

就是这样! 添加信号发射器并定义其信号接收器和React之后,您现在有了一个与场景进行通信的时间轴实例的示例。

When Timeline hits the Signal Emitter, the ShowKey signal is emitted. The Signal Receiver, that is listening for the ShowKey signal, calls theShowRandomKeymethod.

当时间轴到达信号发射器时,将发射ShowKey信号。 正在侦听ShowKey信号的信号接收器调用ShowRandomKey方法。

But you don’t have to stop there. Multiple signal emitters can emit the same signal. For example, the following Timeline instance has the same Signal Emitter copied and moved to different times; there is an emitter for every musical beat:

但是您不必在此停下来。 多个信号发射器可以发射相同的信号。 例如,以下时间轴实例具有相同的信号发射器,已复制并移动到不同的时间。 每个音乐节拍都有一个发射器:

You can also drag a Signal Asset directly from the Project window to the Timeline window. A Signal Emitter is automatically created with theEmit Signalproperty already set.

您也可以将信号资产直接从“项目”窗口拖到“时间轴”窗口。 在设置了“发射信号”属性的情况下, 会自动创建一个信号发射器 。

To see what the finished project looks like, with all Signals, download it here.

要查看带有所有Signals的已完成项目的外观,请 在此处下载 。

总结一下 (To summarize)

In order to set up your first signal, you need to:

为了设置您的第一个信号,您需要:

Right-click on a Marker area or a track that supports signals, then chooseAdd Signal Emitter…

右键单击“标记”区域或支持信号的轨道,然后选择“添加信号发射器”。

In the Inspector window, click Create Signal Asset, choose a file name and press Enter.

在检查器窗口中,单击“创建信号资产”,选择一个文件名,然后按Enter。

Still in the Inspector, clickCreate Reaction…button and define the reaction associated with the signal you just created.

仍在检查器中,单击“创建React…”按钮并定义与您刚创建的信号相关的React。

奖励常见问题 (Bonus FAQ)

Can I have multiple receivers on a single GameObject?Yes, all Signal Receivers on a GameObject receives the signals sent to that GameObject.

我可以在一个GameObject上有多个接收器吗?是的,GameObject上的所有信号接收器都接收发送到该GameObject的信号。

Can a signal asset be reused between multiple Timeline instances?Yes, a Signal Asset can be used in more than one Timeline instance.

信号资产可以在多个时间轴实例之间重用吗?是的,一个信号资产可以在多个时间轴实例中使用。

Can a Signal Asset be extended and customized?Yes, Signal Asset, Signal Emitter and Signal Receiver can all be extended. But this is a subject for another day; there will be another blog post about customization.

信号资产可以扩展和定制吗?是的,信号资产,信号发射器和信号接收器均可扩展。 但这是另一天的话题。 将会有另一篇有关自定义的博客文章。

Are signals guaranteed to be emitted?Yes. Signals will not be skipped; they will be emitted on the next frame regardless of the game’s FPS.

是否保证发出信号?是。 信号不会被跳过; 无论游戏的FPS是多少,它们都会在下一帧发出。

What happens if duplicate a Timeline Asset contains signal emitters?The signal emitters keep their reference to their Signal Assets. Signal Assets are not duplicated.

如果重复的时间轴资产包含信号发射器,会发生什么?信号发射器参考其信号资产。 信号资产不重复。

演示地址

-

Check out beginner and advanced learning content for using Timeline on theUnity Learn Premiumplatform.

在Unity Learn Premium平台上查看有关使用时间轴的初学者和高级学习内容。

翻译自: //05/21/how-to-use-timeline-signals/

前端时间轴的那根线

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。