예제 #1
0
        public void CreateAnimation(ImportedAnimation anim, string basePath, string masterName, AnimationTargetObjectType targetType)
        {
            AnimationClip clip;
            // attempt to ignore characters in master name after _ character
            string newName = masterName.Split('_')[0];

            ///NOTE - DDobyns experiment to fix animation naming issues
            ///string fileName = basePath + "/" + masterName + "_" + anim.name + ".anim";   // original behavior is this line
            //string fileName = basePath + "/" + newName + "_" + anim.name + ".anim";      // DDobyns original fix is this line
            string fileName = basePath + "/" + anim.name + ".anim";      // DDobyns Feb 26 2020 fix is this line
            // this results in naming animations with the Action and Angle only. e.g., Character_run45 will save a clip named run45
            bool isLooping = anim.isLooping;

            // check if animation file already exists
            clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fileName);
            if (clip != null)
            {
                // get previous animation settings
                targetType = PreviousImportSettings.GetAnimationTargetFromExistingClip(clip);
            }
            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 = anim.frames[i].sprite;
                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 = anim.frames[anim.Count - 1].sprite;

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

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

            EditorUtility.SetDirty(clip);
            anim.animationClip = clip;
        }
        public void CreateAnimation(ImportedAnimation anim, string basePath, string masterName, AnimationTargetObjectType targetType)
        {
            AnimationClip clip;
            string        fileName  = basePath + "/" + masterName + "_" + anim.name + ".anim";
            bool          isLooping = anim.isLooping;

            // 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 = anim.frames[i].sprite;
                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 = anim.frames[anim.Count - 1].sprite;

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

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

            EditorUtility.SetDirty(clip);
            anim.animationClip = clip;
        }