unity2020中timeline使用自定义轨道和PlayableTrack,PlayableGraph

一.Playable Track入门

在这里插入图片描述

在这里插入图片描述下面是一个小例子能够实现自定义Playable Track 中控制灯光的颜色:

在这里插入图片描述实现上面的效果先添加脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class LightControlBehaviour : PlayableBehaviour
{

    public Light light;
    public Color color = Color.white;
    public float intensity = 1;
    /// <summary>
    /// timeLine 每次更新都会执行此方法
    /// </summary>
    /// <param name="playable"></param>
    /// <param name="info"></param>
    /// <param name="playerData"></param>
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        if (light != null)
        {
            light.color = color;
            light.intensity = intensity;

        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class LightControlAsset : PlayableAsset
{
    // 在clip中的light对象
    public ExposedReference<Light> light;
    public Color color = Color.white;
    public float intensity = 1;
    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        // 把逻辑和资源联系起来
        var playable = ScriptPlayable<LightControlBehaviour>.Create(graph);
        var lightControlBehavliur = playable.GetBehaviour();
        // 获取到light的对象
        lightControlBehavliur.light = light.Resolve(graph.GetResolver());
        lightControlBehavliur.color = color;
        lightControlBehavliur.intensity = intensity;

        return playable;
    }
}

在这里插入图片描述

二 timeline自定义轨道

(由于PlayableTrack不能复用,不能更换绑定的数据源,所以PlayableTrack并不实用,自定义轨道更实用)
在这里插入图片描述在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
//绑定的Clip的类型(可以是定义的,也可以是AudioClip等)
[TrackClipType(typeof(LightControlAsset))]
// 绑定的Track对象的类型
[TrackBindingType(typeof(Light))]
public class LightControlTrack : TrackAsset
{
   
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class LightControlAsset : PlayableAsset
{
    // 在clip中的light对象
    //public ExposedReference<Light> light;
    public Color color = Color.white;
    public float intensity = 1;
    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        // 把逻辑和资源联系起来
        var playable = ScriptPlayable<LightControlBehaviour>.Create(graph);
        var lightControlBehavliur = playable.GetBehaviour();
        // 获取到light的对象
        //lightControlBehavliur.light = light.Resolve(graph.GetResolver());
        lightControlBehavliur.color = color;
        lightControlBehavliur.intensity = intensity;

        return playable;
    }
}
---------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class LightControlBehaviour : PlayableBehaviour
{

    //public Light light;
    public Color color = Color.white;
    public float intensity = 1;
    /// <summary>
    /// timeLine 每次更新都会执行此方法
    /// </summary>
    /// <param name="playable"></param>
    /// <param name="info"></param>
    /// <param name="playerData">就是Track中绑定的物体本身</param>
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        Light light = playerData as Light;
        if (light != null)
        {
            light.color = color;
            light.intensity = intensity;

        }
    }
}

在这里插入图片描述

三 Playable API相关介绍

在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述

四Playable创建一个简单的动画(不用Controller状态机播放)

在这里插入图片描述在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

[RequireComponent(typeof(Animator))]
public class PlayableAnimation : MonoBehaviour
{
    public AnimationClip Clip;

    private PlayableGraph _playableGraph;

    private void Start()
    {
         1. 创建 playGraph
        //_playableGraph = PlayableGraph.Create("PlayableAnimation");
         2.创建output AudioPlayableOutput
        //var playableOut = AnimationPlayableOutput.Create(_playableGraph,"Animation",GetComponent<Animator>());
         3. playables创建
        //var clipPlayable = AnimationClipPlayable.Create(_playableGraph, Clip);
         4.连接 playable 到 out
        //playableOut.SetSourcePlayable(clipPlayable);
         5. playables之间的连接

         6 播放
        //_playableGraph.Play();
        //------------以上的步骤可以由下面一句代替-------------
        AnimationPlayableUtilities.PlayClip(GetComponent<Animator>(), Clip, out _playableGraph);
    }

    private void OnDestroy()
    {
        _playableGraph.Destroy();
    }
}

五Playable创建混合动画

在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

public class PlayableBlendTree : MonoBehaviour
{
    public AnimationClip Clip1;
    public AnimationClip Clip2;
    private PlayableGraph _playableGraph;
    private AnimationMixerPlayable _mixPlayable;
    [Range(0,1)]
    public float weight = 0.5f;
    void Start()
    {
        // 创建 graph
        _playableGraph = PlayableGraph.Create("PlayableBlendTree");
        // 创建 out
        var playableOut = AnimationPlayableOutput.Create(_playableGraph,"Animation",GetComponent<Animator>());
        // 创建 mixPlayable
        _mixPlayable = AnimationMixerPlayable.Create(_playableGraph, 2);
        // 将graph 和 playable 绑定
        playableOut.SetSourcePlayable(_mixPlayable);
        // 创建clipPlayable
        var clipPlayable0 = AnimationClipPlayable.Create(_playableGraph, Clip1);
        var clipPlayable1 = AnimationClipPlayable.Create(_playableGraph, Clip2);

        _playableGraph.Connect(clipPlayable0,0,_mixPlayable,0);
        _playableGraph.Connect(clipPlayable1, 0, _mixPlayable, 1);
        // 播放
        _playableGraph.Play();
    }

    // Update is called once per frame
    void Update()
    {
        _mixPlayable.SetInputWeight(0, weight);
        _mixPlayable.SetInputWeight(1, 1 - weight);
    }
    private void OnDestroy()
    {
        _playableGraph.Destroy();
    }
}

六 PlayableGraph 运行时动态修改(添加或删除clip)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

public class PlayableBlendTree : MonoBehaviour
{
    public AnimationClip Clip1;
    public AnimationClip Clip2;
    private PlayableGraph _playableGraph;
    private AnimationMixerPlayable _mixPlayable;
    [Range(0,1)]
    public float weight = 0.5f;
    void Start()
    {
        // 创建 graph
        _playableGraph = PlayableGraph.Create("PlayableBlendTree");
        // 创建 out
        var playableOut = AnimationPlayableOutput.Create(_playableGraph,"Animation",GetComponent<Animator>());
        // 创建 mixPlayable
        _mixPlayable = AnimationMixerPlayable.Create(_playableGraph, 2);
        // 将graph 和 playable 绑定
        playableOut.SetSourcePlayable(_mixPlayable);
         创建clipPlayable
        //var clipPlayable0 = AnimationClipPlayable.Create(_playableGraph, Clip1);
        //var clipPlayable1 = AnimationClipPlayable.Create(_playableGraph, Clip2);
         playable之间的连接
        //_playableGraph.Connect(clipPlayable0,0,_mixPlayable,0);
        //_playableGraph.Connect(clipPlayable1, 0, _mixPlayable, 1);
        // 播放
        _playableGraph.Play();
    }

    // Update is called once per frame
    void Update()
    {
        _mixPlayable.SetInputWeight(0, weight);
        _mixPlayable.SetInputWeight(1, 1 - weight);

        if (Input.GetKeyDown(KeyCode.A))
        {
             创建clipPlayable
            var clipPlayable0 = AnimationClipPlayable.Create(_playableGraph, Clip1);
            var clipPlayable1 = AnimationClipPlayable.Create(_playableGraph, Clip2);
            // playable之间的连接
            _playableGraph.Connect(clipPlayable0, 0, _mixPlayable, 0);
            _playableGraph.Connect(clipPlayable1, 0, _mixPlayable, 1);
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            // 1. 先断开连接
            _playableGraph.Disconnect(_mixPlayable, 0);
            _playableGraph.Disconnect(_mixPlayable, 1);
            // 2.后销毁
            _mixPlayable.Destroy();
            _playableGraph.DestroyPlayable(_mixPlayable);
            // 最后销毁output       如果只有一个Graph,默认是 0
            _playableGraph.DestroyOutput(_playableGraph.GetOutput(0));

        }
    }
    private void OnDestroy()
    {
        _playableGraph.Destroy();
    }
}

### 更改 Unity 中动画播放速度的方法 #### 使用 Animator 组件调整整体播放速度 当需要在运行时动态调整整个 `Animator` 的播放速度时,可以通过设置 `Animator.speed` 属性来实现。此属性影响所有处于活动状态下的动画片段的播放速率。 ```csharp // C# 代码示例:调整 Animator 整体播放速度 public class AdjustAnimationSpeed : MonoBehaviour { public float newSpeed = 1.0f; private Animator animator; void Start() { animator = GetComponent<Animator>(); animator.speed = newSpeed; // 设置新的播放速度 } } ``` 这种方法适用于希望统一改变角色或物体上所有动画的表现情况[^2]。 #### 针对特定状态调整播放速度 对于想要单独控制某一个具体动画状态的情况,在默认情况下直接通过 API 调整单个状态的速度并不被支持。不过,可以采用间接的方式达成目的——即检测当前是否进入了目标状态并相应地调整 `Animator` 的全局速度;一旦退出该状态再恢复原来的速度设定。 ```csharp // C# 代码示例:针对特定状态调整播放速度 void Update() { if (animator.GetCurrentAnimatorStateInfo(0).IsName("Target State")) { originalSpeed = animator.speed; animator.speed = specialSpeedForThisState; } else { animator.speed = originalSpeed; } } ``` 这种方式允许开发者灵活应对不同场景的需求,而无需每次都重新配置复杂的参数。 #### 利用 Timeline 系统自定义播放速度 如果项目中使用UnityTimeline 工具来进行复杂序列化叙事或者高级时间线管理,则有更多选项可用于定制动画行为: - **编辑器内操作**:可以在 Editor 中右键点击轨道选择 "Convert To Clip Track" 后手动输入 Speed Multiplier 值。 - **编程接口调用** - 对于整个 Playable Director 可以利用如下方式快速更改编播速: ```csharp // C# 代码示例:修改 Timeline 全局播放速度 var graph = director.playableGraph; if (!graph.IsValid()) return; graph.GetRootPlayable(0).SetSpeed(newGlobalSpeed); ``` - 或者精确到每一个独立轨道上的剪辑(Clip),逐一遍历并应用所需的时间缩放因子: ```csharp // C# 代码示例:修改指定轨道中的每个剪辑播放速度 foreach (var track in timelineAsset.GetOutputTracks()) { foreach (var clip in track.GetClips()) { clip.timeScale = individualClipSpeed; } } ``` 上述方法提供了极大的灵活性,使得即使是最细致入微的变化也能得到妥善处理[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值