Unity 3D:使用AssetBundle动态加载FBX

时间:2013-11-11 13:46:49

标签: c# unity3d

我正在尝试在运行时将已创建的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中编写脚本全新,谢谢

1 个答案:

答案 0 :(得分: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”

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