Search code examples
c#unity-game-engineencodeassetbundle

How encode Audio file and save it with unity?


I want to encode some type of file from my assetbundle and save those files to my path. I use this code for encoding Texture2D file to ".jpg" and save it:

(my assetbundle method work fine)
texTmp = (Texture2D) Instantiate (assetRequest.asset);
SaveSaveToJpg(texTmp);

and save with:

void SaveToJpg(Texture2D texture)
{
byte[] bytes = texture.EncodeToPNG();
File.WriteAllBytes(Application.persistentDataPath + "/"+ texture.name + ".jpg", bytes);
}

So in Unity documentation, I saw how can encode the .mp4 file, but how can I encode the Audio file and save it with unity?

Edit: I use https://gist.github.com/darktable/2317063#file-savwav-cs class, but is there any unity official method for this?


Solution

  • SavWav Script

    public AudioClip myAudio;
    
    SavWav.Save(Application.persistentDataPath + "/audio.wav", myAudio);