2016-05-11 46 views
0

我对统一编程比较陌生,以前从未使用过assetbundles。我正在使用示例项目,每个人都可以从统一网站下载并根据需要进行调整,我已经知道如何使用加载场景功能,这是我需要的,但是加载场景脚本是我目前使用的不是下载资产捆绑包,而是从计算机中的某个地方加载它。 我正在开发一个Android/IOS应用程序,我们的目标是创建一个简单的菜单场景,然后从服务器下载资源包并加载场景。一旦用户下载,所有数据都需要存储在手机中。我尝试了一切,但我无法使它工作,即使在统一文档中的代码似乎不适用于我。请,如果有人能帮助我,这里是LoadScenes脚本的代码。我对统一资产包管理器附带的原始代码进行的唯一修改是,包名称和场景名称由按钮传递。该脚本当前从计算机中的文件夹加载捆绑包,这不是我所需要的,我需要从服务器下载捆绑包,然后从设备中的文件夹加载捆绑包。谢谢!LoadFromCacheOrDownload的正确用法是什么?

using UnityEngine; 
using System.Collections; 
using AssetBundles; 
using UnityEngine.UI; 


public class LoadScenes : MonoBehaviour{ 

public string sceneAssetBundle; 
public string sceneName; 
public string sName; 
public string bName; 

// Use this for initialization 
IEnumerator Start() 
{ 
    yield return StartCoroutine(Initialize()); 

    // Load level. 
    yield return StartCoroutine(InitializeLevelAsync (sceneName, true)); 
} 

public void getScene(string sName){ 
    sceneName = sName; 

} 

public void getBundle(string bName){ 
    sceneAssetBundle = bName; 

} 
    // Initialize the downloading url and AssetBundleManifest object. 
public IEnumerator Initialize(){ 


    // Don't destroy this gameObject as we depend on it to run the loading script. 
    //DontDestroyOnLoad(gameObject); 

    // With this code, when in-editor or using a development builds: Always use the AssetBundle Server 
    // (This is very dependent on the production workflow of the project. 
    // Another approach would be to make this configurable in the standalone player.) 
    #if DEVELOPMENT_BUILD || UNITY_EDITOR 
    AssetBundleManager.SetDevelopmentAssetBundleServer(); 
    #else 
    // Use the following code if AssetBundles are embedded in the project for example via StreamingAssets folder etc: 
    AssetBundleManager.SetSourceAssetBundleURL(Application.dataPath + "/"); 
    // Or customize the URL based on your deployment or configuration 
    AssetBundleManager.SetSourceAssetBundleURL("http://www.MyWebsite/MyAssetBundles"); 
    #endif 

    // Initialize AssetBundleManifest which loads the AssetBundleManifest object. 
    var request = AssetBundleManager.Initialize(); 

    if (request != null) 
     yield return StartCoroutine(request); 
} 





public IEnumerator InitializeLevelAsync (string levelName, bool isAdditive) 
{ 
    // This is simply to get the elapsed time for this phase of AssetLoading. 
    float startTime = Time.realtimeSinceStartup; 

    // Load level from assetBundle. 
    AssetBundleLoadOperation request = AssetBundleManager.LoadLevelAsync(sceneAssetBundle, levelName, isAdditive); 
    if (request == null) 
     yield break; 
    yield return StartCoroutine(request); 

    // Calculate and display the elapsed time. 
    float elapsedTime = Time.realtimeSinceStartup - startTime; 
    Debug.Log("Finished loading scene " + levelName + " in " + elapsedTime + " seconds"); 
} 
} 
+0

LoadFromCacheOrDownload的正确用法是提供包含您尝试下载的资产包的URL的版本号。也就是说,你会说它会是什么版本。如果您之前对相同版本号的同一捆绑包进行了调用,它将尝试从缓存中加载它。如果没有,它会从服务器下载它。但是,你想要的行为似乎只是下载文件并存储它。在这种情况下,请勿使用LoadFromCacheOrDownload。 – Bart

回答

0

我是用例如上面的困惑,所以我创造了我自己的脚本下载场景形成的资产bundle.After创造一个场景或场景的资产捆绑使用下面的代码加载场景: -

public class LoadScene : MonoBehaviour { 
//public Variables 
public string url; // url where your asset bundle is present, can be your hard disk or ftp server 
public string AssetBundleName; 
public string levelName; 
public int version; 

//private variables 
private AssetBundle assetBundle; 

/*Corountines 
By using this, the function will simply stop in that point until the WWW object is done downloading, 
but it will not block the execution of the rest of the code, it yields until it is done.*/ 
protected IEnumerator LoadTheScene() 
{ 
    if (!Caching.IsVersionCached(url + "/" + AssetBundleName, version)){ 
     WWW www = WWW.LoadFromCacheOrDownload(url + "/" + AssetBundleName, version); 
     yeild return www; 
     assetBundle = www.assetBundle; 
     www.Dispose(); 
     if (assetBundle != null) 
     { 
      string[] path = assetBundle.GetAllScenePaths(); 
      //below code is for finding the "scene name" from the bundle 
      foreach (string temp in path) 
      { 
       Debug.Log(temp); 
       string[] name = temp.Split('/'); 
       string[] sceneName = name[name.Length - 1].Split('.'); 
       string result = sceneName[0]; 
       if (result == levelName) 
       { 
        yield return (SceneManager.LoadSceneAsync(result)); 
       } 
      } 
     } 

    } 

    else{ 
     Debug.Log("Asset Already Cached..."); 
     yield return Caching.CleanCache(); 
     //After using an asset bundle you should unload it otherwise an exception will be thrown saying asset bundle is already loaded.. if you use WWW.LoadFromCacheOrDownload again. 
    } 

} 

}