EditorAnimationCurveExtension

Author: Nikolay Kandalintsev (nicloay)

Description
Unity has AnimationCurve class to work with animation.

Unfortunately you can set only smooth keyframes to it with left and right tangents through your code. But in the inspector you have 2 more options - Constant and Linear Screenshot bellow show difference between them.

I used several methods writen by Unity guys. This methods highlighted by UnityEditor.CurveUtility.cs (c) Unity Technologies comment.



This utility solve this problem, you can find example how to create Linear and Constant animation

Usage
Place following scripts somewhere in your Assets folder under your Poroject

C# - CurveExtension.cs
This class used to add 2 new methods to AnimationCurve This methods used when you create Linear curve to update left and right tangents of keys.

C# - KeyframeUtil.cs
Use KeyframeUtil.GetNew(....) to create new keyframes with left and right TangentMode

Example
I will not show here how to save object to AssetDatabase, Assume that you already have Animator component and AnimationClip. See the full source code at the end of this page.

Constant curve
This type of curve is very useful for m_IsActive properties (to enable and disable gameobjects)

Example Full Source Code C# - CreateAnimation.cs
This is editor class so place this code to any Editor folder inside your Assets folder

After that you will have new menu item Assets/animation

When you click on it, following script will create gameobjects on opened scene, AnimatorController and Animation assets in the Assets folder