时间膨胀下的动画

时间膨胀下的动画

原理请看上一篇文章《时间膨胀》,这里只粘贴代码

 
 
  1. public static class MLAnimationExt
  2. {
  3. public static IEnumerator Play(this Animation animation, string clipName, bool useTimeScale, Action onComplete)
  4. {
  5. //We Don't want to use timeScale, so we have to animate by frame..
  6. if (!useTimeScale)
  7. {
  8. AnimationState _currState = animation[clipName];
  9. bool isPlaying = true;
  10. float _startTime = 0F;
  11. float _progressTime = 0F;
  12. float _timeAtLastFrame = 0F;
  13. float _timeAtCurrentFrame = 0F;
  14. float deltaTime = 0F;
  15. animation.Play(clipName);
  16. _timeAtLastFrame = Time.realtimeSinceStartup;
  17. while (isPlaying)
  18. {
  19. _timeAtCurrentFrame = Time.realtimeSinceStartup;
  20. deltaTime = _timeAtCurrentFrame - _timeAtLastFrame;
  21. _timeAtLastFrame = _timeAtCurrentFrame;
  22. _progressTime += deltaTime;
  23. _currState.normalizedTime = _progressTime / _currState.length;
  24. animation.Sample();
  25. //Debug.Log(_progressTime);
  26. if (_progressTime >= _currState.length)
  27. {
  28. //Debug.Log("Bam! Done animating");
  29. if (_currState.wrapMode != WrapMode.Loop)
  30. {
  31. //Debug.Log("Animation is not a loop anim, kill it.");
  32. //_currState.enabled = false;
  33. _currState.normalizedTime = 0.99f;
  34. animation.Sample();
  35. isPlaying = false;
  36. }
  37. else
  38. {
  39. //Debug.Log("Loop anim, continue.");
  40. _progressTime = 0.0f;
  41. }
  42. }
  43. yield return new WaitForEndOfFrame();
  44. }
  45. yield return null;
  46. if (onComplete != null)
  47. {
  48. onComplete();
  49. }
  50. }
  51. else
  52. {
  53. animation.Play(clipName);
  54. }
  55. }
  56. }

再来看他的应用:

 
 
  1. public class anim_no_timescale : MonoBehaviour {
  2. public AnimationClip anim;
  3. // Use this for initialization
  4. void Start () {
  5. Animation animation = this.gameObject.GetComponent<Animation>();
  6. StartCoroutine(animation.Play(anim.name, false, delegate()
  7. {
  8. Debug.Log("播放完毕");
  9. }));
  10. }
  11. // Update is called once per frame
  12. void Update () {
  13. }
  14. }

详细工程,请点击这里:https://github.com/badforlabor/test2.git 中的 test_timescale 文件夹





转载于:https://www.cnblogs.com/badforlabor/p/5512000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值