Unity3D Timeline扩展与自定义事件处理

前言

在Unity3D中扩展Timeline系统并实现自定义事件处理,可以通过以下步骤完成:

一、使用Signal系统(内置方法)

  1. 创建SignalAsset
  • 在Project窗口右键选择 Create > Signal ,生成一个SignalAsset(如EnemySpawnSignal)。

  • 添加Signal Track和Emitter

  • 在Timeline窗口中右键添加 Signal Track

  • 在Signal Track上右键添加 Signal Emitter,并分配创建的SignalAsset。

对惹,这里有一 个游戏开发交流小组 ,希望大家可以点击进来一起交流一下开发经验呀!

  1. 配置Signal Receiver
  • 在目标GameObject上添加SignalReceiver组件。
  • 将SignalAsset拖入Receiver,绑定到对应的UnityEvent或方法。

示例代码:

arduino 复制代码
public class EnemySpawner : MonoBehaviour
{
    public void SpawnEnemy()
    {
        Instantiate(enemyPrefab, spawnPoint.position, Quaternion.identity);
    }
}

在SignalReceiver中绑定EnemySpawnSignalSpawnEnemy方法。

二、自定义Track和Clip(高级扩展)

1. 创建自定义Clip和Behaviour

csharp 复制代码
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

[System.Serializable]
public class EventClip : PlayableAsset
{
    public UnityEvent onTrigger;

    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        var playable = ScriptPlayable<EventBehaviour>.Create(graph);
        var behaviour = playable.GetBehaviour();
        behaviour.onTrigger = onTrigger;
        return playable;
    }
}

public class EventBehaviour : PlayableBehaviour
{
    public UnityEvent onTrigger;
    private bool triggered;

    public override void OnBehaviourPlay(Playable playable, FrameData info)
    {
        if (!triggered && onTrigger != null)
        {
            onTrigger.Invoke();
            triggered = true;
        }
    }
}
  1. 创建自定义Track
csharp 复制代码
[TrackClipType(typeof(EventClip))]
[TrackBindingType(typeof(GameObject))]
public class EventTrack : TrackAsset { }

3. 在Timeline中使用

  • 添加EventTrack到Timeline。
  • EventClip拖入轨道,在Inspector中配置onTrigger事件。

三、自定义Marker触发事件

1. 创建Marker和Receiver

kotlin 复制代码
[Serializable]
public class CustomMarker : Marker, INotification
{
    public PropertyName id { get; }
    public string message;
}

public class MarkerReceiver : MonoBehaviour, INotificationReceiver
{
    public void OnNotify(Playable origin, INotification notification, object context)
    {
        if (notification is CustomMarker marker)
        {
            Debug.Log($"Marker triggered: {marker.message}");
        }
    }
}

2. 在Timeline中添加Marker

  • 在Marker Track上添加CustomMarker,设置message属性。
  • 绑定目标GameObject上的MarkerReceiver组件。

四、编辑器扩展(可选)

为自定义Clip添加编辑器支持:

csharp 复制代码
#if UNITY_EDITOR
[CustomEditor(typeof(EventClip))]
public class EventClipEditor : Editor
{
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(serializedObject.FindProperty("onTrigger"));
        serializedObject.ApplyModifiedProperties();
    }
}
#endif

五、实际应用示例

场景: 在Timeline播放到第5秒时生成敌人。

  1. 使用Signal系统:
  • 创建EnemySignal,在5秒位置添加SignalEmitter。

  • 在敌人生成器上配置SignalReceiver调用生成方法。

  • 使用自定义Clip:

  • 在EventClip的onTrigger事件中绑定生成敌人的方法。

  • 调整Clip长度为0,放置在5秒位置。

六、注意事项

  • 生命周期管理: 确保事件在正确的时机触发,使用OnBehaviourPlayProcessFrame
  • 性能优化: 避免在PlayableBehaviour中频繁调用昂贵操作。
  • 多轨道协调: 使用Track绑定确保不同轨道间的数据交互正确。

通过上述方法,可灵活扩展Timeline系统,满足各种事件触发需求。Signal系统适合简单事件,自定义Clip适用于复杂逻辑,而Markers则适合精确时间点触发。根据项目需求选择最合适的方式。

更多教学视频

Unity3D​www.bycwedu.com/promotion_channels/2146264125

相关推荐
yanyu-yaya2 小时前
速学兼复习之vue3章节3
前端·javascript·vue.js·学习·前端框架
智绘前端8 小时前
React 组件开发速查卡
前端·react.js·前端框架
白鳯10 小时前
分形世界:React实现交互式分形图绘制与导出
react.js·前端框架·cursor·分形·vibe coding·分形绘制·数学之美
利刃大大12 小时前
【Vue】Vue介绍 && 声明式渲染 && 数据响应式
前端·javascript·vue.js·前端框架
墨轩尘1 天前
qiankun的简单使用
前端·vue.js·前端框架
木斯佳1 天前
Vue2/Vue3 迁移头秃?Renderless 架构让组件 “无缝穿梭”
架构·前端框架
Android出海1 天前
Google Play“先试后买”功能曝光:解决买断制游戏试玩痛点
游戏·游戏开发·android开发·google play·app出海·游戏出海·android出海
壹号机长1 天前
canvas烟花特效各种前端框架都可以使用H5,vue,react,
vue.js·react.js·前端框架
Marshmallowc1 天前
React性能优化:useState初始值为什么要用箭头函数?深度解析Lazy Initialization与Fiber机制
前端·react.js·性能优化·前端框架·react hooks
云边散步1 天前
godot2D游戏教程系列一(7)
游戏开发