2016-05-11 50 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. 
    } 

} 

}