Search Unity

Programmatically Created Sprite Animation Not Previewing Properly

Discussion in 'Scripting' started by Yinyamina, Aug 19, 2019.

  1. Yinyamina

    Yinyamina

    Joined:
    Feb 27, 2013
    Posts:
    3
    Hey everyone, I'm having a problem with an animation that I created for automating creating NPCs. Everything seems to look right. The sprites and timing are in the animation, but when I press play, it gets stuck on frame 1. When I move the SAME animation manually (No changes, I literally just move the frames forwards and then back into the proper position), it works. What is going wrong during the creation of the animation and how can I fix it?

    Here is the problem in action:


    Here is the relevant code:

    Code (CSharp):
    1. // Create Animation
    2.             AnimationClip animation = new AnimationClip();
    3.  
    4.             animation.frameRate = 4;
    5.  
    6.             Debug.Log(anim);
    7.             Debug.Log(animationSequence.Length);
    8.             EditorCurveBinding spriteBinding = new EditorCurveBinding();
    9.             spriteBinding.type = typeof(SpriteRenderer);
    10.             spriteBinding.path = "Sprite";
    11.             spriteBinding.propertyName = "m_Sprite";
    12.             ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[animationSequence.Length];
    13.  
    14.             for ( int i = 0; i < animationSequence.Length; i++ ) {
    15.                 //Debug.Log();
    16.                 spriteKeyFrames[i] = new ObjectReferenceKeyframe();
    17.                 spriteKeyFrames[i].time = i / animation.frameRate;
    18.                 spriteKeyFrames[i].value = sprites[animationSequence[i]];
    19.             }
    20.  
    21.             AnimationUtility.SetObjectReferenceCurve(animation, spriteBinding, spriteKeyFrames);
    22.             AnimationClipSettings animClipSett = new AnimationClipSettings();
    23.             animClipSett.loopTime = true;
    24.  
    25.             AnimationUtility.SetAnimationClipSettings(animation, animClipSett);
    26.  
    27.             // Add Animation File
    28.             AssetDatabase.CreateAsset(animation, path + "/" + id + " " + anim + " .anim");
    29.             AssetDatabase.SaveAssets();
    30.             AssetDatabase.Refresh();
    I followed this tutorial and made some adjustments to fit my needs:
    https://answers.unity.com/questions/1080430/create-animation-clip-from-sprites-programmaticall.html
     
    Last edited: Aug 19, 2019
  2. Yinyamina

    Yinyamina

    Joined:
    Feb 27, 2013
    Posts:
    3
  3. Yinyamina

    Yinyamina

    Joined:
    Feb 27, 2013
    Posts:
    3
    -Solved-

    The animations were being created with a stop time of 0. I added in this line

    animClipSett.stopTime = 1;


    in my animation settings and that did the trick!