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 等资源可使用它来创建对场景对象的引用。

相关推荐
ysn111113 小时前
NGUI实现反向定位到层级面板结点
unity
Thomas_YXQ10 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
KhalilRuan14 小时前
Unity-MMORPG内容笔记-其一
unity·游戏引擎
向宇it18 小时前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
切韵10 天前
Unity编辑器扩展:UI绑定复制工具
ui·unity·编辑器
11 天前
Lua复习之何为闭包
开发语言·unity·游戏引擎·lua·交互
深空数字孪生11 天前
2025年小程序地图打车的5大技术革新:实时路况预测与智能调度升级
大数据·人工智能·unity·性能优化·小程序·游戏引擎
程序猿多布11 天前
Unity Addressable使用之检测更新流程
unity·addressable
Bunny Chen12 天前
Unity中的物理单位是真实的吗?
unity·游戏引擎
benben04412 天前
Unity3D仿星露谷物语开发69之动作声音
游戏·ui·unity·c#·游戏引擎