我试图在运行时将已经创建的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脚本编写的新手,谢谢

最佳答案

在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”

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

10-06 07:36