Skip to content

Commit

Permalink
add: support for regular animation
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomikoski committed Sep 11, 2022
1 parent 327087f commit e9c30f6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
63 changes: 32 additions & 31 deletions Scripts/Editor/Core/AnimationSequencerControllerCustomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private void OnEnable()
reorderableList.onRemoveCallback += OnClickToRemove;
reorderableList.onReorderCallback += OnListOrderChanged;
reorderableList.drawHeaderCallback += OnDrawerHeader;
EditorApplication.update += EditorUpdate;
EditorApplication.playModeStateChanged += OnEditorPlayModeChanged;

#if UNITY_2021_1_OR_NEWER
Expand All @@ -56,6 +57,8 @@ private void OnEnable()
Repaint();
}



public override bool RequiresConstantRepaint()
{
return true;
Expand All @@ -70,6 +73,8 @@ private void OnDisable()
reorderableList.onReorderCallback -= OnListOrderChanged;
reorderableList.drawHeaderCallback -= OnDrawerHeader;
EditorApplication.playModeStateChanged -= OnEditorPlayModeChanged;
EditorApplication.update -= EditorUpdate;

#if UNITY_2021_1_OR_NEWER
UnityEditor.SceneManagement.PrefabStage.prefabSaving -= PrefabSaving;
#else
Expand All @@ -88,6 +93,18 @@ private void OnDisable()
tweenTimeScale = 1f;
}

private void EditorUpdate()
{
if (Application.isPlaying)
return;

SerializedProperty progressSP = serializedObject.FindProperty("progress");
if (Mathf.Approximately(progressSP.floatValue, -1))
return;

SetProgress(progressSP.floatValue);
}

private void OnEditorPlayModeChanged(PlayModeStateChange playModeState)
{
if (playModeState == PlayModeStateChange.ExitingEditMode)
Expand Down Expand Up @@ -206,36 +223,15 @@ protected virtual void DrawCallbacks()
private void DrawSettings()
{
SerializedProperty autoPlayModeSerializedProperty = serializedObject.FindProperty("autoplayMode");
SerializedProperty playOnAwakeSerializedProperty = serializedObject.FindProperty("playOnAwake");
SerializedProperty pauseOnAwakeSerializedProperty = serializedObject.FindProperty("pauseOnAwake");
SerializedProperty pauseOnAwakeSerializedProperty = serializedObject.FindProperty("startPaused");

using (EditorGUI.ChangeCheckScope changedCheck = new EditorGUI.ChangeCheckScope())
{
var autoplayMode = (AnimationSequencerController.AutoplayType)autoPlayModeSerializedProperty.enumValueIndex;
AnimationSequencerController.AutoplayType autoplayMode = (AnimationSequencerController.AutoplayType)autoPlayModeSerializedProperty.enumValueIndex;
EditorGUILayout.PropertyField(autoPlayModeSerializedProperty);

string playOnAwakeLabel = null;
string pauseOnAwakeLabel = null;
switch(autoplayMode)
{
case AnimationSequencerController.AutoplayType.Awake:
playOnAwakeLabel = "Play On Awake";
pauseOnAwakeLabel = "Pause On Awake";
break;

case AnimationSequencerController.AutoplayType.OnEnable:
playOnAwakeLabel = "Play On Enable";
pauseOnAwakeLabel = "Pause On Enable";
break;

default:
Debug.LogError($"Unhandled AutoplayType {autoplayMode}");
break;
}

EditorGUILayout.PropertyField(playOnAwakeSerializedProperty, new GUIContent(playOnAwakeLabel));
if (playOnAwakeSerializedProperty.boolValue)
EditorGUILayout.PropertyField(pauseOnAwakeSerializedProperty, new GUIContent(pauseOnAwakeLabel));
if (autoplayMode != AnimationSequencerController.AutoplayType.Nothing)
EditorGUILayout.PropertyField(pauseOnAwakeSerializedProperty);

DrawPlaybackSpeedSlider();

Expand Down Expand Up @@ -469,16 +465,21 @@ private void DrawProgressSlider()

if (EditorGUI.EndChangeCheck())
{
if(!sequencerController.IsPlaying)
PlaySequence();

sequencerController.PlayingSequence.Goto(tweenProgress *
sequencerController.PlayingSequence.Duration());
SetProgress(tweenProgress);
}

GUILayout.FlexibleSpace();
}

private void SetProgress(float tweenProgress)
{
if (!sequencerController.IsPlaying)
PlaySequence();

sequencerController.PlayingSequence.Goto(tweenProgress *
sequencerController.PlayingSequence.Duration());
}

private float GetCurrentSequencerProgress()
{
float tweenProgress;
Expand Down Expand Up @@ -572,7 +573,7 @@ private void DuplicateItem(int index)
ContextClickUtils.CopyPropertyValue(sourceSerializedProperty, source);
source.serializedObject.ApplyModifiedProperties();
}

private void DrawContextInputOnItem(SerializedProperty element, int index, Rect rect1)
{
rect1.x -= 24;
Expand Down
35 changes: 21 additions & 14 deletions Scripts/Runtime/Core/AnimationSequencerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public enum PlayType
public enum AutoplayType
{
Awake,
OnEnable
OnEnable,
Nothing
}

[SerializeReference]
Expand All @@ -34,9 +35,7 @@ public enum AutoplayType
[SerializeField]
private AutoplayType autoplayMode = AutoplayType.Awake;
[SerializeField]
protected bool playOnAwake;
[SerializeField]
protected bool pauseOnAwake;
protected bool startPaused;
[SerializeField]
private float playbackSpeed = 1f;
public float PlaybackSpeed => playbackSpeed;
Expand Down Expand Up @@ -69,6 +68,10 @@ public enum AutoplayType
public bool IsPlaying => playingSequence != null && playingSequence.IsActive() && playingSequence.IsPlaying();
public bool IsPaused => playingSequence != null && playingSequence.IsActive() && !playingSequence.IsPlaying();

[SerializeField, Range(0, 1)]
private float progress = -1;


protected virtual void Awake()
{
if (autoplayMode != AutoplayType.Awake)
Expand All @@ -87,12 +90,9 @@ protected virtual void OnEnable()

private void Autoplay()
{
if (playOnAwake)
{
Play();
if (pauseOnAwake)
playingSequence.Pause();
}
Play();
if (startPaused)
playingSequence.Pause();
}

protected virtual void OnDisable()
Expand Down Expand Up @@ -276,7 +276,7 @@ public virtual Sequence GenerateSequence()
onFinishedEvent.Invoke();
}
});

for (int i = 0; i < animationSteps.Length; i++)
{
animationSteps[i].AddTweenToSequence(sequence);
Expand Down Expand Up @@ -340,12 +340,11 @@ public void SetAutoplayMode(AutoplayType autoplayType)

public void SetPlayOnAwake(bool targetPlayOnAwake)
{
playOnAwake = targetPlayOnAwake;
}

public void SetPauseOnAwake(bool targetPauseOnAwake)
{
pauseOnAwake = targetPauseOnAwake;
startPaused = targetPauseOnAwake;
}

public void SetTimeScaleIndependent(bool targetTimeScaleIndependent)
Expand All @@ -372,7 +371,15 @@ public void SetLoops(int targetLoops)
{
loops = targetLoops;
}


private void Update()
{
if (progress == -1.0f)
return;

SetProgress(progress);
}

#if UNITY_EDITOR
// Unity Event Function called when component is added or reset.
private void Reset()
Expand Down

0 comments on commit e9c30f6

Please sign in to comment.