目录
1.简介
2.Animation
3.Animator
4.ParticleSystem
1.简介
Unity中默认的动画Animation、Animator、粒子特效ParticleSystem等和时间有关的都会受到时间缩放因子(Time.timeScale)的影响。但有时候我们希望它们不受Time.timeScale影响:比如在实现一个类型“子弹时间”的效果时,UI特效是正常播放的;本文即对此做详细介绍
原理:参见上篇博文:Unity3d关于Time.timeScale对于游戏时间及Update、LateUpdate、FixedUpdate的影响测试
2.Animation
Animation需要在Update中用真实的游戏时间做动画采样
[RequireComponent(typeof(Animation))]
public class RealAnimation : MonoBehaviour
{// Fieldprivate Animation mAnim;private float time = 0;// Methodprivate void Awake(){if (mAnim == null)mAnim = gameObject.GetComponent<Animation>();}private void Update(){if (mAnim == null)return;time += Time.unscaledDeltaTime;foreach (AnimationState anim in mAnim){if (mAnim.IsPlaying(anim.name))anim.normalizedTime = time / anim.length;}mAnim.Sample();}
}
3.Animator
Animator比较简单,Unity原生支持,只需更改Animator.updateMode即可
mAnimator.updateMode = AnimatorUpdateMode.UnscaledTime;
4.ParticleSystem
ParticleSystem类似Animation需要在Update中用真实的游戏时间做动画采样
public class RealParticleSystem : MonoBehaviour
{// Fieldprivate ParticleSystem mParticle;// Methodprivate void Awake(){if (mParticle == null)mParticle = gameObject.GetComponent<ParticleSystem>();}private void Update(){if (mParticle == null)return;mParticle.Simulate(Time.unscaledDeltaTime, true, false);}
}