LoginSignup
0
0

More than 1 year has passed since last update.

【Unity】ParticleSystem の再生位置を簡単に変えられる拡張メソッド

Posted at

概要

ParticleSystem のSimulate()を扱いやすくする拡張メソッドSkip()を作りました。

gif.gif

Simulate() を使うと、再生位置を進めたり開始位置から指定時間までシミュレーションを進めたりできます。
しかし、使用後に Particle が止まったりマイナスの値に対応していなかったりして不便なところがあります。
そこで、再生・停止の状態を維持してマイナスの値にも対応した拡張メソッドを作りました。

Skip()は Simulate() と比較して下記の挙動を取ります。

  • 再生・停止の状態を維持します
  • t がプラスの場合は単純にシミュレーションを進めます
  • t がマイナスの場合は Restart してシミュレーションを進め、-t 秒前の状態を再現します

また、再生・停止の状態を維持して再生位置を変えられるSetTime()も用意しています。

コード

お手持ちの ParticleSystemEx クラスに追加してご利用ください。

ParticleSystemEx.cs
using UnityEngine;

namespace Extensions
{
    public static class ParticleSystemEx
    {
        public static ParticleSystem Skip(this ParticleSystem particle, float second, bool withChildren = true, bool fixedTimeStep = true)
        {
            if (second == 0f) return particle;
            var isPlayed = particle.isPlaying;

            if (second >= 0f) particle.Simulate(second, withChildren, false, fixedTimeStep);
            else particle.Simulate(particle.totalTime + second, withChildren, true, fixedTimeStep);

            if (isPlayed) particle.Play();
            return particle;
        }
        public static ParticleSystem SetTime(this ParticleSystem particle, float time, bool withChildren = true, bool fixedTimeStep = true)
        {
            var isPlayed = particle.isPlaying;
            particle.Simulate(time, withChildren, true, fixedTimeStep);
            if (isPlayed) particle.Play();
            return particle;
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0