为什么不能在Start()方法之外使用ParticleSystem.Emit()?

时间:2019-09-10 20:07:06

标签: c# unity3d particle-system

我正在尝试使用ParticleSystem.Emit()动态发射粒子,并且无法让Emit()Start()方法之外工作。

我能够直接或单独使用Start()方法发射粒子。尝试在Update()LateUpdate()FixedUpdate()中执行相同操作时,它将不再起作用。

没有错误报告。我报告了更新方法中的粒子数量,以确保其中的代码确实在运行,并且它报告了我在Start()方法中发出的粒子数量。

当尝试在更新方法中发射粒子时,我尝试过:

  1. 直接在更新循环中使用Emit()
  2. 使用直接在更新循环中调用的单独方法。
  3. 在更新循环中使用称为Invoke()的单独方法。

我能够检查和更改Update()中的粒子位置,因此我认为我没有某种示波器问题。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    public ParticleSystem aSystem;
    private ParticleSystem.Particle[] aParticle;

    void Start()
    {
        aParticle = new ParticleSystem.Particle[aSystem.main.maxParticles];

        // Emit particle.
        DoEmit();

        Debug.Log("Particle Count: " + aSystem.particleCount);
    }

    void Update()
    {
        int numParticles = aSystem.GetParticles(aParticle);
        Debug.Log("Number of particles: " + numParticles);
        for (int i = 0; i < numParticles; i++)
        {
            if (aParticle[i].position.z > 1)
            {
                aParticle[i].position = new Vector3(0f,0f,0f);
            }
            aParticle[i].velocity = new Vector3(0f, 0f, 1f);
        }
        aSystem.SetParticles(aParticle);

        // Emit particle.
        DoEmit();
    }

    void DoEmit()
    {
        // Configure render settings for particle.
        ParticleSystemRenderer aRenderer = aSystem.GetComponent<ParticleSystemRenderer>();
        aRenderer.renderMode = ParticleSystemRenderMode.Mesh;
        aRenderer.mesh = Resources.Load<Mesh>("vector");

        // Configure rest of settings and emit.
        var emitParams = new ParticleSystem.EmitParams();
        emitParams.startLifetime = 120;
        emitParams.position = new Vector3(0.0f, 0.0f, 0.0f);
        emitParams.velocity = new Vector3(0.0f, 0.0f, 0.0f);
        emitParams.startSize = 1;
        aSystem.Emit(emitParams, 1);
        aSystem.Play(); // Continue normal emissions
        Debug.Log("DoEmit() called!");
    }
}

预期结果:沿+ z方向移动的粒子流。

实际结果:一个粒子沿+ z方向移动。

1 个答案:

答案 0 :(得分:1)

我需要做一些类似的事情,并发现 this answer in the Unity forums 为我提供了一个很棒、可靠的解决方案,我已经多次重复使用并针对我的目的进行了修改。它是一个粒子池,您可以根据需要从中发射一个或多个粒子,杀死最后制作的粒子并在达到最大粒子数后重新使用它。我认为它完全符合您的要求。我将在此处提供相关脚本以供参考(为了清楚起见,还附有 @Artifact-Jesse 的丰富多彩的评论)。

请注意,此脚本是为 Unity 2018.x 编写的,但从 2020.2.1 开始仍然运行良好

Artifact-Jesse 的一个相当通用的粒子池的完整功能示例:

using UnityEngine;

[RequireComponent(typeof(ParticleSystem))]
public class ParticlePool : MonoBehaviour
{

 private int lastParticleIndex = 0;  // keeps track of our oldest particle (for deletion)

 // these will all be inited in Initialize() on Start()
 private ParticleSystem particleSys;             // our object's particle system
 private ParticleSystem.Particle[] particles;    // our reusable array of particles
 private ParticleSystem.EmitParams emitParams;   // reusable emitparams
 private int maxParticles = 0;       // total number of particles in our scene before re-using


 private void Awake()
 {
     Initialize(); // initialize all of our member variables
 }



 public void CreateParticle(Vector3 position, float size, Vector3 velocity, float angularVelocity)
 {
     // if we're at our particle count limit, kill our oldest particle.

     int activeParticles = particleSys.GetParticles(particles);
     /// this thing sucks. Read the Unity docs VERY carefully to understand...
     /// basically the parameter (particles) is an out parameter which will
     /// write out the existing particles in the particle system to our
     /// reusable array. After that, we can directly modify the particles
     /// and then when we're done, write the particles back into the
     /// particle system with ParticleSystem.SetParticles( particles, count )
     
     if (activeParticles >= maxParticles)
     {
         // set lifetime to -1 to kill the particle
         particles[lastParticleIndex].remainingLifetime = -1;
         // we need to reset start lifetime to a normal value, too or the particle will still have infinite lifetime
         particles[lastParticleIndex].startLifetime = 1;

         lastParticleIndex++;    // keep track of oldest particle
         if (lastParticleIndex >= maxParticles) lastParticleIndex = 0;

         // have to re-write
         particleSys.SetParticles(particles, particles.Length);  // write those pesky particles back into our ParticleSystem
     }


     // set up params for this particle, you can use whatever you want (see unity docs for EmitParams for what's available)
     emitParams.angularVelocity = angularVelocity;
     emitParams.position = position;
     emitParams.startSize = size;
     emitParams.velocity = velocity;
     emitParams.startLifetime = float.MaxValue; // float.MaxValue makes the particle's lifetime infinite

     particleSys.Emit(emitParams, 1);
     particleSys.Play();
 }


 void Initialize()
 {
     if (particleSys == null)
         particleSys = GetComponent<ParticleSystem>();

     maxParticles = particleSys.main.maxParticles;

     if (particles == null || particles.Length < particleSys.main.maxParticles)
         particles = new ParticleSystem.Particle[particleSys.main.maxParticles];
 }

}

注意:这个例子中的粒子系统只是带有渲染器的最小化。不循环,清醒时不开始,模拟空间设置为世界。 MaxParticles 受上述脚本的约束。