- Home /
Android Assets Bundle Error
hi there, i'm using Unity 5 Pro and i'm trying to manage assets bundle on Android platform, all work perfectly on editor but on android i get NullReferenceException, i dont know how to solve it :(
that's the code to build asset:
using UnityEngine;
using UnityEditor;
public class ExportAssetBundles {
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
static void ExportResource () {
// Bring up save panel
string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
if (path.Length != 0) {
// Build the resource file from the active selection.
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android);
Selection.objects = selection;
}
}
}
and tha'ts the code to load and instantiate asset:
using UnityEngine;
using System.Collections;
public class AssetLoader : MonoBehaviour {
public static AssetLoader Instance;
private string url = "http://download1639.mediafire.com/rbw69d868j0g/s1vkw7gaju3yk0s/BannerBundle.unity3d";
public int version = 1;
AssetBundle bundle;
IEnumerator Start () {
WWW www = WWW.LoadFromCacheOrDownload (url, 1);
yield return www;
AssetBundle bundle = www.assetBundle;
Debug.Log(bundle.name);
yield return bundle;
GameObject obj = Instantiate(bundle.LoadAsset("myObject"), Vector3.zero, Quaternion.identity) as GameObject;
Debug.Log(obj);
}
}
Thanks for help
I am close to the solution, if I try to download the package with wifi working properly, with 3g it me back a bundle null, I tried to do a loop that redownload the bundle until it returns true, but enters into an infinite loop (I tried to control the www.progress and up to 100 each time) that's the new code:
void Awake () {
DontDestroyOnLoad(this.gameObject);
StartCoroutine(Download());
}
IEnumerator Download () {
www = new WWW (url);
yield return www;
yield return new WaitForSeconds(1);
if(www.isDone) {
if (www.assetBundle == null)
StartCoroutine(Download());
else if (www.assetBundle != null)
Instantiate();
}
}
Your answer
Follow this Question
Related Questions
Android Build Error 2 Answers
Help with SQLite for Android Errors in Unity3D? 1 Answer
Sound file Asset bundle not decompress 0 Answers
APK Can't be open in Android system microphone 0 Answers
CommandInvokationFailure: Failed to re-package resources 1 Answer