So I’m trying to create a basic assetbundle that contains just a material that I can load into another program via a script.
The code I’m using to generate the bundle is based on this guide. I’ve created a basic unity project where I’ve got my material and it’s assigned to an asset bundle. However, when I build the bundle and try to load it using either AssetBundle.LoadFromMemory
or AssetBundle.LoadFromMemoryAsync
, I get the error: Failed to decompress data for the AssetBundle 'Memory'.
I’ve tried a bunch of different combinations of options with the BuildPipeline.BuildAssetBundles
method but none of them worked.
I tried loading the asset bundles into AssetStudio to try and verify the bundle’s content and got an OutOfMemory exception while it was trying to load the material’s shader.
After a good bit of messing around I finally got that issue to stop by going into the project settings and telling unity to always include the material’s shader in the bundles (the shader it uses is just “Standard”) and this made it so AssetStudio could load the bundle but it said that the only thing in the bundle were type definitions for Material and Shader but 0 assets. Additionally, trying to load the bundle still resulted in the Failed to decompress message.
I’ve tried googling around for other people with the same issue but couldn’t find anything related. Does anyone have any ideas of why my assetbundle seems to be generating as empty?
@Aidanamite - First, let’s make sure that the material is assigned to an AssetBundle. In the Unity Editor, select the material, go to the Inspector, and at the bottom, you’ll find the AssetBundle option. Assign a name to your AssetBundle, e.g., “my_material_bundle”.
Now, let’s create a script to build the AssetBundle. Create a new C# script in your Unity project, and name it “AssetBundleBuilder”. Replace its content with the following code:
using UnityEditor;
using UnityEngine;
public class AssetBundleBuilder : MonoBehaviour
{
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBundles()
{
string assetBundleDirectory = "Assets/AssetBundles";
if (!System.IO.Directory.Exists(assetBundleDirectory))
{
System.IO.Directory.CreateDirectory(assetBundleDirectory);
}
BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);
}
}
- Save the script, and in the Unity Editor, click on “Assets” in the menu, and then click on “Build AssetBundles”. This will build the AssetBundle and save it in the “Assets/AssetBundles” folder.
Now, let’s create another script to load the AssetBundle and instantiate the material at runtime. Create a new C# script and name it “AssetBundleLoader”. Replace its content with the following code:
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class AssetBundleLoader : MonoBehaviour
{
public string assetBundleUrl = "file://path/to/your/AssetBundles/my_material_bundle";
IEnumerator Start()
{
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(assetBundleUrl);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Failed to load AssetBundle: " + request.error);
}
else
{
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
Material material = bundle.LoadAsset<Material>("YourMaterialName");
if (material != null)
{
// Do something with the material, e.g., assign it to a GameObject
// gameObject.GetComponent<Renderer>().material = material;
}
else
{
Debug.LogError("Failed to load material from AssetBundle");
}
bundle.Unload(false);
}
}
}
- Replace the “file://path/to/your/AssetBundles/my_material_bundle” with the correct file path to your AssetBundle file. Attach the “AssetBundleLoader” script to any GameObject in your scene, and run the scene.
If you’ve followed these steps and you’re still facing issues, there might be a problem with your material or shader. Make sure that your material and shader are compatible with the platform you’re targeting (Built-in, URP, HDRP). If the issue persists, please provide more information about your Unity version, platform, and any other relevant details.