/*
         *      get animation by name; used when creating an AnimatorOverrideController
         *      we look for similar names so the OverrideController is still functional in cases where more specific or alternative animations are not present
         *      idle <- idle
         *      idleAlt <- idle
         */
        public AnimationClip GetClipOrSimilar(string clipName)
        {
            AnimationClip clip = GetClip(clipName);

            if (clip != null)
            {
                return(clip);
            }

            List <ImportedSingleAnimationInfo> similarAnimations = new List <ImportedSingleAnimationInfo>();

            foreach (var item in animations)
            {
                if (clipName.Contains(item.name))
                {
                    similarAnimations.Add(item);
                }
            }

            if (similarAnimations.Count > 0)
            {
                ImportedSingleAnimationInfo similar = similarAnimations.OrderBy(x => x.name.Length).Reverse().First();
                return(similar.animationClip);
            }

            return(null);
        }
        public void CalculateTimings()
        {
            for (int i = 0; i < animations.Count; i++)
            {
                ImportedSingleAnimationInfo anim = animations[i];

                anim.SetFrames(frames.GetRange(anim.firstSpriteIndex, anim.Count));
            }
        }
        private void BuildIndex()
        {
            _animationDatabase = new Dictionary <string, ImportedSingleAnimationInfo>();

            for (int i = 0; i < animations.Count; i++)
            {
                ImportedSingleAnimationInfo anim = animations[i];
                _animationDatabase[anim.name] = anim;
            }
        }
Exemple #4
0
        private static bool GetAnimationsFromJSON(ImportedAnimationInfo importedInfos, JSONObject meta)
        {
            if (!meta.ContainsKey("frameTags"))
            {
                Debug.LogWarning("No 'frameTags' found in JSON created by Aseprite.");
                IssueVersionWarning();
                return(false);
            }

            var frameTags = meta["frameTags"].Array;

            foreach (var item in frameTags)
            {
                JSONObject frameTag = item.Obj;
                ImportedSingleAnimationInfo anim = new ImportedSingleAnimationInfo();
                anim.name             = frameTag["name"].Str;
                anim.firstSpriteIndex = (int)(frameTag["from"].Number);
                anim.lastSpriteIndex  = (int)(frameTag["to"].Number);

                importedInfos.animations.Add(anim);
            }

            return(true);
        }
        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 #6
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;
        }