Skip to content

Instantly share code, notes, and snippets.

@identity2
Created June 8, 2019 06:14
Show Gist options
  • Save identity2/cec6d1994d90be4309968faa49ce24c9 to your computer and use it in GitHub Desktop.
Save identity2/cec6d1994d90be4309968faa49ce24c9 to your computer and use it in GitHub Desktop.
Serialize
public static void SaveAudioClipToDisk(AudioClip audioClip, string filename)
{
//create file
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath+ "/" + filename);
//serialize by setting the sample, frequency, samples, and channels to the new AudioClipSample instance
AudioClipSample newSample = new AudioClipSample();
newSample.sample = new float[audioClip.samples * audioClip.channels];
newSample.frequency = audioClip.frequency;
newSample.samples = audioClip.samples;
newSample.channels = audioClip.channels;
//get the actual sample from the AudioClip
audioClip.GetData (newSample.sample, 0);
bf.Serialize (file, newSample);
file.Close ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment