Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity AssetBundle Examples.
using System;
using UnityEngine;
using System.Collections;
public class AssetBundleSample : MonoBehaviour {
public GUIText guitext;
// Use this for initialization
void Start () {
// Clear Cache
Caching.CleanCache();
string loadUrl = "https://dl.dropboxusercontent.com/u/172835/track";
#if UNITY_ANDROID && !UNITY_EDITOR
loadUrl += ".android.unity3d";
#elif UNITY_IPHONE && !UNITY_EDITOR
loadUrl += ".iphone.unity3d";
#else
loadUrl += ".unity3d";
#endif
StartCoroutine(load(loadUrl, 1));
}
// Update is called once per frame
void Update () {
// progress
int percent = (int)(www.progress * 100);
guitext.text = percent.ToString() + "%";
}
private WWW www;
private IEnumerator load(string url, int version) {
// wait for the caching system to be ready
while (!Caching.ready)
yield return null;
// load AssetBundle file from Cache if it exists with the same version or download and store it in the cache
www = WWW.LoadFromCacheOrDownload(url, version);
yield return www;
Debug.Log("Loaded ");
if (www.error != null)
throw new Exception("WWW download had an error: " + www.error);
AssetBundle assetBundle = www.assetBundle;
Instantiate(assetBundle.mainAsset); // Instantiate(assetBundle.Load("AssetName"));
// Unload the AssetBundles compressed contents to conserve memory
assetBundle.Unload(false);
}
}
// Builds an asset bundle from the selected objects in the project view.
// Once compiled go to "Menu" -> "Assets" and select one of the choices
// to build the Asset Bundle
using UnityEngine;
using UnityEditor;
public class ExportAssetBundles {
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
static void ExportResurce() {
// Bring up save panel
string basename = Selection.activeObject ? Selection.activeObject.name : "New Resource";
string path = EditorUtility.SaveFilePanel("Save Resources", "", basename, "");
if (path.Length != 0) {
// Build the resource file from the active selection.
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
// for Android
BuildPipeline.BuildAssetBundle(Selection.activeObject,
selection, path + ".android.unity3d",
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets,
BuildTarget.Android);
// for iPhone
BuildPipeline.BuildAssetBundle(Selection.activeObject,
selection, path + ".iphone.unity3d",
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets,
BuildTarget.iPhone);
// for WebPlayer
BuildPipeline.BuildAssetBundle(Selection.activeObject,
selection, path + ".unity3d",
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets,
BuildTarget.WebPlayer);
Selection.objects = selection;
}
}
// [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
// static void ExportResource() {
// // Bring up save panel
// string path = EditorUtility.SaveFilePanel("Save Resources", "", "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);
// Selection.objects = selection;
// }
// }
// [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
// static void ExportResourceNoTrack() {
// // 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.
// BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
// }
// }
}
@yaeda

This comment has been minimized.

Owner

yaeda commented Apr 18, 2013

  • ExportAssetBundles.cs is editor script, so should be placed under Assets/Editor.
  • AssetBundleSample.cs is loader sample.
@pymanunity3d

This comment has been minimized.

pymanunity3d commented May 30, 2014

Hi :)
Thank you

I have a question
i do everything that you had said
but when i delete file it creates too
very strange
Please see photo
http://axgig.com/images/08313496303827739806.jpg
Sorry for poor English :(

@etheritsoluions

This comment has been minimized.

etheritsoluions commented Aug 27, 2014

Exception: WWW download had an error: Rejected because no crossdomain.xml policy file was found
AssetBundleSample+c__Iterator0.MoveNext () (at Assets/Loader/AssetBundleSample.cs:52)

@kathees

This comment has been minimized.

kathees commented Jul 17, 2015

I've used your ExportAssetBundle.cs script for create .android.unity3d asset files. It was working fine but today when i tried, i got below error. I don't know whats happened,

Building Asset Bundles requires Unity Advanced for AndroidPlayer
UnityEditor.BuildPipeline:BuildAssetBundle(Object, Object[], String, BuildAssetBundleOptions, BuildTarget)
ExportAssetBundles:ExportResurce() (at Assets/Editor/ExportAssetBundles.cs:21)

@ssuresh1295

This comment has been minimized.

ssuresh1295 commented Aug 4, 2017

What is the procedure to export AssetBundle? And How to call AssetBundleSample.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment