Exemple #1
0
    private static void CreateAnimationClip(int animationId, AnimationType animationType, int frameCount)
    {
        // First you need to create e Editor Curve Binding
        EditorCurveBinding curveBinding = new EditorCurveBinding();

        // I want to change the sprites of the sprite renderer, so I put the typeof(SpriteRenderer) as the binding type.
        curveBinding.type = typeof(SpriteRenderer);
        // Regular path to the gameobject that will be changed (empty string means root)
        curveBinding.path = "";
        // This is the property name to change the sprite of a sprite renderer
        curveBinding.propertyName = "m_Sprite";

        AnimationClip animClip = new AnimationClip();

        // An array to hold the object keyframes
        ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[frameCount];
        for (int i = 0; i < frameCount; i++)
        {
            Sprite sprite = Resources.Load <Sprite>(string.Format("Fights/{0}/Fight-{0}-{1}-{2}", StringUtils.Digit3(animationId), animationType.GetHashCode(), StringUtils.Digit2(i + 1)));

            keyFrames[i] = new ObjectReferenceKeyframe();
            // set the time
            keyFrames[i].time = 0.12f * i;
            // set reference for the sprite you want
            keyFrames[i].value = sprite;
            Debug.LogWarning(sprite);
        }
        AnimationUtility.SetObjectReferenceCurve(animClip, curveBinding, keyFrames);

        if (animationType == AnimationType.Idle)
        {
            // WrapMode is not working now, using AnimationClipSettings instead
            // animClip.wrapMode = WrapMode.Loop;

            AnimationClipSettings settings = new AnimationClipSettings();
            settings.loopTime  = true;
            settings.startTime = 0;
            settings.stopTime  = animClip.length;
            AnimationUtility.SetAnimationClipSettings(animClip, settings);
        }
        AssetDatabase.CreateAsset(animClip, string.Format(@"Assets/Resources/Fights/{0}/{1}{0}.anim", StringUtils.Digit3(animationId), Enum.GetName(animationType.GetType(), animationType).ToLower()));
    }