public static void createAnimation() { GameObject go = GameObject.Find(GAME_OBJECT_NAME); if (go != null) removeGameObjectAndComponents(go); Animator animator; go = createGameObjects(out animator); AnimationClip animationClip = new AnimationClip(); AnimationUtility.SetAnimationType(animationClip, ModelImporterAnimationType.Generic); AnimationCurve activeCurve = new AnimationCurve(); AnimationCurve positionLineCurve = new AnimationCurve(); AnimationCurve positionSmoothCurve = new AnimationCurve(); for (int i = 0; i < stepValues.Length; i++) { float time = stepTime * i; activeCurve .AddKey(KeyframeUtil.GetNew(time, stepValues[i] , TangentMode.Stepped)); positionLineCurve .AddKey(KeyframeUtil.GetNew(time, stepValues[i] + 2, TangentMode.Linear)); positionSmoothCurve.AddKey(KeyframeUtil.GetNew(time, stepValues[i] - 2, TangentMode.Smooth)); } //this will be linear curve, so need to update tangents (should be after keyframes assignments) positionLineCurve.UpdateAllLinearTangents(); animationClip.SetCurve(CUBE1_NAME, typeof(GameObject),"m_IsActive", activeCurve); animationClip.SetCurve(CUBE2_NAME, typeof(Transform),"localPosition.x", positionLineCurve); animationClip.SetCurve(CUBE2_NAME, typeof(Transform),"localPosition.y", positionSmoothCurve); AssetDatabase.CreateAsset(animationClip, ANIMATION_CLIP_PATH); AssetDatabase.SaveAssets(); AddClipToAnimatorComponent(go, animator, animationClip); }
void SetFloatCurve(AnimationCurve curve, SpriteStudioAnimePackAnimePartAnimeAttribute attribute, float fps, int frameCount, float unit = 1.0f, float baseValue = 0) { List<float> keyList = new List<float> (frameCount); SpriteStudioAnimePackAnimePartAnimeAttributeKey prevKey = null; SpriteStudioAnimePackAnimePartAnimeAttributeKey nextKey = null; for (int i = 0; i < frameCount; i++) { float value = 0; SpriteStudioAnimePackAnimePartAnimeAttributeKey key = attribute.key.FirstOrDefault (v => v.time == i); if (key != null) { value = GetValue (key); prevKey = key; } else { IEnumerable<SpriteStudioAnimePackAnimePartAnimeAttributeKey> keys = attribute.key.Where (v => v.time > i); if (keys.Count () == 0) { break; } if (prevKey == null) { prevKey = keys.First (); value = GetValue (prevKey); } else { nextKey = keys.First (); float valuePrev = GetValue (prevKey); float valueNext = GetValue (nextKey); int timePrev = prevKey.time; int timeNext = nextKey.time; float timeNormalize = (float)(i - timePrev) / (float)(timeNext - timePrev); timeNormalize = Mathf.Clamp01 (timeNormalize); switch (prevKey.ipType) { case "linear": { value = GetLinear (valuePrev, valueNext, timeNormalize); break; } case "bezier": { float curveStartTime = float.Parse (prevKey.curve.Value.Split (' ') [0]); float curveStartValue = float.Parse (prevKey.curve.Value.Split (' ') [1]); float curveEndTime = float.Parse (prevKey.curve.Value.Split (' ') [2]); float curveEndValue = float.Parse (prevKey.curve.Value.Split (' ') [3]); Vector2 start = new Vector2 ((float)timePrev, valuePrev); Vector2 controlStart = new Vector2 (curveStartTime, curveStartValue); Vector2 end = new Vector2 ((float)timeNext, valueNext); Vector2 controlEnd = new Vector2 (curveEndTime, curveEndValue); value = GetBezier (start, controlStart, end, controlEnd, timeNormalize); break; } case "hermite": { float curveStartValue = float.Parse (prevKey.curve.Value.Split (' ') [1]); float curveEndValue = float.Parse (prevKey.curve.Value.Split (' ') [3]); value = GetHermite (valuePrev, curveStartValue, valueNext, curveEndValue, timeNormalize); break; } case "acceleration": { value = GetAccelerate (valuePrev, valueNext, timeNormalize); break; } case "deceleration": { value = GetDecelerate (valuePrev, valueNext, timeNormalize); break; } default: { value = GetValue (prevKey); break; } } } } keyList.Add (value); } for (int i = 0; i < keyList.Count; i++) { float time = GetTime (i, fps); float value = keyList [i]; if (i == 0) { curve.AddKey (KeyframeUtil.GetNew (time, baseValue + value * unit, tangentMode)); } else if (i == keyList.Count - 1) { curve.AddKey (KeyframeUtil.GetNew (time,baseValue + value * unit, tangentMode)); } else { float prev = keyList [i - 1]; float next = keyList [i + 1]; if (value == prev && value == next) { continue; } curve.AddKey (KeyframeUtil.GetNew (time, baseValue + value * unit, tangentMode)); } } curve.UpdateAllLinearTangents (); }