Last active
January 17, 2018 19:02
-
-
Save Dreezn/50914b539e54efdf62b0b0e4aeaafa72 to your computer and use it in GitHub Desktop.
Nasty solution for resizing and moving clip support in Editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[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