FUtility.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace Flux
  4. {
  5. /**
  6. * @brief Runtime Utility class for Flux
  7. */
  8. public class FUtility {
  9. public const int FLUX_VERSION = 210;
  10. public static bool IsAnimationEditable( AnimationClip clip )
  11. {
  12. return clip == null || ( ((clip.hideFlags & HideFlags.NotEditable) == 0) && !clip.isLooping );
  13. }
  14. public static void ResizeAnimationCurve( AnimationCurve curve, float newLength )
  15. {
  16. float frameRate = 60;
  17. float oldLength = curve.length == 0 ? 0 : curve.keys[curve.length-1].time;
  18. // float newLength = newLength;
  19. if( oldLength == 0 )
  20. {
  21. // handle no curve
  22. curve.AddKey(0, 1);
  23. curve.AddKey(newLength, 1);
  24. return;
  25. }
  26. float ratio = newLength / oldLength;
  27. float inverseRatio = 1f / ratio;
  28. int start = 0;
  29. int limit = curve.length;
  30. int increment = 1;
  31. if( ratio > 1 )
  32. {
  33. start = limit - 1;
  34. limit = -1;
  35. increment = -1;
  36. }
  37. for( int i = start; i != limit; i += increment )
  38. {
  39. Keyframe newKeyframe = new Keyframe( Mathf.RoundToInt(curve.keys[i].time * ratio * frameRate)/frameRate, curve.keys[i].value, curve.keys[i].inTangent*inverseRatio, curve.keys[i].outTangent*inverseRatio );
  40. newKeyframe.tangentMode = curve.keys[i].tangentMode;
  41. curve.MoveKey(i, newKeyframe );
  42. }
  43. }
  44. }
  45. }