Unity Timeline学习笔记(5) - 自定义轨道切片上变量Transform对象丢失,使用ExposedReference来解决。

问题

我在笔记(4)中后来又引用了Hierarchy中的Transform对象Transform obj,发现一些问题。

要么无法拖入进去对象,要么拖入进去保存后,再次编辑或者运行的时候发现obj丢失了。

我们还是用修改下笔记(4)的部分代码来解决。

csharp 复制代码
[DisplayName("玩家固定(Transform)Clip")]
[Serializable]
public class PlayerTransformMixerPlayableAsset : PlayableAsset
{
    [Tooltip("固定玩家的Transform")]
    public ExposedReference<Transform> fixedTransform;
    [Tooltip("结束后返回原始位置")]
    public bool FinishToBack = true;

    //public PlayerTransformBehaviour template = new PlayerTransformBehaviour();
    public ClipCaps clipCaps
    {
        get
        {
            return ClipCaps.Blending; 
        }
    }
    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        PlayerTransformBehaviour template = new PlayerTransformBehaviour();
        template.fixedTransform = fixedTransform.Resolve(graph.GetResolver());
        template.FinishToBack = FinishToBack;
        return ScriptPlayable<PlayerTransformBehaviour>.Create(graph, template);
       
    }

  
}

  
public class PlayerTransformBehaviour : PlayableBehaviour
{
    public Transform fixedTransform;  
    public bool FinishToBack = true;  
    public float passtime;  //计算当前块的播放进度
    public bool started;  //是否刚进入
}

我们需要把面板值加上ExposedReference来添加引用。

然后创建的时候通过fixedTransform.Resolve来赋值就可以了。

说明

ExposedReference 是一个泛型类型,可用于创建对场景对象的引用,以及通过使用上下文对象在运行时解析它们的实际值。ScriptableObject 或 PlayableAsset 等资源可使用它来创建对场景对象的引用。

相关推荐
小李也疯狂1 小时前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的1 小时前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y1 小时前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤1 小时前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里2 小时前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ2 小时前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎
YigAin4 小时前
Unity中的Lock,到底在锁什么,什么时候该用?
unity
Var_al4 小时前
抖小Unity WebGL分包命令行工具实践指南
unity·游戏引擎·webgl
天人合一peng6 小时前
unity 通过代码修改button及其名字字体的属性
unity·游戏引擎
GLDbalala10 小时前
Unity基于自定义管线实现经典经验光照模型
unity·游戏引擎