Asset Bundle Asset Path

Hey All!

Basically I need help with getting a path from an asset that has been taken out of an asset bundle.

Firstly I’m not even sure if you can. But at the moment I want to take the asset from the asset bundle and put it in the persisantData path.

What I got below returns an empty string which means I cant tell SaveAndDownload where to look.

I’m really stuck on this. Maybe there might be another way to do it?

    public AnimatedGifPlayer animatedGifPlayer;
    public string url;
    public Texture2D testTexture;


    [ContextMenu("Starter")]
    public void Starter()
    {
        testTexture = [Asset Bundle With Object].LoadAsset<Texture2D>("grass.gif");
        
        // This returns an empty string
        string assetPath = AssetDatabase.GetAssetPath(testTexture);
        url = assetPath;

        StartCoroutine(SaveAndDownload(Application.persistentDataPath + "/grass.gif"));
    }


    IEnumerator SaveAndDownload( string saveTo)
    {
        WWW www = new WWW(url);
        yield return www;

        byte[] bytes = www.bytes;
        File.WriteAllBytes(saveTo, bytes);
        Debug.Log(url);

        yield return new WaitForSeconds(1);

        Debug.Log("Load");
//Do something with the new file
    }
}

It returns an empty string because the contents of an assetbundle is not managed by the AssetDatabase.
The only way to get to assets inside AssetBundles is via its own API.
Once a texture has been loaded from the bundle you could save it via the Texture API.
I honestly hope Unity makes saving things put into AssetBundles as hard as possible, to not give away user content too easily.

Once you extract from the assetbundle, you have the asset. testTexture is the asset. You can’t get a path to the object because it is in the assetbundle which you extracted to memory.

Since you have the testTexture, you should just be converting that to a byte[ ] array and saving it out, not trying to load it and then save it out again.

Okay that makes sense. I used the way you suggested but with a TextAsset and and work now!

Cheers

1 Like