Search Unity

How do you use AssetBundles in a Windows compiled build?

Discussion in 'Asset Bundles' started by ENAYSOFT, Jan 4, 2019.

  1. ENAYSOFT

    ENAYSOFT

    Joined:
    Aug 22, 2012
    Posts:
    13
    Hello,

    Unity and also AssetBundle newbie here :)

    Perhaps I don't understand AssetBundles correctly but I'm having trouble using them in Windows after I have compiled a build.

    I've placed the assets in either Assets/AssetBundle directory or Assets/StreamingAssets and they work fine in the Unity Editor.
    But when built nothing happens. I can only assume I haven't included the assetbundles in the build, or something like that.
    Whenever I search about assetbundles it always explains downloading the assets from some website somewhere.
    All I want to do is use AssetBundles offline in the build I've just created. Likely I am missing something very simple since I'm still a newbie.

    If anyone can see what I am doing wrong, it would be greatly appreciated :)

    I'm using the "PC, Mac & Linux Standalone" setting and encoding my assets and loading them using the following code (which I will say again, work ok in the Unity Editor, just not when I make a build)

    Code (CSharp):
    1.     static void BuildAllAssetBundles()
    2.     {
    3.         string assetBundleDirectory = "Assets/AssetBundles";
    4.         if(!Directory.Exists(assetBundleDirectory))
    5.         {
    6.             Directory.CreateDirectory(assetBundleDirectory);
    7.         }
    8.         BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    9.     }
    Code (CSharp):
    1. var bundleLoadRequest = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/" + _assetBundleName);
    2.             yield return bundleLoadRequest;
    3.             Debug.Log("bundleLoadRequest finished");
    4.             var myLoadedAssetBundle = bundleLoadRequest.assetBundle;
    5.             if (myLoadedAssetBundle == null)
    6.             {
    7.                 Debug.Log("Failed to load AssetBundle!");
    8.                 yield break;
    9.             }
    10.  
    11.             var assetLoadRequest = myLoadedAssetBundle.LoadAssetAsync<AudioClip>(_fileName);
    12.             yield return assetLoadRequest;
    Many thanks for reading my post. :)
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    I store asset bundles in "Assets/StreamingAssets", because:
    https://docs.unity3d.com/Manual/StreamingAssets.html

    ... and then use AssetBundle.LoadFromFile.

    Here is an example how to load an asset bundle file from StreamingAssets:
    https://docs.unity3d.com/ScriptReference/AssetBundle.LoadFromFile.html

    Here is a link to the "A guide to AssetBundles" document:
    https://unity3d.com/learn/tutorials/topics/best-practices/guide-assetbundles-and-resources

    If your bundles have dependencies, like BundleB depends on content of BundleA, then you need to load its dependencies first.

    Update: Unity Addressables makes working with Asset Bundles easier, see https://docs.unity3d.com/Manual/com.unity.addressables.html
     
    Last edited: Dec 16, 2020
    idbrii, alpha_rat and ENAYSOFT like this.
  3. ENAYSOFT

    ENAYSOFT

    Joined:
    Aug 22, 2012
    Posts:
    13
    Hi Peter, many thanks for your detailed post.

    I checked the link for Streaming Assets and noticed

    path = Application.dataPath + "/StreamingAssets";

    which I replaced as mine was

    path =Application.streamingAssetsPath + "/"

    I find it very odd that the official Unity docs regarding StreamingAssets don't even use the

    .streamingAssetsPath tag

    At any rate, through random stabs in the dark, everything is now magically working.

    Again, many thanks. :D
     
    Peter77 likes this.
  4. ENAYSOFT

    ENAYSOFT

    Joined:
    Aug 22, 2012
    Posts:
    13
    Hey Peter77, just wanted to quickly thank you again for your post, I've achieved what I set out to do and learned how StreamingAssets work. :)
     
  5. EvolvedPhoenix

    EvolvedPhoenix

    Joined:
    May 17, 2018
    Posts:
    1
    i keep getting this error when i try instantiating the my assetbundle
    here is my code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;

    public class GunWeaponModel : MonoBehaviour
    {
    public GameObject GFX;

    void Start()
    {
    var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "Mods/Windows/testgun"));
    if (myLoadedAssetBundle == null)
    {
    Debug.Log("Failed to load AssetBundle!");
    return;
    }

    var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("testgun");
    Instantiate(prefab);
    prefab.transform.parent = GFX.transform;

    myLoadedAssetBundle.Unload(false);
    }
    }