How to change the Start color of the Particle System

不打扰是莪最后的温柔 提交于 2019-12-25 09:06:45

问题


So I am just trying to simply change the start color of a particle system via script, and it's not working.

 private ParticleSystem trailPartical;   // The  particle system

 public Color StartColor
 {
      var main = trailPartical.main;
      main.startColor = value;
 }

This is not working at all, and I have also tried the depreciated version:

 trailParticle.startColor = value;

回答1:


You're trying to use StartColor as a method, judging by the code inside the {}, even though you declared it as a variable.

Apart from this mistake, due to some changes in the ParticleSystem, you need to access the main module of the component:

ParticleSystem.MainModule main = GetComponent<ParticleSystem>().main;
main.startColor = Color.blue; // <- or whatever color you want to assign

inside a script attached to the game which also has the particle system component.




回答2:


I think I know what you are trying to do. You want to simplify setting the color with just one function or property.

You will get this error with your current code:

A get or set accessor expected.

That's because you did not implement the set accesstor.

That property should be like this:

private ParticleSystem trailPartical; 

public Color StartColor
{
    set
    {
        var main = trailPartical.main;
        main.startColor = value;
    }
}

then...

void Start()
{
    trailPartical = GetComponent<ParticleSystem>();
    StartColor = Color.red;
}

This should work.




回答3:


In case anybody wondering how to set the gradient:

ParticleSystem.MainModule psMain = GetComponent<ParticleSystem>().main;
psMain.startColor = new ParticleSystem.MinMaxGradient(Color.white, Color.red);


来源:https://stackoverflow.com/questions/42794894/how-to-change-the-start-color-of-the-particle-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!