I'm downloading Asset bundles built for Android platform from server. These Asset bundles contain prefabs, 3D model files and textures of the model. I'm then using the Asset bundle to spawn the prefab in scene. All this works fine in the editor. But when I build the APK and run it on my phone the Asset bundle loads but the model doesn't spawn.
For more context, I removed the copies of the asset bundles from the streaming assets folder (since the bundles were being loaded from the server anyway) and cleared the unused Asset Bundle names. I did this to reduce the APK size, now I', wondering if that is what caused this. I tried putting the files back in streaming assets, it did not work. This is the coroutine that spawns the model.
Again, everything works fine in the editor. And I know the Asset bundle is loaded because the flag at the end (modelloaded) turns true.
This is the coroutine that spawns the model
IEnumerator SpawnAnimal(string url)
{
using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url, 0))
{
yield return request.SendWebRequest();
try
{
if (request.isHttpError || request.isNetworkError)
{
animal = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, GetMethod.instance.clickedid));
}
else
{
animal = DownloadHandlerAssetBundle.GetContent(request);
// animal = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, GetMethod.instance.clickedid));
}
}
catch (Exception ex)
{
}
if (animal != null)
{
GameObject fbxObject = Instantiate(animal.LoadAsset<GameObject>(GetMethod.instance.clickedName));
fbxObject.transform.position = new Vector3(0, -0.75f, 4);
fbxObject.transform.rotation = new Quaternion(0, 100, 0, 0);
AddMissingComponents(fbxObject);
modelLoaded = true;
}
}
yield return null;
}
So turns out even if you include the prefab to a model in your asset bundle you cannot delete it from the project path. You can delete the actual model, textures and all other dependencies that you include in the asset bundle but the prefab itself should still be in the project folder in the same location it was when you build the asset bundle. And it should have the same asset bundle label also.
There was nothing wrong with the code.
This needs to be mentioned somewhere in Unity Asset Bundle documentation. Because in the editor the functionality works and it is very difficult to figure why it wouldn't work in the build if you accidentally delete the original prefabs or move them since they are empty