2014-10-31 90 views
0

我正在試驗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://stackoverflow.com/questions/33434835/creating-and-downloading-asset-bundle-from-local-server-in-unity-5 – 2015-10-30 14:42:49

回答

1

問題是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()); 
    } 
}