Hi,
I´m having some problems with AssetBundles, I can´t find where´s the error. When I run the scene, the asset bundle is downloaded and the GameObject instantiated but it doesn´t appear.
This is the code for creating the asset bundles:
CreateAssetBundles.cs
using UnityEditor;
public class CreateAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBundles()
{
BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
}
This is the code for loading asset bundes at run-time (of course that it´s attached to an empty object in the scene):
CatchingLoadExample.cs
using System;
using UnityEngine;
using System.Collections;
public class CachingLoadExample : MonoBehaviour
{
private string BundleURL = "file://C:/Users/AR/Documents/UnityProjects/ASBundles/Assets/AssetBundles/test.unity3d";
private string AssetName = "Cube";
IEnumerator Start()
{
Debug.Log("Connecting");
// Download the file from the URL. It will not be saved in the Cache
using (WWW www = new WWW(BundleURL))
{
yield return www;
if (www.error != null)
{
Debug.Log("Error downloading");
throw new Exception("WWW download had an error:" + www.error);
}
AssetBundle bundle = www.assetBundle;
if(bundle == null)
{
Debug.Log("Bundle null");
}
GameObject Cube;
if (AssetName == "")
{
Debug.Log("Loading main asset");
Instantiate(bundle.mainAsset);
}
else
{
Debug.Log("Loading with name");
Cube = bundle.LoadAsset(AssetName, typeof(GameObject)) as GameObject;
Instantiate(Cube);
Cube.transform.Translate(new Vector3(0f, 0f, 0f));
Cube.transform.localScale = new Vector3(1f, 1f, 1f);
Cube.transform.localRotation = Quaternion.identity;
Cube.SetActive(true);
}
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
Debug.Log("Finish");
}
}
When I run the scene, I can see in the console the following logs:
- Connecting
- Loading with name
- Finish
But the GameObject does´t appear.
Thanks.
PD: Sorry about my english, it´s not my first language