Unity 使用ParticleSystem.Simulate让粒子播放不受时间缩放影响和ParticleSystem.Simulate详解

目录

前言:

代码:

Simulate详解:

1. Unity API描述:

参数:

描述:

2. Unity API的描述没太看懂,于是做了以下实验

(1).参数t

效果:

结论:

(2).参数restart :true

效果:

(3).参数restart :false

效果:

结论二:

(4).利用Simulate实现特效播放不受时间缩放影响

效果:

结论三:

(5). 参数fixedTimeStep

效果:

结论四:


前言:

    我们在unity项目中为了做一些展示效果通常会动态调整时间缩放,比如当boss入场时 我们为了给定一个boss展示时间内不受到其他因素影响 通常会把时间缩放调整为0,而boss展示时围绕着boss的特效也会随着时间的停止而暂停播放,如此就会导致boss的展示动作和展示特效不匹配的问题 那么有没有解决的办法呢?这时候就用到了ParticleSystem.Simulate。

代码:

 void Update() {
        particleSystems[0].Simulate(1, true, true);
    } 

注: 其中第二个参数控制此粒子的子对象是否也不受时间缩放影响

Simulate详解:

1. Unity API描述:

public void Simulate (float t, bool withChildren= true, bool restart= true, bool fixedTimeStep= true);

参数:

t:ParticleSystem 模拟快进的时间段(以秒为单位)。如果 restart 为 true,则将 ParticleSystem 重置为时间 0,然后从该值快进如果 restart 为 false,则 ParticleSystem 模拟将从该时间指定的当前状态快进


withChildren:同时快进所有子粒子系统


restart:重新启动并从头开始


fixedTimeStep:仅根据 Time 选项中的“固定时间”值以固定时间间隔更新系统

描述:

在给定时间段内模拟粒子以快进粒子系统,然后暂停。


2. Unity API的描述没太看懂,于是做了以下实验

(1).参数t

void Start() {
         particleSystems = this.GetComponentsInChildren<ParticleSystem>();
        particleSystems[0].Simulate(1);
}

效果:

 可见右侧粒子呈现出播放1s以后的状态

结论:

ParticleSystem.Simulate可以模拟当前粒子特效播放到某个时间的效果

(2).参数restart :true

void Start() {
        particleSystems = this.GetComponentsInChildren<ParticleSystem>();
        Invoke("Simulate", 0.5f);
    }

    void Simulate() {
        particleSystems[0].Simulate(1, false, true);
    }

效果:

 我们发现右侧粒子在播放0.5秒以后瞬间呈现出播放到1s的状态

(3).参数restart :false

void Start() {
        particleSystems = this.GetComponentsInChildren<ParticleSystem>();
        Invoke("Simulate", 0.5f);
    }

    void Simulate() {
        particleSystems[0].Simulate(1, false, false);
    }

效果:

我们发现右侧粒子在播放0.5秒以后瞬间呈现出播放到1.5s的状态

结论二:

当restart为true时 ParticleSystem.Simulate模拟当前粒子特效播放到时间t的效果
              为false时ParticleSystem.Simulate模拟当前粒子特效播放到 以播放的时间+时间t 的效果

(4).利用Simulate实现特效播放不受时间缩放影响

把时间缩放调整为0

void Start() {
        particleSystems = this.GetComponentsInChildren<ParticleSystem>();
    }


    // Update is called once per frame
    void Update() {
        particleSystems[0].Simulate(1, false, true);
    }

效果:

此时我们发现左侧粒子已经停止播放 右侧粒子不停的变化着
然后我们调整了一下速度

void Start() {
        particleSystems = this.GetComponentsInChildren<ParticleSystem>();
    }


    // Update is called once per frame
    void Update() {
        particleSystems[0].Simulate(Time.unscaledDeltaTime, false, true);
    }

 发现右侧粒子闪烁一下后消失

再次调整 关闭restart

void Start() {
        particleSystems = this.GetComponentsInChildren<ParticleSystem>();
    }


    // Update is called once per frame
    void Update() {
        particleSystems[0].Simulate(Time.unscaledDeltaTime, false, false);
    }

发现右侧粒子会在时间停止的状态下正常播放

注:开始播放的时候会有一瞬间粒子变多的问题,打印发现是Time.unscaledDeltaTime刚进入时的值有问题

如图:

之后会恢复正常

 

结论三:

通过上述方法可以在时间缩放为0时模拟特效播放效果

(5). 参数fixedTimeStep

把时间缩放调整为1并且增大fixedTime的值

void Start() {
        particleSystems = this.GetComponentsInChildren<ParticleSystem>();
    }


    // Update is called once per frame
    void Update() {
        particleSystems[0].Simulate(Time.deltaTime, false, false, true);
    }

效果:

   右边的粒子会每隔0.3秒变化一次

结论四:

fixedTimeStep为true时粒子每隔fixedtimestep时间更新一次

猜你喜欢

转载自blog.csdn.net/SmillCool/article/details/127070139