Timeline 自定义轨道

timeline 的基本构成

Data :PlayableBehaviour    

Clip:PlayableAsset

Mixier:PlayableBehaviour

Data 是数据   clip   是Asset 类型轨道那一段戏    

Mix只要是这条轨道就会控制    Track 是轨道

首先在数据里面定义一个数据 

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

/// <summary>
/// 存放需要更改数据
/// </summary>
public class Data :PlayableBehaviour   
{
    public Transform endpos; //结束的位置
    //updata
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        Transform action = playerData as Transform;

        action.position = Vector3.Lerp(action.position,endpos.position,info.deltaTime);

    }
}

然后定义一个 剧本  要放入一个数据  再返回一个数据  clip

用工厂生产一个playable 

我们可以看到继承 PlayableAsset  里面有一个抽象的方法 这个可以看到是一个工厂模式

我们要生成一个  playerable 返回 这个时候我们要去用工厂创建

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

public class Clip : PlayableAsset,ITimelineClipAsset
{

    private Data template=new Data();

    //暴露引用
   public ExposedReference<Transform> endpos;

    public ClipCaps clipCaps {

        get { return ClipCaps.None; }//不可以融合
    
    }



    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        var playable = ScriptPlayable<Data>.Create(graph, template);
        //取出一个具体位置返回
        Data clone = playable.GetBehaviour();
        clone.endpos = endpos.Resolve(graph.GetResolver());
        return playable;
    }

  
}

这个有一个问题    为什么要设置暴露引用

我们开始时候我们不知道终点在那里 我们要克隆一个出来   clone

我们获得需要配置的数据  

Data 里面定义的其实就是声明的 赋值就在clip 赋值

相信大家还有一个问题为啥不知接声明一个 位置 直接返回

Asset 是不能转为 Behaviour    

但是 Behaviour     可以转换为 Asset   BehaviourAsset

Asset 可以转换为 BehaviourAsset  BehaviourAsset转换为 Behaviour   

之后我们自定义轨道

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;

// 定义轨道颜色
[TrackClipType(typeof(Clip))] //定义轨道类型
[TrackBindingType(typeof(Transform))] //绑定属性的类型

public class Track : TrackAsset
{
   
    //轨道
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

就可以执行了

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值