我正在尝试使用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时就停止了。
如果有人可以帮助我,我将不胜感激。
非常感谢
问题是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());
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句