I’ve making a particle physics engine for unity.
I have a ‘ParticleSystem’ component which contains an array of ‘Particle’ objects which contain data about the particles in the system.
I think it would be really handy if I could save this array while the simulation is running. That way I could fill a funny shape with liquid, save it, and then when I play the scene again I could re-create the particles in that exact arrangement.
For instance in the example image below the particles have flowed into this shape during runtime. It would be cool to be able to save and reload this arrangement.
[34949-eg.png*_|34949]
Now. I’d like to avoid usign a .net serializer or playerprefs and have to mess around with save files. The neatest way to do this imo is to serialize this data as part of the component (which unity is serializing bits of anyway)
heres what the particle class looks like
public class LPParticle
{
/// <summary>Position of the particle</summary>
public Vector3 Position;
/// <summary>Color of the particle</summary>
public Color _Color;
/// <summary>Remaining lifetime of the particle, < 0 = infinite, 0 = particle will be destroyed next step</summary>
public float LifeTime;
/// <summary>Pressure the particle is experiencing</summary>
public float Weight;
/// <summary>Velocity of the particle</summary>
public Vector3 Velocity;
/// <summary>Userdata value of the particle. Can be used for gameplay mechanics</summary>
public int UserData;
}
I’ve tried out one idea so far. It involved having another class like the particle class but derived from criptableObject like so…
[Serializable]
public class LPParticleCopy : ScriptableObject
{
/// <summary>Position of the particle</summary>
public Vector3 Position;
/// <summary>Color of the particle</summary>
public Color _Color;
/// <summary>Remaining lifetime of the particle, < 0 = infinite, 0 = particle will be destroyed next step</summary>
public float LifeTime;
/// <summary>Pressure the particle is experiencing</summary>
public float Weight;
/// <summary>Velocity of the particle</summary>
public Vector3 Velocity;
/// <summary>Userdata value of the particle. Can be used for gameplay mechanics</summary>
public int UserData;
}
Then when you hit a save button on the components inspector panel it makes a copy of all the particles using the new class…
public void Save()
{
SavedParticles = new LPParticleCopy[Particles.Length];
for (int i = 0; i < Particles.Length; i++)
{
SavedParticles *= ScriptableObject.CreateInstance<LPParticleCopy>();*
if(Particles.Position != null) SavedParticles_.Position = Particles*.Position;_
if(Particles._Color != null) SavedParticles._Color = Particles.Color;*
if(Particles.LifeTime != null) SavedParticles.LifeTime = Particles*.LifeTime;
if(Particles.Weight != null) SavedParticles.Weight = Particles.Weight;
if(Particles.Velocity != null) SavedParticles.Velocity = Particles.Velocity;
if(Particles.UserData != null) SavedParticles.UserData = Particles.UserData;
}
Saved = true;
}
This method got me this far… This screenshot shows that particle system component after hitting the save button…
[34951-saved.png*|34951]*
The rest of my plan involved copying the component at this stage (with right click), then stopping play mode and pasting the component values back into it.
After performing this step the array is the right lenght but unfortunately it is filled with ‘None(LPParticleCopy)’
*
*