PAYATHON 2026

如何通过代码为 TimelineAsset 创建轨道?

技术老齐

结论

TimelineAsset.CreateTrack 是泛型方法,调用时要指定轨道类型,例如 AudioTrack。TimelineAsset 继承自 ScriptableObject,应通过 ScriptableObject.CreateInstance<T>() 创建,不能使用 new,也不能通过 GameObject.AddComponent<T>() 添加。

在单元测试中可以这样写:

TimelineAsset timeline =
    ScriptableObject.CreateInstance<TimelineAsset>();

AudioTrack track =
    timeline.CreateTrack<AudioTrack>(null, "whoosh");

这里的 null 表示创建根轨道,不将它作为其他轨道的子轨道。

完整的测试示例

如果 Edit Mode 测试不需要把 Timeline 保存为项目资源,可以使用下面的代码:

using NUnit.Framework;
using UnityEngine;
using UnityEngine.Timeline;

public class ProjectSettingsTests
{
    [Test]
    public void ProjectSettingsTest()
    {
        TimelineAsset timeline =
            ScriptableObject.CreateInstance<TimelineAsset>();

        try
        {
            AudioTrack track =
                timeline.CreateTrack<AudioTrack>(null, "whoosh");

            TimelineClip clip =
                track.CreateClip<AudioPlayableAsset>();

            clip.displayName = "whoosh clip";
            clip.start = 0;
            clip.duration = 1;

            Assert.AreEqual("whoosh", track.name);
            Assert.AreEqual("whoosh clip", clip.displayName);

            // 根据 Settings 的实际接口继续测试转换结果。
            // Settings settings = new Settings();
            // string result = settings.Convert(timeline);
            // Assert.That(result, ...);
        }
        finally
        {
            Object.DestroyImmediate(timeline);
        }
    }
}

如果当前 Unity Timeline 版本提供简化重载,也可以写成:

AudioTrack track = timeline.CreateTrack<AudioTrack>("whoosh");

不过,带 parent 参数的写法兼容性更明确:

timeline.CreateTrack<AudioTrack>(null, "whoosh");

为什么 CreateTrack("whoosh") 无法编译

CreateTrack 的泛型版本需要类型参数 T,用来确定要创建哪一种 TrackAsset:

CreateTrack<T>(TrackAsset parent, string trackName)
    where T : TrackAsset

调用下面的代码时:

timeline.CreateTrack("whoosh");

编译器只知道轨道名称,无法判断应该创建 AudioTrack、AnimationTrack、ActivationTrack,还是自定义轨道,因此会报告无法推断类型参数。

调用时需要显式提供轨道类型:

AudioTrack track =
    timeline.CreateTrack<AudioTrack>(null, "whoosh");

命名参数只用于说明传入的值对应哪个形参:

timeline.CreateTrack<AudioTrack>(
    parent: null,
    trackName: "whoosh"
);

它不能帮助编译器推断泛型类型。下面的写法仍然缺少 T:

timeline.CreateTrack(name: "whoosh");

具体参数名可能因 Timeline 包版本或重载而异。使用命名参数时,应以当前项目中 IDE 显示的方法签名为准。

为什么不能使用 new TimelineAsset()

TimelineAsset 的继承关系大致如下:

TimelineAsset
└── PlayableAsset
    └── ScriptableObject
        └── Object

ScriptableObject 需要由 Unity 的对象系统初始化,因此要通过 ScriptableObject.CreateInstance<T>() 创建:

TimelineAsset timeline =
    ScriptableObject.CreateInstance<TimelineAsset>();

不要使用:

TimelineAsset timeline = new TimelineAsset();

即使这段代码在某些环境下能够通过 C# 编译,创建出的对象也没有经过 Unity 所需的原生对象初始化,后续 API 可能无法正常工作。

为什么 AddComponent<TimelineAsset>() 也不行

GameObject.AddComponent<T>() 对泛型参数有以下约束:

where T : Component

它只能添加继承自 Component 的类型,例如:

AudioSource source = gameObject.AddComponent<AudioSource>();

TimelineAsset 继承自 ScriptableObject,并非 Component,所以不能挂载到 GameObject 上:

gameObject.AddComponent<TimelineAsset>(); // 编译错误

如果要在场景中播放 Timeline,可以把 PlayableDirector 添加到 GameObject,然后将创建好的 TimelineAsset 赋给它:

GameObject go = new GameObject("Timeline Test");

PlayableDirector director =
    go.AddComponent<PlayableDirector>();

TimelineAsset timeline =
    ScriptableObject.CreateInstance<TimelineAsset>();

director.playableAsset = timeline;

如果只测试 Timeline 数据转换,一般不需要创建 GameObject 或 PlayableDirector。

创建其他类型的轨道

泛型参数必须是 TrackAsset 的派生类型:

AnimationTrack animationTrack =
    timeline.CreateTrack<AnimationTrack>(null, "Animation");

ActivationTrack activationTrack =
    timeline.CreateTrack<ActivationTrack>(null, "Activation");

SignalTrack signalTrack =
    timeline.CreateTrack<SignalTrack>(null, "Signals");

自定义轨道的写法相同:

MyCustomTrack customTrack =
    timeline.CreateTrack<MyCustomTrack>(null, "Custom");

MyCustomTrack 需要正确继承 TrackAsset,并满足当前 Timeline 版本对自定义轨道的要求。

测试时需要注意什么

测试结束后,应销毁临时创建的 Unity 对象,避免残留对象影响后续测试:

Object.DestroyImmediate(timeline);
Object.DestroyImmediate(go);

如果测试只检查内存中的轨道和 clip 数据,就不需要调用 AssetDatabase.CreateAsset。测试资源序列化、重新加载或项目文件保存时,才需要通过 UnityEditor API 将 TimelineAsset 保存为 .playable 资源。这类代码只能放在 Editor 环境中。

这两个错误的原因不同。CreateTrack 缺少具体的轨道类型,AddComponent 则要求目标类型继承 Component。先用 ScriptableObject.CreateInstance<TimelineAsset>() 创建 Timeline,再调用 CreateTrack<AudioTrack>(null, "whoosh") 即可。

备注:内容仅供参考。