问题
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