首页 文章

Unity 5.3 Assetbundles

提问于
浏览
0

我在unity 5.3项目中使用了 AssetBundleManager API .

我的设置:我有两个场景 . 一个主场景和另一个场景将作为多场景加载到主场景中,现在在编辑器中工作正常 . 现在我想将我的主场景构建到我的Android设备上,但是当我点击加载级别按钮时,没有任何反应 . 我已经 Build 了我的assetbundle,它放在我的root的 AssetBundles 文件夹中 . 这是我在文件中读到的内容,应该是什么?

enter image description here

我缺少什么,我需要将assetsbundle文件放在其他地方吗?我正在使用这个脚本,我从 AssetBundleManager 插件的例子中得到了这个脚本 .

using UnityEngine;
using System.Collections;
using AssetBundles;
using UnityEngine.SceneManagement;

public class LoadScenes : MonoBehaviour
{

public string sceneAssetBundle;
public string sceneName;
public bool load;
public bool destroy;
string curLevel;

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

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

void Update(){
    if (load) {
        load = false;
        // Load level.
        StartCoroutine(InitializeLevelAsync (sceneName, true) );
    }

    if (destroy) {
        destroy = false;
        SceneManager.UnloadScene(sceneName);
    }
}

public void loadLevel(string level){
    curLevel = level;
    StartCoroutine (InitializeLevelAsync (level, true));
}

public void unloadLevel(){
    SceneManager.UnloadScene(curLevel);
}

// Initialize the downloading url and AssetBundleManifest object.
protected 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);
}

protected 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" );
}
}

2 回答

  • 0

    而不是使用:

    AssetBundleManager.SetSourceAssetBundleURL(Application.dataPath“/”);

    尝试使用Application.persistentDataPath,希望这将解决问题 . Link到手册 .

  • 0

    您必须将数据路径设置为assetbundle所在的文件夹,即myassetbundle.unity3d

    例如:-

    AssetBundleManager.SetSourceAssetBundleURL(Application.dataPath“file://// D:\ ptech-user \ Documents \ assetbundle3 \ Assets \ Assets \ AssetBundle”);

相关问题