Skip to content

Commit

Permalink
Merge pull request #56 from brunomikoski/feature/support-for-regular-…
Browse files Browse the repository at this point in the history
…animation

Feature/support for regular animation
  • Loading branch information
brunomikoski authored Sep 21, 2022
2 parents 327087f + 6717c9b commit d6cb370
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 55 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [0.5.0]
### Changed
- Added support to control AnimationSequencer though regular unity animations, in both runtime / playtime
- Fixed issue with color tweens no working on the editor > 2021
- Added a settings menu on `Edit/Preferences/AnimationSequencer`
- You can now disable the auto foldout of the steps while previewing on the Settings

## [0.4.0]
### Changed
- Fixed Unity 2020 compability
Expand Down Expand Up @@ -185,6 +192,7 @@ Thanks for all the [suggestions](https://github.com/brunomikoski/Animation-Seque
### Added
- First initial working version

[0.5.0]: https://github.com/brunomikoski/Animation-Sequencer/releases/tag/v0.5.0
[0.4.0]: https://github.com/brunomikoski/Animation-Sequencer/releases/tag/v0.4.0
[0.3.9]: https://github.com/brunomikoski/Animation-Sequencer/releases/tag/v0.3.9
[0.3.8]: https://github.com/brunomikoski/Animation-Sequencer/releases/tag/v0.3.8
Expand Down
65 changes: 33 additions & 32 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 @@ -452,7 +448,7 @@ private void PlaySequence()
}

wasShowingStepsPanel = showStepsPanel;
showStepsPanel = false;
showStepsPanel = !AnimationSequencerSettings.GetInstance().AutoHideStepsWhenPreviewing;
}

private void DrawProgressSlider()
Expand All @@ -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
18 changes: 18 additions & 0 deletions Scripts/Editor/Core/AnimationSequencerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using UnityEditor;
using UnityEngine;

namespace BrunoMikoski.AnimationSequencer
{
public sealed class AnimationSequencerSettings : ScriptableObjectForPreferences<AnimationSequencerSettings>
{
[SerializeField]
private bool autoHideStepsWhenPreviewing = true;
public bool AutoHideStepsWhenPreviewing => autoHideStepsWhenPreviewing;

[SettingsProvider]
private static SettingsProvider SettingsProvider()
{
return CreateSettingsProvider("Animation Sequencer", null);
}
}
}
3 changes: 3 additions & 0 deletions Scripts/Editor/Core/AnimationSequencerSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions Scripts/Editor/Utility/ScriptableObjectForPreferences.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace BrunoMikoski.AnimationSequencer
{
//From https://github.com/baba-s/UniScriptableObjectForPreferences/blob/master/Editor/ScriptableObjectForPreferences.cs
public abstract class ScriptableObjectForPreferences<T> : ScriptableObject
where T : ScriptableObjectForPreferences<T>
{
private static string ConfigName => typeof(T).Name;
private static T instance;

public static T GetInstance()
{
if (instance != null)
{
return instance;
}

instance = CreateInstance<T>();
string json = EditorUserSettings.GetConfigValue(ConfigName);
EditorJsonUtility.FromJsonOverwrite(json, instance);
if (instance == null)
{
instance = CreateInstance<T>();
}

return instance;
}

public static SettingsProvider CreateSettingsProvider(
string settingsProviderPath = null,
Action<SerializedObject> onGUI = null,
Action<SerializedObject> onGUIExtra = null
)
{
if (settingsProviderPath == null)
{
settingsProviderPath = $"{typeof(T).Name}";
}

T foundInstance = GetInstance();
SerializedObject serializedObject = new SerializedObject(foundInstance);
IEnumerable<string> keywords = SettingsProvider.GetSearchKeywordsFromSerializedObject(serializedObject);
SettingsProvider provider = new SettingsProvider(settingsProviderPath, SettingsScope.User, keywords);
provider.guiHandler += _ => OnGuiHandler(onGUI, onGUIExtra);
return provider;
}

private static void OnGuiHandler(Action<SerializedObject> onGUI,
Action<SerializedObject> onGUIExtra)
{
T foundInstance = GetInstance();
Editor editor = Editor.CreateEditor(foundInstance);
using (EditorGUI.ChangeCheckScope scope = new EditorGUI.ChangeCheckScope())
{
SerializedObject serializedObject = editor.serializedObject;
serializedObject.Update();
if (onGUI != null)
{
onGUI(serializedObject);
}
else
{
editor.DrawDefaultInspector();
}

onGUIExtra?.Invoke(serializedObject);
if (!scope.changed)
{
return;
}

serializedObject.ApplyModifiedProperties();
string json = EditorJsonUtility.ToJson(editor.target);
EditorUserSettings.SetConfigValue(ConfigName, json);
}
}
}
}
3 changes: 3 additions & 0 deletions Scripts/Editor/Utility/ScriptableObjectForPreferences.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
11 changes: 3 additions & 8 deletions Scripts/Runtime/Core/DOTweenActions/ColorGraphicDOTWeen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public sealed class ColorGraphicDOTWeen : DOTweenActionBase

[SerializeField]
private Color color;
public Color Color
{
get => color;
set => color = value;
}

private Graphic targetGraphic;
private Color previousColor;
Expand All @@ -40,15 +35,15 @@ protected override Tweener GenerateTween_Internal(GameObject target, float durat
previousColor = targetGraphic.color;
TweenerCore<Color, Color, ColorOptions> graphicTween = targetGraphic.DOColor(color, duration);

#if UNITY_EDITOR
#if UNITY_EDITOR
if (!Application.isPlaying)
{
// Work around a Unity bug where updating the colour does not cause any visual change outside of PlayMode.
// https://forum.unity.com/threads/editor-scripting-force-color-update.798663/
graphicTween.OnUpdate(() =>
{
targetGraphic.enabled = false;
targetGraphic.enabled = true;
targetGraphic.transform.localScale = new Vector3(1.001f, 1.001f, 1.001f);
targetGraphic.transform.localScale = new Vector3(1, 1, 1);
});
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.brunomikoski.animationsequencer",
"displayName": "Animation Sequencer",
"version": "0.4.0",
"version": "0.5.0",
"unity": "2018.4",
"description": "Animation sequencer is a way to create complex animations of sequence of events by a friendly user interface. Requires DOTween v1.2.632",
"keywords": [
Expand Down

0 comments on commit d6cb370

Please sign in to comment.