2

我正在尝试在运行时将已创建的 FBX 对象加载到场景中,我四处搜索发现可以使用assetbundle 来执行此操作。我尝试了这段代码,但它似乎没有实例化场景中的对象,也没有弹出错误。

这是代码

using System;
using UnityEngine;
using System.Collections;

public class CachingLoadExample : 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.Load(AssetName));
            // Unload the AssetBundles compressed contents to conserve memory
                    bundle.Unload(false);

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

我添加了一个新的空游戏对象,将 C# 代码拖到该游戏对象,提供了资产包链接“file://C:/Users/Sahibzada/Documents/New Unity Project/Assets/100777102370.FBX”但没有运气

有人可以指导我,代码有什么问题,我对 Unity 中的脚本完全陌生,谢谢

4

1 回答 1

2

在 Assets 文件夹中创建一个名为 AssetBundles 的文件夹,然后您需要使用编辑器脚本创建 FBX 的 AssetBundle,如下所示:http ://docs.unity3d.com/Manual/BuildingAssetBundles.html

using UnityEditor;

public class CreateAssetBundles
{
     [MenuItem ("Assets/Build AssetBundles")]
     static void BuildAllAssetBundles ()
     {
         BuildPipeline.BuildAssetBundles ("Assets/AssetBundles");
     }
}

最后,您需要在 BundleURL 中引入新 AssetsBundle 的 URL:

"file://C:/Users/Sahibzada/Documents/New Unity Project/Assets/AssetsBundles/yourassetbundle" 

在 AssetName 上你需要引入“yourassetbundle”

我还建议不要使用带有空格的路径,这也可能是一个问题。

于 2016-05-11T10:17:12.850 回答