![]() |
Magical Box
1.15
Modular Particle Engine for Unity3D
|
Depending on your parameters you may want to store additional data in your particles. This is posssible using particle data slots. Data slots are managed by emitters, the data itself is stored in MBParticle.UserData.
First you'll need to override MBParameter.OnPlay() to register a data slot and store the returned ID. Using this ID you can read and write data to particles in your parameter:
public MBParticleCustomData : MBParameter { int mSlotID; public override void OnPlay() { mSlotID=ParentEmitter.RegisterParticleUserData("ParticleCustomData"+GetInstanceID()); // InstanceID is used to make this slot unique to this parameter instance } public override void OnBirth(MBParticle PT) { if (PT.HasUserData(mSlotID)) PT.UserData[mSlotID]=Random.insideUnitSphere(); // store a random Vector3 as custom data, just for demonstration purpose } public override void OnLifetime(MBParticle PT) { if (PT.HasUserDataValue(mSlotID)) Debug.Log(PT.Userdata[mSlotID]); // access the previously stored data. Of course you don't want to use Debug.Log in this method, do you? } }
User Data slots will be cleared if you play a previously stopped emitter and when calling MBEmitter.Stop() explicitely.
You should use MBParticle.HasUserData and MBParticle.HasUserDataValue to prevent runtime errors when adding parameters while there are existing particles.