首页 文章

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

提问于
浏览
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中编写脚本全新,谢谢

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”

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

相关问题