hi guys,
i want to access the new particle system modules such as “emission” and “shape” in script,
so that i can change the particle style during runtime .
but i can not find any api to access the modules after i get the ParticleSystem component.
is there a way to access the modules using script ? how ?
thanks a lot !
You can’t by normal means, however, with the aid of legacy animations, you can.
First, add the Animation component (not Animator!) to your object you want to animate.
Then in code, each time you want to animate a property, use this snippet:
Animation animation = GetComponent<Animation>();
AnimationCurve curve = AnimationCurve.Linear(startTime, startValue, endTime, endValue);
AnimationClip clip = new AnimationClip();
clip.legacy = true;
clip.SetCurve("", typeof(ParticleSystem), "ShapeModule.radius", curve);
animation.AddClip(clip, "Resize");
animation.Play("Resize");
The above code will animate the radius property of the shape module from start value to end value. In general, the property key is named “nameModule.property”, but if you want to know for sure, do the following:
-
Under Edit > Project Settings > Editor, change Asset Serialization mode to Force Text
-
Look for the scene containing your particle effect. Open in text editor.
-
Search for the particle system and look for the key you want to change.
Of course, for an instantaneous change, just define the animation curve to be something like
AnimationCurve curve = AnimationCurve.Linear(0, 0, 0, newValue);
Took me a while to get it working!
I found solution of changing shape through script.
NOT working:
public ParticleSystem psystem;
void Start(){
psystem.shape.box = new Vector3(2,2,2); // error, couldn't compile
//psystem.shape.box.Set(2,2,2); // has no error, and compile, but has no results
}
Working:
public ParticleSystem psystem;
void Start(){
var x = psystem.shape;
x.box = new Vector3(2,2,2); // solve my problem
//x.box.Set(2,2,2); - still has no error, but still has no results
// if you want change shape cone radius - x.radius = 34345;
}
Hope it will helpfull
Read the documentation on what variables and functions you can access with the Shuriken particle system, because you can’t change “shape” through scripts. You can only change those in the editor.
Shuriken particle system documentation
Good luck!
You can modify all variables.
For example, i change my angle shape:
SerializedObject so = new SerializedObject(GetComponent<ParticleSystem>());
so.FindProperty("ShapeModule.angle").floatValue = 25f;
so.ApplyModifiedProperties();
You can see all particleSystem propriety with:
SerializedProperty it = so.GetIterator();
while (it.Next(true))
Debug.Log (it.propertyPath);
(sorry for my bad english, i’m italian).
Old question, but I just came across this trying to google for myself. To explain what I was doing a bit, in a ‘diablo-ish’ kind of game, I have a spell explode, and attach an ‘I’m on fire!!’ effect object… By default, it’s just a lame little sphere as it’s default emission shape, so depending on what it hit I then wanted to change it from emitting my sphere to SkinnedMeshRenderer, MeshRenderer, etc…
Firstly, I declared a GameObject parentObject; and set it with my OnCollision script, as my meshes are always buried in child objects of the monster’s parent containing my collider.
//Get particleSystem's ShapeModule
ParticleSystem particleSystem = GetComponent<ParticleSystem>();
ParticleSystem.ShapeModule shapeModule = particleSystem.shape;
//Check&Set for MeshRenderer, set Shape MODULE's ParticleSystemShapeType
if (parentObject.GetComponentInChildren<SkinnedMeshRenderer>())
{
shapeModule.shapeType = ParticleSystemShapeType.SkinnedMeshRenderer;
shapeModule.skinnedMeshRenderer = parentObject.GetComponentInChildren<SkinnedMeshRenderer>();
}
Just a snippet, I change more variables, like meshShapeType (Vert/Edge/Tri), which I actually found before just shapeType, but it gave me hope! lol, and useMeshColors, etc…, and else if through the other types, then if none found, it’ll just stay it’ll silly default sphere emitter… and it works like a charm in my project’s latest build!
Really took some digging to find “shapeType”. I kept trying to change .shape assuming it was the Shape Property and not the Shape Module… Finally IntelliSense said ‘MODULE’, and I recalled the docs… Unity - Manual: Particle System modules … So, that’s why I purposely declared particleSystem and shapeModule in that way, to illustrate what about this made me feel the most stupid and caused me to google lol!
Also may need to note that I’m on 5.3.2p2… I’m a few patches behind now, but they’ve been working a lot on the Particle System recently, so this may still be fairly new… Anyways, hope this helps someone access their variables, in case I wasn’t the last person still trying.
As of Unity 5 (I’m on 5.3), module structs can be retrieved through public get by reference. Modifying the structs also modifies the values on the particle system.
ParticleSystem ps = GetComponent<ParticleSystem>();
// Set the burst on EmissionModule
ParticleSystem.Burst burst = new ParticleSystem.Burst()
{
time = 0.0f,
minCount = 1,
maxCount = 5
};
ParticleSystem.EmissionModule em = ps.emission;
em.SetBursts(new ParticleSystem.Burst[] { burst });
// Set the angle of the ShapeModule
ParticleSystem.ShapeModule sm = ps.shape;
sm.angle = 30.0f;