如何自动从AssetBundle加载所有依赖项?

时间:2016-05-12 17:53:39

标签: unity3d assetbundle

我目前正在使用Unity 5.3.4,而且我很难使用资产套装,我们确实需要在我们的项目中使用它们。我现在面临的主要困难是,你不能把它与一个场景及其依赖对象捆绑在一起,好吧,我有一个主要的捆绑,这是" cena1"和一捆对象" objcena1",看着cena1清单,我可以看到有一个"依赖关系:"字段,后跟我的objcena1包所在的路径。但是,出于某种原因,当我从" cena1"加载场景时,所有对象都丢失了。我怎么解决这个问题?谢谢!

快速编辑:资产包位于我的计算机的不同文件夹中,仅用于测试。

代码的另一个编辑:

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

public class LoadScenes : MonoBehaviour
{
    public string sceneAssetBundle;
    public string sceneName;
    public string sName;
    public string bName;
    public string BundleURL;
    public string AssetName;
    public int version;

    void Start() {
        StartCoroutine (DownloadAndCache());
    }

    IEnumerator DownloadAndCache (){
        // Wait for the Caching system to be ready
        while (!Caching.ready)
            yield return null;

        // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
        using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
            yield return www;
            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);
            AssetBundle bundle = www.assetBundle;
            if (AssetName == "")
                ;
            else
                StartCoroutine (Initialize ());
                StartCoroutine(InitializeLevelAsync(sceneName,true));
            // Unload the AssetBundles compressed contents to conserve memory
            bundle.Unload(false);

        } // memory is freed from the web stream (www.Dispose() gets called implicitly)
    }

    IEnumerator Start1() {
        using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
            yield return www;
            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);
            AssetBundle bundle = www.assetBundle;
            if (AssetName == ""){
                Instantiate(bundle.mainAsset);

            }
        }

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

编辑13/05:尝试另一个代码,这一次,它甚至没有加载它应该的场景,它只是说我要实例化的东西是null。我的意思是,assetBundles让我头疼,认为它更容易实现。这两个代码都显示为团结网站上的教程,而且,我已经尝试了将近一周而没有结果。除了脚本之外,在项目的assets文件夹中的assetsbundle中不存在任何资产,根据assetbundles逻辑,它们应该包含所有资产,对吧?好吧,这是新代码,它不起作用。

using System;
using UnityEngine;
using System.Collections;

public class LoadScenes : MonoBehaviour {
    public string BundleURL;
    public string AssetName;
    public int version;

    void Start() {
        StartCoroutine (DownloadAndCache());
    }

    IEnumerator DownloadAndCache (){
        // Wait for the Caching system to be ready
        while (!Caching.ready)
            yield return null;

        // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
        using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
            yield return www;
            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);
            AssetBundle bundle = www.assetBundle;
            if (AssetName == "")
                Instantiate(bundle.mainAsset);
            else
                Instantiate(bundle.LoadAsset(AssetName));
            // Unload the AssetBundles compressed contents to conserve memory
            bundle.Unload(false);

        } // memory is freed from the web stream (www.Dispose() gets called implicitly)
    }
}

0 个答案:

没有答案