AssetBundle.CreateFromMemory

I’m trying this code I’ve found in http://unity3d.com/support/documentation/Manual/AssetBundles.html

string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
IEnumerator Start () {
    // Start a download of the encrypted assetbundle
    WWW www = new WWW (url);

    // Wait for download to complete
    yield return www;

    // Get the byte data
    byte[] encryptedData = www.bytes;

    // Decrypt the AssetBundle data
    byte[] decryptedData = YourDecryptionMethod(encryptedData);

    // Create an AssetBundle from the bytes array
    AssetBundle bundle = AssetBundle.CreateFromMemory(decryptedData);

    // You can now use your AssetBundle
}

But it generates this error “CS0029: Cannot implicitly convert type UnityEngine.AssetBundleCreateRequest' to UnityEngine.AssetBundle’”

I would like to use this method to encrypt my asset bundle. I realy hope someone could point some light here…

That’s pretty cool, I didn’t even know that existed!

Looks like the doc example has a typo, though. If you check out the Scripting section of the manual, CreateFromMemory is asynchronous and actually returns an AssetBundleCreateRequest that you can yield on, then load the .assetBundle property.

Now we have a working code, thanks to getluky!

    string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
    IEnumerator Start () {
        // Start a download of the encrypted assetbundle
        WWW www = new WWW (url);
     
        // Wait for download to complete
        yield return www;
     
        // Get the byte data
        byte[] encryptedData = www.bytes;
     
        // Decrypt the AssetBundle data
        byte[] decryptedData = YourDecryptionMethod(encryptedData);
     
        // Create an AssetBundle from the bytes array
        AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData);
        while (!acr.isDone)
        {
                yield;
        }
        AssetBundle bundle = acr.assetBundle;
     
        // You can now use your AssetBundle
    }

Nice! I believe you could simplify that by directly doing the following instead of the while loop poll (the same as you did with www above it):

yield return acr;