1

私は現在 Unity 5.3.4 を使用していますが、Asset Bundles を使いこなすのはちょっと難しいので、プロジェクトでそれらを使用する必要があります。現在の私の主な問題は、シーンとそれに依存するオブジェクトを含むバンドルを作成できないことです。わかりました。「cena1」であるメイン バンドルと、そのオブジェクトのバンドル「objcena1」があり、cena1 を見てください。マニフェストを見ると、"Dependencies:" フィールドがあり、その後に 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 の方が実装しやすいと思っていたので、かなり頭が痛くなりました。これらのコードは両方とも unity の Web サイトにチュートリアルとして表示されています。スクリプトを除いて、プロジェクトのアセット フォルダーのアセットバンドルに存在する必要があるアセットはありません。アセットバンドルのロジックによれば、スクリプトにはすべてのアセットが含まれているはずですよね? さて、ここに新しいコードがありますが、これも機能しません。

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)
    }
}
4

0 に答える 0