How to modify ParticleSystem from Editor script?

Exposed ParticleSystem API is very limitted (Unity - Scripting API: ParticleSystem). For example you can not access “Size over lifetime” or “Rotation over lifetime” or simply set lifetime as “random between two constants” from script.

Has anyone found a way to do that? Through reflection? Through some hack using the inspector script?

It looks like it’s possible, it’s just not very straight forward. You can modify private variables using this trick:

var so : SerializedObject = new SerializedObject(Selection.activeGameObject.GetComponent(Renderer));
so.FindProperty("m_ScaleInLightmap").floatValue = 0.9;

You can iterate all serializable properties using this loop:

SerializedObject so = new SerializedObject(particleSystem);
SerializedProperty it = so.GetIterator();
while (it.Next(true))
	Debug.Log (it.propertyPath);

After that you will notice that ParticleSystem has a bunch of properties like InitialModule, SizeModule, etc. Each sub-property like InitialModule.startLifetime has a property minMaxState (int value from 0 to 3) representing 4 states:

  1. Constant
  2. Curve
  3. Random between two constants
  4. Random between two curves

There are three properties to store these values: scalar, minCurve, maxCurve. Note that minCurve/maxCurve.keys[0].value is used for storing extra data for “Random between two constants”.