在Unity的timeline相关自定义代码编写中,有时候会需要在PlayableBehaviour 中获取TimeClip 的开始和结束时间,通过PlayableBehaviour 提供的接口我们会发现找不到对应的属性或者方法。
这篇回答中有详细解释:How to access the clip timing (start, end time) in PlayableBehaviour functions
这里附代码实现方式,需要在Track、Asset和Playable通过三次传递:
[TrackClipType(typeof(XXXControlAsset))]
public class XXXControlTrack : TrackAsset
{
public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
{
foreach (var clip in GetClips())
{
var customClip = clip.asset as H2StoryControlAsset;
if (customClip != null)
{
customClip.m_CustomClipStart = clip.start;
customClip.m_CustomClipEnd = clip.end;
}
}
return ScriptPlayable<XXXPlayable>.Create (graph, inputCount);
}
}
public class XXXControlAsset : PlayableAsset
{
[HideInInspector]
public double m_CustomClipStart;
[HideInInspector]
public double m_CustomClipEnd;
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
{
XXXPlayable template = new XXXPlayable()
{
m_CustomClipStart = m_CustomClipStart,
m_CustomClipEnd = m_CustomClipEnd,
};
return ScriptPlayable<XXXPlayable>.Create(graph, template);
}
}
public class XXXPlayable : PlayableBehaviour
{
public double m_CustomClipStart;
public double m_CustomClipEnd;
}
|