Unity3d加载assetsbundle

问题描述:

我正在试验Unity3d AssetBundles。我试图用它的对象加载一个场景。我有我的创造资产包这个简单的代码:Unity3d加载assetsbundle

[MenuItem ("Build/BuildAssetBundle")] 
static void myBuild(){ 
    string[] levels = {"Assets/main.unity"}; 
    BuildPipeline.BuildStreamedSceneAssetBundle(levels,"Streamed-Level1.unity3d",BuildTarget.Android); 
} 

我用上面的代码从在有摄像头,并在中心的立方体的场景搭建的资产包。

,我有这样的代码来加载它:

using UnityEngine; 
using System.Collections; 

public class loader : MonoBehaviour { 

    public GUIText debugger; 
    private string url = "http://www.myurl.com/Streamed-Level1.unity3d"; 
    // Use this for initialization 
    IEnumerator Start() { 
     Debug.Log("starting"); 
     WWW www = WWW.LoadFromCacheOrDownload(url,1); 
     if(www.error != null) 
     { 
      Debug.LogError(www.error); 
     } 
     yield return www; 
     Debug.Log("after yield"); 
     AssetBundle bundle = www.assetBundle; 
     bundle.LoadAll(); 
     Debug.Log("loaded all"); 
     Application.LoadLevel("main"); 


    } 

    // Update is called once per frame 
    void Update() { 

    } 
} 

的问题是似乎是当它到达去LOADALL停止。

如果有人能帮助我,我将不胜感激。

非常感谢

+0

你的问题几乎是我的答案,但问题是“Streamed-Level1.unity3d”将保存在哪里? – 2015-10-30 14:39:32

+0

你有回答这个任务吗? http://*.com/questions/33434835/creating-and-downloading-asset-bundle-from-local-server-in-unity-5 – 2015-10-30 14:42:49

问题是C#有迭代器/发电机组等,看起来像一个功能,但他们没有。所以你的代码只是创建迭代器,但不要运行它。使用StartCoroutine加载资源:

using UnityEngine; 
using System.Collections; 

public class BundleLoader : MonoBehaviour{ 
    public string url; 
    public int version; 
    public IEnumerator LoadBundle(){ 
     using(WWW www = WWW.LoadFromCacheOrDownload(url, version){ 
      yield return www; 
      AssetBundle assetBundle = www.assetBundle; 
      GameObject gameObject = assetBundle.mainAsset as GameObject; 
      Instantiate(gameObject); 
      assetBundle.Unload(false); 
     } 
    } 
    void Start(){ 
     StartCoroutine(LoadBundle()); 
    } 
}