- Home /
Load Asset Bundle in Editor
Hey Guys!,
I've been trying to load an asset bundle from an editor script but I'm just unable to do it :/...
you might think "Why would you do that?" well... the answer is simple. I have a Library of asset bundles and have a program to place the assets easily so what I just need is to load lets say the asset X which resides in the address http://myserver.com/myasset.unity3dbundle
and load in the Editor, so I can calculate a light map of the generated scene..
Here is what I have written:
using UnityEngine; using UnityEditor;
using System.Collections;
public class DecryptAssetBundle : EditorWindow {
private string assetURL = "";
private static WWW request;
[MenuItem("Bundles/Decrypt Asset Bundle")]
static void Init () {
DecryptAssetBundle window = EditorWindow.GetWindow(typeof(DecryptAssetBundle)) as DecryptAssetBundle;
window.position = new Rect(Screen.width/2,Screen.height/2, 400,150);
}
void OnGUI()
{
assetURL = EditorGUILayout.TextField("Asset bundle URL: ", assetURL);
GUILayout.BeginHorizontal();
if(GUILayout.Button("Clear"))
{
assetURL = "";
}
if(GUILayout.Button("Decrypt"))
{
MonoBehaviour m = new MonoBehaviour(); //Shouldnt do this but is the only way to make StartCoproutine work
m.StartCoroutine(LoadAsset(assetURL));//doesnt work!!!!
}
GUILayout.EndHorizontal();
}
private IEnumerator LoadAsset(string s)
{
Debug.Log("Loading " + s + " ...");
request = new WWW(s);
yield return request;
if(request.error != null)
Debug.LogError(request.error);
GameObject g = (GameObject) request.assetBundle.mainAsset as GameObject;
Instantiate(g);
}
}
Any help would be greatly appreciated :)
Thanks!
Answer by msknapp · Sep 23, 2011 at 07:16 PM
I have actually accomplished this with my own script. You have the wrong idea here, you cannot use StartCoroutine except during runtime. My script is designed to not freeze up the editor while running. Use this instead:
public class DecryptAssetBundle : EditorWindow {
private string assetURL = "";
private static WWW request;
private static bool run = false;
private static IEnumerator en = null;
[MenuItem("Bundles/Decrypt Asset Bundle")]
static void Init () {
DecryptAssetBundle window = EditorWindow.GetWindow(typeof(DecryptAssetBundle)) as DecryptAssetBundle;
window.position = new Rect(Screen.width/2,Screen.height/2, 400,150);
}
void OnGUI()
{
assetURL = EditorGUILayout.TextField("Asset bundle URL: ", assetURL);
GUILayout.BeginHorizontal();
if(GUILayout.Button("Clear"))
{
assetURL = "";
}
if(GUILayout.Button("Decrypt"))
{
run=true;
}
if(GUILayout.Button("Abort"))
{
run=false;
}
GUILayout.EndHorizontal();
}
public void Update() {
if (!run) {
if (en != null)
en = null;
return;
}
if (en == null) {
en = LoadAsset(assetUrl);
}
if (! en.MoveNext())
run=false;
}
private IEnumerator LoadAsset(string s)
{
Debug.Log("Loading " + s + " ...");
request = new WWW(s);
while (! request.isDone)
// avoid freezing the editor:
yield return ""; // just wait.
// I don't like the following line because it will freeze up
// your editor. So I commented it out.
// yield return request;
if(request.error != null)
Debug.LogError(request.error);
GameObject g = (GameObject) request.assetBundle.mainAsset as GameObject;
Instantiate(g);
}
}
Answer by ckfinite · May 19, 2011 at 08:13 PM
Have you seen the example code here? http://unity3d.com/support/documentation/ScriptReference/AssetBundle.html. It seems like it is similar to what you want to do.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081038im_/https://answers.unity.com/themes/thub/images/avi.jpg)