#Managing baked ParticleSystem with Unity ECS feels way too complex, am I missing something?

1 messages · Page 1 of 1 (latest)

lofty shale
#

I'm working with ECS and baked GameObjects that have ParticleSystems as children.

Everything seems linked properly (Play On Awake works, destruction works, multiple instances have their own PS). Also, instantiating multiple entities creates independent ParticleSystems, so there’s clearly some link (probably through CompanionObjects)

Now I'd like to Play/Pause these ParticleSystems from ECS at runtime: I tried adding a wrapper MonoBehaviour via AddComponentObject to access the ParticleSystem, then using a System (no job, no burst) to control them but it do not works, no error but nothing is played/paused..

ChatGPT suggested an alternative: spawn classic GameObjects manually through a Mono Manager that tracks requests from ECS entities, instead of relying on the baked ParticleSystems.

It sounds like a heavy setup just to Play/Pause VFX, and I do not want to build a legacy system like that if I just miss soething and could be done directly in ECS (or at least in a hybrid way) ?

calm pivot
#

you can access the ParticleSystem directly from a system with EntityManager.GetComponentObject<ParticleSystem>(entity) without any additional components, for example:

partial class ParticleSystemToggleSystem : SystemBase
{
    float lastChangeTime;
    float period = 2;
    bool playing;

    protected override void OnUpdate()
    {
        var now = (float)SystemAPI.Time.ElapsedTime;

        foreach(var (_, e) in SystemAPI.Query<RefRO<LocalToWorld>>().WithAll<ParticleSystem>().WithEntityAccess())
        {
            var particleSystem = EntityManager.GetComponentObject<ParticleSystem>(e);
            if(now - lastChangeTime > period)
            {
                playing = !playing;
                lastChangeTime = now;
                if(playing)
                {
                    Debug.Log($"Playing particle system");
                    particleSystem.Play();
                }
                else
                {
                    Debug.Log($"Stopping particle system");
                    particleSystem.Stop();
                }
            }
        }
    }
}
#

(the LocalToWorld is there just to have something to satisfy SystemAPI.Query syntax)

lofty shale
#

Oh !... I didn't know about GetComponentObject..

I'm not used to Query like that (I'm using mostly IJobEntity with [WithAll] etc..

#

Could I use a query like MustBePlayedTag instead of LocalToWorld ? or it have to be on WithAll ?

calm pivot
#

sure, you can use whatever component you have

lofty shale
#

My goal is like to fetch all:

  • VfxMustBePlayedTag and not VfxPlayingTag
  • VfxNotMustBePlayedTag and VfxPlayingTag

So it'll be something like:

foreach(var (_, e) in SystemAPI.Query<RefRO<VfxMustBePlayedTag>>().WithNone<VfxPlayingTag>().WithEntityAccess()) {}
foreach(var (_, e) in SystemAPI.Query<RefRO<VfxPlayingTag>>().WithNone<VfxNotMustBePlayedTag >().WithEntityAccess()) {}

?!

calm pivot
#

you won't need RefRO with tag components

#

it's just to avoid unnecessary copying of the LocalToWorld component which is quite large

#

but yes, something like that and also add .WithAll<ParticleSystem>()

#

unless you want to instantiate it of course rather than just playing/stopping

lofty shale
#

And last quesiton to be sure to understand, the entity e I'll get is the child entity that baked and contain the ParticleSystem ?
For now the tags are on the root entity. But I can ref the particuleSystem entity and tags them instead

lofty shale
calm pivot
#

you'll have to have the tags on the same GameObject as the ParticleSystem

#

each baked GameObject becomes its own entity, and each iteration of the Query loop concerns exactly one entity

lofty shale
#

I think I got it, the missing point was "GetComponentObject" here and I was perturbed by the query builder that I'm not used too but I got it

#

I'll try it and come back to Makr as Solved if it works as intereded 🙂
Thank again

lofty shale
#

I worked like a charm.... So easier than my solution. Thank a lot ! 🙂