Skip to content

Instantly share code, notes, and snippets.

@identity2
Created June 8, 2019 06:13
Show Gist options
  • Save identity2/677db3c929d6d9cbb51b47bf9efde01d to your computer and use it in GitHub Desktop.
Save identity2/677db3c929d6d9cbb51b47bf9efde01d to your computer and use it in GitHub Desktop.
Deserialize
public static void LoadAudioClipFromDisk(AudioSource audioSource, string filename)
{
if (File.Exists (Application.persistentDataPath + "/"+ filename)) {
//deserialize local binary file to AudioClipSample
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/" + filename, FileMode.Open);
AudioClipSample clipSample = (AudioClipSample) bf.Deserialize (file);
file.Close ();
//create new AudioClip instance, and set the (name, samples, channels, frequency, [stream] play immediately without fully loaded)
AudioClip newClip = AudioClip.Create(filename, clipSample.samples, clipSample.channels, clipSample.frequency, false);
//set the acutal audio sample to the AudioClip (sample, offset)
newClip.SetData (clipSample.sample, 0);
//set to the AudioSource
audioSource.clip = newClip;
}
else
{
Debug.Log("File Not Found!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment