unity 自定义时间轴_如何创建自定义时间轴标记

本文介绍了如何在Unity 2019.1及更高版本中利用时间轴(Timeline)创建自定义标记。从简单的标记创建开始,逐步讲解如何结合可播放通知(Playable Notifications)、TimeNotificationBehaviour、MarkerNotification来实现代码执行和自定义样式。通过实现INotification接口,可以控制通知的发送时间,并通过自定义样式改变标记的视觉表现。此外,还展示了如何利用片段发送通知,增强时间轴功能。

unity 自定义时间轴

Starting with Unity 2019.1, Timeline supports markers! In this blog post, I will show you how to create custom markers.

Unity 2019.1 开始 ,时间轴支持标记! 在此博客文章中,我将向您展示如何创建自定义标记。

Previously, I explained how to use Timeline Signals to trigger events. When we originally designed that feature, we quickly saw that in order to make it work, we couldn’t use clips. Since one of the main characteristics of a signal is that it has no “duration”, we would need a new type of item, hence the addition of markers to Timeline. Internally, signals are implemented using markers. Let’s see how to add a custom marker to Timeline.

之前 ,我解释了如何使用时间轴信号触发事件。 当我们最初设计该功能时,我们很快看到为了使其起作用,我们无法使用剪辑。 由于信号的主要特征之一是它没有“持续时间”,因此我们需要一种新型的物品,因此需要在时间轴上添加标记。 在内部,信号是使用标记实现的。 让我们看看如何向时间轴添加自定义标记。

The code and assets used for this blog post are available here.

此处提供了 用于此博客文章的代码和资产

一个简单的标记 (A Simple Marker)

A marker is a new item that can be added to a Timeline Asset and is used to represent a point in time. Markers also have a specialization, just like clips do (Activation clip, Audio clip, Animation clip, etc). This lets you create your own type of marker to build something that covers your specific workflows.

标记是可以添加到时间轴资产的新项目,用于表示时间点。 标记也具有特殊性,就像剪辑一样( 激活 剪辑, 音频 剪辑, 动画 剪辑等)。 这使您可以创建自己的标记类型来构建涵盖您特定工作流程的标记。

In order to add a new type of marker, all you need to do is to create a class that inherits the Marker class:

为了添加新型标记,您需要做的就是创建一个继承 Marker 类的类:

1

public class SimpleMarker : UnityEngine.Timeline.Marker {}

1

public class SimpleMarker : UnityEngine . Timeline . Marker { }

That’s it! This custom marker can now be added to any track on the timeline marker area:

而已! 现在可以将此自定义标记添加到时间线标记区域上的任何轨道:

At this point, this simple marker is only a visual item. This means that this marker cannot run code when triggered. That doesn’t mean it is isn’t useful; a marker can be a snap point or an annotation (see Part V). It’s also accessible through the Timeline API in editor and at runtime.

在这一点上,这个简单的标记只是一个 视觉项目 。 这意味着该标记 在触发时 无法运行代码 。 这并不意味着它没有用。 标记可以是捕捉点或注释(请参见第V部分)。 也可以 通过 编辑器中和运行时 的Timeline API对其 进行 访问 。

We will need to combine a marker with another system to make it able to execute code. If you’re interested in knowing how the system works, read the next two parts, otherwise, you can skip to Part IV (I won’t be offended!). 

我们将需要将标记与另一个系统结合起来以使其能够执行代码。 如果您想了解系统的工作原理,请阅读下面的两部分,否则,您可以跳至第四部分(我不会感到冒犯!)。

第二部分–可播放的通知 (Part II – Playable Notifications)

The Playable API allows notifications to be sent to an object while a PlayableGraph is processed. Playable Notifications can be used to inform a target object that an event occurred. I will build a simple graph and manually send a notification.

Playable API允许在处理PlayableGraph时将通知发送到对象。 可播放的通知可用于通知目标对象已发生事件。 我将构建一个简单的图形并手动发送通知。

First, I need to create a notification: a class that implements the INotification interface.

首先,我需要创建一个 Notification :一个实现 INotification 接口的类。

1

2
3
4
public class MyNotification : INotification
{
    public PropertyName id { get; }
}

1

2
3
4
public class MyNotification : INotification
{
     public PropertyName id { get ; }
}

We can use the id property to uniquely identify the notification. For these examples, I don’t really need it, so I will use the default implementation.

我们可以使用 id 属性来唯一标识通知。 对于这些示例,我并不是真的需要它,因此我将使用默认实现。

Then, I need a receiver: a class that implements the INotificationReceiver interface. For this example, I have a receiver that will print the time at which a notification was received. 

然后,我需要一个 接收器 :一个实现 INotificationReceiver 接口的类。 对于此示例,我有一个接收器,它将打印接收通知的时间。

1

2
3
4
5
6
7
8
9
10
11
class ReceiverExample : INotificationReceiver
{
   public void OnNotify(Playable origin, INotification notification, object context)
   {
       if (notification != null)
       {
           double time = origin.IsValid() ? origin.GetTime() : 0.0;
           Debug.LogFormat("Received notification of type {0} at time {1}", notification.GetType(), time);
       }
   }
}
<think>好的,我现在需要解决用户的问题:Unity时间轴中特效不显示的情况。根据用户提供的引用内容,特别是引用[1]中提到的问题,即特效在Editor里正常,但打包后不显示。我需要逐步分析可能的原因和解决方案。 首先,用户提到的是Timeline里的特效在打包后消失,这可能涉及到资源打包的问题。可能的原因有: 1. **资源未被正确包含在构建中**:有时候,特效依赖的材质、贴图或Shader可能没有被Unity自动包含到构建中,尤其是在使用自定义资源或第三方插件时。需要检查这些资源的设置。 2. **平台兼容性问题**:比如Shader在不同平台(如Android或iOS)的兼容性问题。引用[3]提到了MMD模型在Android上的兼容性问题,可能类似的问题出现在特效的Shader上。 3. **时间轴动画绑定问题**:特效可能在时间轴中被正确设置,但在运行时由于对象未激活或绑定丢失导致无法显示。 接下来,根据引用[1]的分析步骤,可能需要进行以下检查: - **检查特效资源的依赖项**:确保所有材质、贴图、Shader都被正确引用,并且在构建设置中标记为包含。 - **Shader兼容性**:查看特效使用的Shader是否支持目标平台。可能需要更换为跨平台兼容的Shader,或修改Shader设置。 - **构建后资源路径问题**:有时候,打包后的资源路径可能与Editor中不同,导致资源加载失败。需要确保资源路径正确,或使用Resources.Load等方式加载。 此外,引用[2]提到了特效插件可能提供解决方案,比如使用Teleport Effect等资源,可能需要检查是否有插件冲突或需要更新。 可能的解决步骤: 1. **检查构建设置**:在Build Settings中确保所有相关场景和资源被添加。 2. **验证Shader兼容性**:在Project Settings的Graphics设置中,查看包含的Shader是否包括特效使用的那些。 3. **测试不同平台**:尝试在其他平台打包测试,确定是否为特定平台问题。 4. **查看打包日志**:构建后的日志可能包含资源丢失或Shader错误的提示,帮助定位问题。 需要确保用户按照这些步骤逐一排查,特别是在资源管理和平台兼容性方面。同时,参考引用中的案例,可能需要手动处理资源包含或调整Shader设置。</think>### Unity时间轴中特效不显示的解决方案 #### 1. **检查资源依赖与构建设置** - **确保资源被包含在构建中** 特效依赖的材质、贴图或Shader可能未被自动打包。在Unity编辑器中,通过以下步骤验证: - 在Project窗口中选择特效资源,查看Inspector面板中的**“Include in Build”**选项是否勾选[^1]。 - 在构建设置(`File > Build Settings`)中,确保所有相关场景已添加到**Scenes In Build**列表。 - **手动标记关键资源** 若资源未被自动识别,可通过脚本或插件强制包含(例如使用`Resources`文件夹或`Addressables`)。 #### 2. **验证Shader与平台兼容性** - **检查Shader支持的目标平台** - 在Project窗口中选择特效使用的Shader,查看其Inspector面板是否支持目标平台(如Android、iOS)。 - 若Shader不兼容,替换为跨平台Shader(如Unity内置的`Standard Shader`或第三方插件提供的Shader)[^2]。 - **调整Graphics设置** 进入`Edit > Project Settings > Graphics`,在**Always Included Shaders**列表中添加特效所需的Shader,确保其被编译到构建中。 #### 3. **排查时间轴绑定与激活状态** - **确认特效对象在时间轴中的绑定** - 在Timeline窗口检查特效的轨道是否绑定到正确的GameObject,且未因预制体嵌套导致引用丢失。 - 若使用预制体实例,确保其已在场景中激活(`SetActive(true)`)。 - **运行时调试** 通过代码在播放时间轴时动态激活特效: ```csharp // 在Timeline播放事件中调用 void PlayEffect() { effectGameObject.SetActive(true); } ``` #### 4. **分析构建日志与测试简化场景** - **查看构建日志** 打包后,打开Unity Editor的构建日志(`Window > Analysis > Build Report`),搜索“Error”或“Missing”关键字,定位缺失的资源或Shader报错[^1]。 - **创建最小化测试场景** 新建一个仅包含时间轴和特效的测试场景,逐步添加依赖项,确定具体是哪个环节导致问题。 #### 5. **第三方插件与资源更新** - **更新或替换插件** 若特效来自第三方插件(如引用[2]中的Teleport Effect),检查插件版本是否兼容当前Unity版本,或尝试替换为其他插件。 - **重新导入纹理与模型** 对于复杂模型(如引用[3]提到的MMD模型),若纹理未正确导入,右键选择纹理文件并点击`Reimport`,确保压缩格式(如ASTC)与目标平台匹配。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值