2016-05-11 2 views
0

私は1つのプログラミングでは比較的新しいので、以前は資産バンドルを使ったことがなかった。私は誰もが単一性のウェブサイトからダウンロードし、それを私のニーズに適応させることができるサンプルプロジェクトを使用しています、私はすでに必要なものですが、ロードシーンの機能を使用する方法を知っていますが、現在使用しているのはアセットバンドルをダウンロードせず、コンピュータ内のどこかからロードします。 私は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. 
    } 

} 

}

関連する問題