Unity でのプログラミングは比較的新しく、アセットバンドルを使用したことはありません。Unity の Web サイトから誰もがダウンロードできるサンプル プロジェクトを使用し、それを自分のニーズに合わせて調整しています。シーンの読み込み機能の使用方法は既に知っていますが、これは必要なものですが、シーンの読み込みスクリプトは現在使用中は、アセット バンドルをダウンロードするのではなく、既にコンピューター内のどこかからロードします。私は Android/IOS アプリケーションに取り組んでいます。私たちの目的は、単純なメニュー シーンを作成し、サーバーからアセット バンドルをダウンロードしてシーンをロードすることです。ユーザーがダウンロードしたら、すべてのデータを電話機に保存する必要があります。私はすべてを試しましたが、それを機能させることができません.unityドキュメントのコードでさえ、私にとっては機能しないようです. 助けてくれる人がいたらここでお願いします」LoadScenes スクリプトのコード。Unity のアセット バンドル マネージャーに付属する元のコードに対して行った唯一の変更は、バンドルの名前とシーンの名前がボタンによって渡されることです。このスクリプトは現在、コンピューターのフォルダーからバンドルをロードしますが、これは必要なものではありません。バンドルをサーバーからダウンロードしてから、デバイスのフォルダーからロードする必要があります。ありがとう!
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" );
}
}