Skip to content

Instantly share code, notes, and snippets.

@Dreezn
Last active January 17, 2018 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dreezn/50914b539e54efdf62b0b0e4aeaafa72 to your computer and use it in GitHub Desktop.
Save Dreezn/50914b539e54efdf62b0b0e4aeaafa72 to your computer and use it in GitHub Desktop.
Nasty solution for resizing and moving clip support in Editor
//[Omitted...]
public class CustomTransformationTrack : TrackAsset
{
public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
{
var mixer = ScriptPlayable<CustomTransformationMixerBehaviour>.Create (graph, inputCount);
#if UNITY_EDITOR
CustomTransformationMixerBehaviour behaviour = mixer.GetBehaviour();
behaviour.CustomTrackReference = this;
#endif
foreach (var clip in GetClips())
{
var customClip = clip.asset as CustomTransformationClip;
if (customClip != null)
{
customClip.CustomClipStart = clip.start;
customClip.CustomClipEnd = clip.end;
}
}
return mixer;
}
//[Omitted...]
}
//[Omitted...]
public class CustomTransformationMixerBehaviour : PlayableBehaviour
{
private bool _firstFrameHappened;
private Quaternion _defaultRotation;
private Transform trackBinding;
#if UNITY_EDITOR
public CustomTransformationTrack CustomTrackReference { get; set; }
#endif
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
trackBinding = playerData as Transform;
if (!trackBinding)
{ return; }
int inputCount = playable.GetInputCount();
if (inputCount == 0)
{ return; }
if (!_firstFrameHappened)
{
_defaultRotation = trackBinding.rotation;
_firstFrameHappened = true;
}
#if UNITY_EDITOR
TimelineClip[] clips = CustomTrackReference.GetClips().ToArray();
#endif
double playTime = playable.GetGraph().GetRootPlayable(0).GetTime();
Quaternion rotation = Quaternion.identity;
for (int i = 0; i < inputCount; i++)
{
var inputPlayable = (ScriptPlayable<CustomTransformationBehaviour>)playable.GetInput(i);
CustomTransformationBehaviour input = inputPlayable.GetBehaviour();
//I'm just hoping 'i' matches the clip array at this point. Please DON'T do this.
#if UNITY_EDITOR
input.CustomClipStart = clips[i].start;
input.CustomClipEnd = clips[i].end;
#endif
double customDuration = input.CustomClipEnd - input.CustomClipStart;
float normalizedClipPosition = Mathf.Clamp01((float)((playTime - input.CustomClipStart) / customDuration));
float rotationToAdd = input.GetAngle(normalizedClipPosition);
rotation *= Quaternion.Euler(Vector3.right * rotationToAdd);
}
trackBinding.rotation = _defaultRotation * rotation;
}
public override void OnGraphStop(Playable playable)
{
if (trackBinding != null)
{ trackBinding.rotation = _defaultRotation; }
base.OnGraphStop(playable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment