更改起始颜色后,粒子系统颜色为粉红色

Particle System color is pink when Start Color is changed

我正在制作打砖块游戏,我想显示与被球击中的砖块颜色相同的粒子。 这是我的代码:

GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
ParticleSystem ps = smokePuff.GetComponent<ParticleSystem>();
ParticleSystem.MainModule psmain = ps.main;
psmain.startColor = gameObject.GetComponent<SpriteRenderer> ().color;

这不起作用,粒子颜色显示为粉红色。如何解决?

我正在使用 Unity 5.6。

这是某些特定版本的 Unity 上的错误。它应该在 Unity 2017.2 中修复。发生的情况是,当您更改 ParticleSystem 颜色时,它会丢失其 material 引用。

您可以将 Unity 更新到最新版本,或者在设置颜色后手动将 material 引用或新的 material 附加回 ParticleSystem

public GameObject smoke;

void Start()
{
    GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
    ParticleSystem ps = smokePuff.GetComponent<ParticleSystem>();
    ParticleSystem.MainModule psmain = ps.main;
    psmain.startColor = gameObject.GetComponent<SpriteRenderer>().color;


    //Assign that material to the particle renderer
    ps.GetComponent<Renderer>().material = createParticleMaterial();
}

Material createParticleMaterial()
{
    //Create Particle Shader
    Shader particleShder = Shader.Find("Particles/Alpha Blended Premultiply");

    //Create new Particle Material
    Material particleMat = new Material(particleShder);

    Texture particleTexture = null;

    //Find the default "Default-Particle" Texture
    foreach (Texture pText in Resources.FindObjectsOfTypeAll<Texture>())
        if (pText.name == "Default-Particle")
            particleTexture = pText;

    //Add the particle "Default-Particle" Texture to the material
    particleMat.mainTexture = particleTexture;

    return particleMat;
}

编辑:

关于创建粒子系统和粉红色粒子问题需要了解的两件事:

1。如果您从 Component 创建粒子系统 ---> Effects ---> Particle System菜单,Unity 将 not 附加 material 到粒子系统,因此它会是粉红色的。您将必须使用上面的代码来创建新的 material 或从编辑器中手动创建。如果不这样做,您将获得粉红色的 ParticleSystem。

你的问题要么是这个问题要么是我上面描述的参考错误

2。如果您从 GameObject 创建粒子系统 ---> Effects ---> 粒子系统菜单,Unity 将创建新的 GameObject,为其附加一个 Particle System 和一个 material。你不应该有粉红色粒子问题除非它是我谈到的错误,当颜色被修改时粒子失去material参考。