Skip to content

Instantly share code, notes, and snippets.

@kevincastejon
Last active March 25, 2024 16:45
Show Gist options
  • Save kevincastejon/7bc223c32344df0604d7ce7057416fda to your computer and use it in GitHub Desktop.
Save kevincastejon/7bc223c32344df0604d7ce7057416fda to your computer and use it in GitHub Desktop.
These 4 scripts make the Timeline able to handle Gameobject's parenting
using UnityEngine;
using UnityEngine.Playables;
public class SetParentBehaviour : PlayableBehaviour
{
public Transform parentObject;
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
if (parentObject == null) return;
var transform = playerData as Transform;
if (transform != null)
{
transform.SetParent(parentObject, true);
}
}
}
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
public class SetParentClip : PlayableAsset, ITimelineClipAsset
{
public ExposedReference<Transform> parentObject;
public ClipCaps clipCaps => ClipCaps.None;
public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
{
var playable = ScriptPlayable<SetParentBehaviour>.Create(graph);
var behaviour = playable.GetBehaviour();
behaviour.parentObject = parentObject.Resolve(graph.GetResolver());
return playable;
}
}
using UnityEngine.Playables;
using UnityEngine;
public class SetParentMixerBehaviour : PlayableBehaviour
{
public Transform defaultParent;
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
int inputCount = playable.GetInputCount();
Transform bindedTransform = playerData as Transform;
bool clipFound = false;
for (int i = 0; i < inputCount; i++)
{
float inputWeight = playable.GetInputWeight(i);
ScriptPlayable<SetParentBehaviour> inputPlayable = (ScriptPlayable<SetParentBehaviour>)playable.GetInput(i);
SetParentBehaviour inputBehaviour = inputPlayable.GetBehaviour();
if (inputWeight > 0.5f)
{
clipFound = true;
inputBehaviour.ProcessFrame(playable, info, bindedTransform);
}
}
if (!clipFound && bindedTransform != null)
{
bindedTransform.SetParent(defaultParent, true);
}
}
}
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
[TrackColor(0.855f, 0.8623f, 0.87f)]
[TrackClipType(typeof(SetParentClip))]
[TrackBindingType(typeof(Transform))]
public class SetParentTrack : TrackAsset
{
public ExposedReference<Transform> defaultParent; // Parent par défaut à utiliser lorsque aucun clip n'est trouvé
public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
{
var mixerPlayable = ScriptPlayable<SetParentMixerBehaviour>.Create(graph, inputCount);
var mixerBehaviour = mixerPlayable.GetBehaviour();
mixerBehaviour.defaultParent = defaultParent.Resolve(graph.GetResolver());
return mixerPlayable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment