Particle System preview scrubbing by script

I want to reproduce the behaviour of the Particle System where one can change the time of the ParticleSystem and see it at particular playback positions.

This can be done with the “Playback Time” but I need to do it by script.
3758326--313234--upload_2018-10-6_20-0-50.png

I only need this to work in editor mode and not in playmode.
I tried using ParticleSystem.Simulate and ParticleSystem.time to no avail.
How can I set the Playback Time value of that popup window by script?

// my failed attempts
if (Ps != null)
{
    // Ps.Simulate(adjustedTime, true, false);
    Ps.Pause(true);
    Ps.time = adjustedTime;
}

ps.Simulate is correct. Be sure to set the restart param.

1 Like

Ah yes, the restart parameter I had forgotten about that. :wink:

I thought the script would allow to preview properly but something’s amiss.
An issue I found was due to the particles having a different seed @ every simulation. But there still are deeper problems and I really hope you can take a look at this: I provided a package of the example I show in the video.

Here’s a video of the difference between clicking on the Particle System’s Restart button and the ParticleSystem.Simulate by script.
As you can see the 1st particle isn’t disappearing and the blue one can’t even be seen going up.

https://www.youtube.com/watch?v=gb8K_D6fCKs

Why is it behaving like that?

using UnityEngine;

[RequireComponent(typeof(ParticleSystem))]
[ExecuteInEditMode]
public class TestAnimate : MonoBehaviour
{
    [Range(0,3f)] public float Timeline;
    private ParticleSystem Ps;

    void OnValidate()
    {
        Ps = GetComponent<ParticleSystem>();
        Ps.Simulate(Timeline, true, true);
        Ps.Pause(true);
        Ps.time = Timeline;
    }
}

3761137–313675–SimulatePackage.unitypackage (18.7 KB)

Instead of:

Ps.Simulate(Timeline, true, true);
Ps.Pause(true);
Ps.time = Timeline;

Try:

Ps.Simulate(Timeline, true, true);

And you’ll also need to control the seed yourself in your script:

var main = Ps.main;
main.randomSeed = 5; // enter your seed here. maybe generate using Random API

However, looking at the video, it’s possible you’re hitting this long-standing bug… Unity Issue Tracker - [Particles] Particle system glitches when using Simulate from script, after the first loop
The bug report for that has a similar looking stuttering behavior.

When I’m moving the playback time when the particle system is paused, it works perfectly.
It means that the Editor is calling something that allows to preview without bugs, I just need to call the same thing then.

What is the editor calling when modifying this value?
Or how can I set that value to something I want through script?
I only need this to work in the editor and not in playmode.

3762817--313930--upload_2018-10-8_16-20-14.png

It calls internal API that you don’t have access to, sorry :frowning:

However, if you are really determined, there are ways to call internal API. If you want to go down that route, here is what you need to call:

ParticleSystemEditorUtils.playbackTime = playbackTime;
ParticleSystemEditorUtils.PerformCompleteResimulation();

This can only work if the system in question is also selected, because it operates on the set of selected systems.

I’m not advocating the use of the following, but with a bit of googling, these came up:
https://gist.github.com/MattRix/9205bc62d558fef98045
https://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method

Obviously, internal methods may also change without warning in future versions of Unity.

Sorry I don’t have a good answer for this. I’ll try find time to look at that bug again in the near future… it’s literally the oldest open Particle System bug… we haven’t managed to find a fix for it (yet).

1 Like

Thanks a lot for answering all this Richard, it’s much appreciated!

I will have to use the reflection temporarily until you find out how to fix that bug because my vfx artist needs that previewer asap. However, I haven’t used reflection yet so do you know why it’s not finding the PsUtils in the SetPlaybackTime() method here?

using System.Reflection;
using UnityEngine;

[RequireComponent(typeof(ParticleSystem))]
[ExecuteInEditMode]
public class TestAnimate : MonoBehaviour
{
    [Range(0,3f)] public float Timeline;
    private ParticleSystem Ps;

    protected static MethodInfo Resimulation;
    protected static FieldInfo PlaybackTimeField;

    void SetPlaybackTime(float _time)
    {
        if (PlaybackTimeField == null || Resimulation == null)
        {
            var PsUtils = typeof(UnityEditor.EditorUtility).Assembly.GetType("ParticleSystemEditorUtils", true);
            PlaybackTimeField = PsUtils.GetField("playbackTime", BindingFlags.Static | BindingFlags.NonPublic);
            Resimulation = PsUtils.GetMethod("PerformCompleteResimulation", (BindingFlags.Static | BindingFlags.NonPublic));
        }
        PlaybackTimeField.SetValue(null, _time);
        Resimulation.Invoke(null, null);
    }

    void OnValidate()
    {
        Ps = GetComponent<ParticleSystem>();

        SetPlaybackTime(Timeline);
    }
}

If I remember correctly, the GetType needs to be prefixed with the assembly name, in the string. I.e “UnityEditor.ParticleSystemEditorUtils”

But I’m no expert with this stuff…

1 Like

Are there any plans to fix the long standing simulate bug in the unity 2019 cycle? My project is totally dependent on this working, been waiting a while, it’d be nice to know if I should just scrap it and move on.

There are no plans… all I can say is that I can try find time to take another look at it. Sorry it’s causing you problems.

Also an annoyance for us. It ate quite a few hours and I was beginning to suspect I was going insane for a bit.
I used a pile of hacks to get it somewhat working, but would appreciate a lot if it was officially fixed.

1 Like

Thanks for the reply, I’m quite sad this isn’t at least in the pipeline since it’s over 3 years now. Would really appreciate anything you can do.

@dpizzle @SugoiDev can you confirm whether this statement from the original post is also true for you?

I ask because, looking at the repro project for the bug we have (https://issuetracker.unity3d.com/issues/particle-system-simulate-issues) the steps demonstrate the issue in a playmode scenario. (and thus any fix would only be guaranteed to fix the playmode issue in that project, and might miss an editmode isuse, if there is one)

If you have an editmode scenario, please report it as a separate bug and reply here with the case number so I can track it.

I only need play mode bug fixed. I do not need editor mode fixed. Sorry, I posted on this thread because it seemed most active and relevant. Many thanks for looking into it.

1 Like

We are currently testing a fix for this issue which aims to resolve it in Play Mode (and players).

To reiterate about Edit Mode: the bug report doesn’t show an Edit Mode problem and we don’t know of any problem with this in Edit Mode, so, if you’re reading this and thinking “Hey, I get this bug in Edit Mode!”, then please report a bug for us and we’ll investigate it :slight_smile:

There’s always been something that seemed more important (and arguably there still is!) but you’re right that it’s been a long time (too long, probably!) and we want to be able to help you out, so we’ve diverted a bit of time towards trying to figure this one out. Hopefully it’s a good fix and will pass testing.

First, thanks Richard for looking deeper into this issue.
I never managed to use the trick with UnityEditor.ParticleSystemEditorUtils you had once mentioned.

Could it be that the play and editor mode bugs are one and the same?
There shouldn’t be a doubt as to whether there is a bug in the editor mode with what I showed in the video above by the way. I could make a project with the scripts and post them as a bug if they are indeed different bugs.

Can we expect a bugfix for Unity 2018?

I don’t think so, because the fix I’ve applied should (in theory) only affect playmode.

Ah, good point! I had forgotten about that. I’ve just loaded it up and yes you’re right, it reproduces what I think is a different issue. I’m currently on a version of 2019.1 one so i can’t say right now if the other fix would fix this too (thougth I’m still speculating that it won’t)

I think what you have in your video is probably different to what’s currently logged, but I’ll check for sure next week, before asking you to do anything. I can log it myself, to be honest, as it’s so simple, it just depends if you want to benefit from the bug notifications by logging it yourself.

Hard to say… it has quite a few votes on the issue tracker. It might make the cut.

1 Like

I’ll leave it to you then ;). Good luck with the bugs, squash em good.

1 Like

Thanks Richard, you’re the best! Fingers crossed it passes testing…

Hey, another update - the editmode repro did indeed still fail. I now have a fix for that too, and we are beginning testing of the changes. After that, assuming all goes well, we will look at what versions we can get this into.

2 Likes