Created
June 8, 2019 06:13
-
-
Save identity2/677db3c929d6d9cbb51b47bf9efde01d to your computer and use it in GitHub Desktop.
Deserialize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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