private void MakeAnimationCurves(GameObject root, AnimationClip animClip, Animation animation)
        {
            var acb = new AnimationCurveBuilder();

            //Set all gameobjects to inactive in first frame
            SetActiveRecursive(root.transform, false);
            root.SetActive(true);

            foreach(var mainlineKey in animation.MainlineKeys)
            {
                //Debug.Log(string.Format("Starting MainlineKey for {0} at {1} seconds", animation.Name, mainlineKey.Time));
                SetGameObjectForKey(root, animClip, mainlineKey);

                //Take a snapshot for our animation
                //AnimationMode.SampleAnimationClip(root, animClip, mainlineKey.Time);
                acb.SetCurveRecursive(root.transform, mainlineKey.Time);
            }

            acb.AddCurves(animClip);
        }
        private void MakeAnimationCurves(GameObject root, AnimationClip animClip, Animation animation)
        {
            acb = new AnimationCurveBuilder();

            //Get a list of all sprites on this GO
            var allSprites = root.GetComponentsInChildren <Transform>(true).Select(sr => AnimationUtility.CalculateTransformPath(sr.transform, root.transform));

            //Add a key for all objects on the first frame
            SetGameObjectForKey(root, animClip, animation.MainlineKeys.First(), 0);

            foreach (var mainlineKey in animation.MainlineKeys)
            {
                var visibleSprites = SetGameObjectForKey(root, animClip, mainlineKey);
                var hiddenSprites  = allSprites.Except(visibleSprites);
                HideSprites(root, hiddenSprites, mainlineKey.Time);
            }

            switch (animation.LoopType)
            {
            case LoopType.True:
                //Cycle back to first frame
                SetGameObjectForKey(root, animClip, animation.MainlineKeys.First(), animation.Length);
                break;

            case LoopType.False:
                //Duplicate the last key at the end time of the animation
                SetGameObjectForKey(root, animClip, animation.MainlineKeys.Last(), animation.Length);
                break;

            default:
                Debug.LogWarning("Unsupported loop type: " + animation.LoopType.ToString());
                break;
            }

            //Add the curves to our animation clip
            //NOTE: This MUST be done before modifying the settings, thus the double switch statement
            acb.AddCurves(animClip);

            foreach (var sprite in spriteChangeKeys)
            {
                if (sprite.Value.Count > 0)
                {
                    BuildSpriteChangeCurve(ref animClip, sprite);
                }
            }

            //Set the loop/wrap settings for the animation clip
            var animSettings = AnimationUtility.GetAnimationClipSettings(animClip);

            switch (animation.LoopType)
            {
            case LoopType.True:
                animClip.wrapMode     = WrapMode.Loop;
                animSettings.loopTime = true;
                break;

            case LoopType.False:
                animClip.wrapMode = WrapMode.ClampForever;
                break;

            case LoopType.PingPong:
                animClip.wrapMode     = WrapMode.PingPong;
                animSettings.loopTime = true;
                break;

            default:
                Debug.LogWarning("Unsupported loop type: " + animation.LoopType.ToString());
                break;
            }

            animClip.SetAnimationSettings(animSettings);

            //Debug.Log(string.Format("Setting animation {0} to {1} loop mode (WrapMode:{2}  LoopTime:{3}) ", animClip.name, animation.LoopType, animClip.wrapMode, animSettings.loopTime));
        }
Beispiel #3
0
        private void MakeAnimationCurves(GameObject root, AnimationClip animClip, Animation animation)
        {
            acb = new AnimationCurveBuilder();

            //Get a list of all sprites on this GO
            var allSprites = root.GetComponentsInChildren<Transform>(true).Select(sr => AnimationUtility.CalculateTransformPath(sr.transform, root.transform));
            
            //Add a key for all objects on the first frame
            SetGameObjectForKey(root, animClip, animation.MainlineKeys.First(), 0);

            foreach (var mainlineKey in animation.MainlineKeys)
            {
                
                var visibleSprites = SetGameObjectForKey(root, animClip, mainlineKey);
                var hiddenSprites = allSprites.Except(visibleSprites);
                HideSprites(root, hiddenSprites, mainlineKey.Time);
            }

            switch (animation.LoopType)
            {
                case LoopType.True:
                    //Cycle back to first frame
                    SetGameObjectForKey(root, animClip, animation.MainlineKeys.First(), animation.Length);
                    break;
                case LoopType.False:
                    //Duplicate the last key at the end time of the animation
                    SetGameObjectForKey(root, animClip, animation.MainlineKeys.Last(), animation.Length);
                    break;
                default:
                    Debug.LogWarning("Unsupported loop type: " + animation.LoopType.ToString());
                    break;
            }

            //Add the curves to our animation clip
            //NOTE: This MUST be done before modifying the settings, thus the double switch statement
            acb.AddCurves(animClip);

            foreach (var sprite in spriteChangeKeys)
            {
                if (sprite.Value.Count > 0)
                {
                    BuildSpriteChangeCurve(ref animClip, sprite);
                }
            }

            //Set the loop/wrap settings for the animation clip
            var animSettings = AnimationUtility.GetAnimationClipSettings(animClip);
            switch(animation.LoopType)
            {
                case LoopType.True:
                    animClip.wrapMode = WrapMode.Loop;
                    animSettings.loopTime = true;
                    break;
                case LoopType.False:
                    animClip.wrapMode = WrapMode.ClampForever;
                    break;
                case LoopType.PingPong:
                    animClip.wrapMode = WrapMode.PingPong;
                    animSettings.loopTime = true;
                    break;
                default:
                    Debug.LogWarning("Unsupported loop type: " + animation.LoopType.ToString());
                    break;
            }

            animClip.SetAnimationSettings(animSettings);

            //Debug.Log(string.Format("Setting animation {0} to {1} loop mode (WrapMode:{2}  LoopTime:{3}) ", animClip.name, animation.LoopType, animClip.wrapMode, animSettings.loopTime));
        }