0

我正在试验 Unity3d AssetBundles。我正在尝试使用其对象加载场景。我有这个简单的代码来创建我的资产包:

 [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 时它停止了。

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

非常感谢

4

1 回答 1

2

问题是 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());
    }
}
于 2014-11-21T11:39:22.743 回答