Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ricardo_arango · Nov 24, 2011 at 04:42 PM · saveloadparticleserialize

How can I serialize the Particles of a Particle Emitter

I want to save the array of Particles returned by the particles property so that I can load it at a later time. How can I do this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by ricardo_arango · Nov 24, 2011 at 04:48 PM

You can use .net marshalling to save the array of particles (structs). First you need to convert the array to a byte[], then save it as a binary file. To load you would read each particle, and convert from byte[] to a Particle struct.

Create a CSharp script named SerializeParticles .cs. Copy/paste the following code:

 using UnityEngine;
 public class SerializeParticles : MonoBehaviour
 {
 }

You need to attach the previous script to the GameObjec with the particle emitter component.

Create another CSharp script named CustomInspectorParticles.cs and place it inside an Editor folder. Copy/paste the following code:

 using System.IO;
 using UnityEngine;
 using UnityEditor;
 using System;
 using System.Runtime.InteropServices;
 
 [CustomEditor(typeof(SerializeParticles))]
 public class CustomInspectorParticles : Editor
 {
     public override void OnInspectorGUI()
     {
         var t = target as SerializeParticles;
         if (t != null){
             if (GUILayout.Button("Load Particle Data"))
                 t.gameObject.GetComponent<ParticleEmitter>().particles = LoadParticleData();
 
             if (GUILayout.Button("Save Particle Data"))
                 SaveParticleData(t.gameObject.GetComponent<ParticleEmitter>().particles);
         }
 
         if (GUI.changed)
             EditorUtility.SetDirty(target);
     }
 
     static Particle[] LoadParticleData()
     {
         // Load the byte array
         var fs = new FileStream(Application.dataPath + "/particleData.asset", FileMode.Open, FileAccess.Read, FileShare.None);
 
         // Number of particles
         var dataSize = Marshal.SizeOf(new Particle());
         var numParticles = ((int)fs.Length / dataSize);
         var particles = new Particle[numParticles];
 
         // Create a byte[] to hold all the particle data
         var byteArray = new byte[dataSize];
         var pointer = Marshal.AllocHGlobal(dataSize);
         try {
             for (var i = 0; i < numParticles; i++) {
                 // Read the bytes of a particle
                 fs.Read(byteArray, 0, dataSize);
                 // Copy the byteArray to unmanaged memory
                 Marshal.Copy(byteArray, 0, pointer, dataSize);
                 // Particle in unmanaged memory 
                 particles[i] = (Particle)Marshal.PtrToStructure(pointer, typeof(Particle));
             }
         }
         finally {
             // Free the unmanaged memory.
             Marshal.FreeHGlobal(pointer);
         }
         fs.Close();
 
         return particles;
     }
 
     static void SaveParticleData(Particle[] particles)
     {
         if (particles == null)
             return;
         if (particles.Length == 0)
             return;
 
         // Size of the entire array
         int totalSize = Marshal.SizeOf(new Particle()) * particles.Length;
         int dataSize = Marshal.SizeOf(new Particle());
 
         // Create a byte[] to hold all the particles, but only one can be copied at the time
         var byteArray = new byte[totalSize];
         IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(new Particle()));
         try {
             for (var i = 0; i < particles.Length; i++) {
                 Marshal.StructureToPtr(particles[i], pointer, false);
                 Marshal.Copy(pointer, byteArray, i * dataSize, dataSize);
             }
         }
         finally {
             // Free the unmanaged memory.
             Marshal.FreeHGlobal(pointer);
         }
 
         // Save the byte array
         var fs = new FileStream(Application.dataPath + "/particleData.asset", FileMode.Create, FileAccess.Write, FileShare.None);
 
         // Save the particle data
         fs.Write(byteArray, 0, byteArray.Length);
         fs.Close();
     }
 }

[Note: This code is a guideline, it needs more data validation]

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

container class save based on Cherno's example 1 Answer

Delete a serialized binary save file 2 Answers

Save and load a class 0 Answers

Serialize GameObject 1 Answer

UnitySerializer not saving properly/any longer 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges