public void CreateAnimation(ImportedSingleAnimationInfo anim, List <Sprite> sprites, string basePath, string masterName, AnimationTargetObjectType targetType)
        {
            AnimationClip clip;
            string        fileName  = basePath + "/" + masterName + "_" + anim.name + ".anim";
            bool          isLooping = ShouldLoop(anim.name);

            // check if animation file already exists
            clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName);
            if (clip != null)
            {
                // get previous animation settings
                targetType = PreviousImportSettings.GetAnimationTargetFromExistingClip(clip);
                isLooping  = clip.isLooping;
            }
            else
            {
                clip = new AnimationClip();
                AssetDatabase.CreateAsset(clip, fileName);
            }

            // change loop settings
            if (isLooping)
            {
                clip.wrapMode = WrapMode.Loop;
                clip.SetLoop(true);
            }
            else
            {
                clip.wrapMode = WrapMode.Clamp;
                clip.SetLoop(false);
            }

            ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[anim.Count + 1];             // one more than sprites because we repeat the last sprite

            for (int i = 0; i < anim.Count; i++)
            {
                ObjectReferenceKeyframe keyFrame = new ObjectReferenceKeyframe {
                    time = anim.GetKeyFrameTime(i)
                };

                Sprite sprite = sprites[anim.firstSpriteIndex + i];
                keyFrame.value = sprite;
                keyFrames[i]   = keyFrame;
            }

            // repeating the last frame at a point "just before the end" so the animation gets its correct length

            ObjectReferenceKeyframe lastKeyFrame = new ObjectReferenceKeyframe {
                time = anim.GetLastKeyFrameTime(clip.frameRate)
            };

            Sprite lastSprite = sprites[anim.firstSpriteIndex + anim.Count - 1];

            lastKeyFrame.value    = lastSprite;
            keyFrames[anim.Count] = lastKeyFrame;

            // save curve into clip, either for SpriteRenderer, Image, or both
            if (targetType == AnimationTargetObjectType.SpriteRenderer)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipHelper.spriteRendererCurveBinding, keyFrames);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipHelper.imageCurveBinding, null);
            }
            else if (targetType == AnimationTargetObjectType.Image)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipHelper.spriteRendererCurveBinding, null);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipHelper.imageCurveBinding, keyFrames);
            }
            else if (targetType == AnimationTargetObjectType.SpriteRendererAndImage)
            {
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipHelper.spriteRendererCurveBinding, keyFrames);
                AnimationUtility.SetObjectReferenceCurve(clip, AnimationClipHelper.imageCurveBinding, keyFrames);
            }

            EditorUtility.SetDirty(clip);
            anim.animationClip = clip;
        }
Exemple #2
0
        public void CreateAnimation(ImportedSingleAnimationInfo anim, List <Sprite> sprites, string basePath, string masterName)
        {
            AnimationClip clip;
            string        fileName = basePath + "/" + masterName + "_" + anim.name + ".anim";

            // check if animation file already exists
            clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName);
            if (clip == null)
            {
                clip = new AnimationClip();
                AssetDatabase.CreateAsset(clip, fileName);
            }

            // change loop settings
            if (ShouldLoop(anim.name))
            {
                clip.wrapMode = WrapMode.Loop;
                clip.SetLoop(true);
            }
            else
            {
                clip.wrapMode = WrapMode.Clamp;
                clip.SetLoop(false);
            }

            EditorCurveBinding curveBinding = new EditorCurveBinding
            {
                path         = "",         // assume SpriteRenderer is at same GameObject as AnimationController
                type         = typeof(SpriteRenderer),
                propertyName = "m_Sprite"
            };

            ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[anim.Count + 1];             // one more than sprites because we repeat the last sprite

            for (int i = 0; i < anim.Count; i++)
            {
                ObjectReferenceKeyframe keyFrame = new ObjectReferenceKeyframe {
                    time = anim.GetKeyFrameTime(i)
                };

                Sprite sprite = sprites[anim.firstSpriteIndex + i];
                keyFrame.value = sprite;
                keyFrames[i]   = keyFrame;
            }

            // repeating the last frame at a point "just before the end" so the animation gets its correct length

            ObjectReferenceKeyframe lastKeyFrame = new ObjectReferenceKeyframe {
                time = anim.GetLastKeyFrameTime(clip.frameRate)
            };

            Sprite lastSprite = sprites[anim.firstSpriteIndex + anim.Count - 1];

            lastKeyFrame.value    = lastSprite;
            keyFrames[anim.Count] = lastKeyFrame;

            // save animation clip values
            AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames);
            EditorUtility.SetDirty(clip);
            anim.animationClip = clip;
        }