• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by naruse · May 19, 2011 at 08:07 PM · editor-scriptingassetbundle

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 t$$anonymous$$nk "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 w$$anonymous$$ch 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 t$$anonymous$$s 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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by msknapp · Sep 23, 2011 at 07:16 PM

I have actually accomplished t$$anonymous$$s 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 w$$anonymous$$le running. Use t$$anonymous$$s 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);
     w$$anonymous$$le (! 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);
 }
 
 } 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image naruse · Nov 23, 2011 at 04:42 PM 0
Share

Awesome! thansk! :)

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ocimum · Dec 18, 2015 at 12:55 PM 0
Share

link is broken

avatar image
0

Answer by dm · Nov 17, 2011 at 12:15 PM

http://www.unifycommunity.com/wiki/index.php?title=LoadAssetBundle

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ocimum · Dec 18, 2015 at 12:55 PM 0
Share

link is broken

avatar image dm_bond ocimum · Dec 18, 2015 at 02:05 PM 0
Share

http://wiki.unity3d.com/index.php/LoadAssetBundle

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can AssetDatabase.LoadAllAssetsAtPath Load All Assets Recursively? 2 Answers

Using Asset Bundles with Editor scripts 0 Answers

Add Asset Bundle Name to a prefab in "Editor Code" 1 Answer

Export list of prefab to asset bundles recursively 0 Answers

How to import the object from server to unity 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges