- Home /
How to modify ParticleSystem from Editor script?
Exposed ParticleSystem API is very limitted (http://docs.unity3d.com/Documentation/ScriptReference/ParticleSystem.html). 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?
Answer by Paulius-Liekis · Jun 30, 2013 at 09:19 PM
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;
so.ApplyModifiedProperties();
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:
Constant
Curve
Random between two constants
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".
Warning, as of Unity4.5, the accepted answer to this question will not work for most people. SerializedObject is in the UnityEditor namespace and, therefore, IS NOT AVAILABLE at run-time. Attempting to build a project that references the UnityEditor namespace will result in failure.
If your intention is to access a ParticleSystem during run-time to dynamically change effects, unfortunately, you still only have access to those handful of properties publicly exposed by the ParticleSystem component.
Hopefully this will change in Unity5, but this has been a persistent limitation since Unity3. Those of us who rely more heavily on scripting than on the editor still have to use the legacy particle system to gain more--though still not total--control of our particle systems.
In my case the intention was for Editor only, e.g. importing full particle setup from 3ds$$anonymous$$ax.
Your answer
Follow this Question
Related Questions
Building a Collection of Subclasses and Modifying Their Public Members from the Inspector 0 Answers
How does Unity sort fields in order of their declaration? 1 Answer
Make popup window in inspector if deleting GameObject 0 Answers
How can I hide a MonoBehaviour from the Inspector? (or make it extremely thin or compact) 1 Answer