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

相关推荐
国家一级摸鱼选手18 小时前
MCP(Model Context Protocol)学习笔记
unity·ai·mcp
会思考的猴子19 小时前
Unity3D发布后软件界面右下角出现Trial Version
unity
ellis197020 小时前
Unity资源管理框架Addressables[五] 构建
unity
派葛穆1 天前
Unity-鼠标悬停改变图像位置
unity·计算机外设·交互
avi91111 天前
图例设计软件:Focusky,VisualComponents,图片字风格化等试用和推荐,最终还是回到Unity 游戏代码
unity·ai·aigc·游戏开发·设计工具·信息图
张老师带你学1 天前
unity道具,健身房资源
科技·游戏·unity·游戏引擎·模型
废嘉在线抓狂.1 天前
简易拆开即用的高性能计时器(C#)
前端·unity·c#
ellis19701 天前
Unity资源管理框架Addressables[四] 资源分组管理
unity
张老师带你学1 天前
unity道具,哑铃架+天文望远镜,一边运动一边观星
科技·游戏·unity·模型·游戏美术
Swift社区1 天前
如果今天重新做 Claw,会用什么技术
游戏·unity