如何在Unity中的Android上本地测试AssetBundle(DLC)?

时间:2019-02-13 16:39:32

标签: android unity3d assets assetbundle

我正在尝试为当前正在开发的游戏测试一些AssetBundles。我想使用它,因为我不想游戏占用很多空间。我仍然不知道服务器如何工作以及如何将它们上传到那里。我正在搜索操作方法,并在此处找到一些整洁的东西:AssetBundle (DLC) data load to Android phone at runtime [UNITY] 但这说明了有关上传到服务器和其他内容的一些信息,但是我想在本地进行测试。有什么建议吗?

按照雷米的告诉我,我的代码如下:

public class LoadFromFileExample : MonoBehaviour {

// Use this for initialization
void Start () {

    string fileName = "planes";
    var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, fileName));
    if (myLoadedAssetBundle == null)
    {
        Debug.Log("Failed to load AssetBundle!");
        return;
    }
    var prefab = myLoadedAssetBundle.LoadAsset< GameObject > ("andy");
    Instantiate(prefab);

    myLoadedAssetBundle.Unload(false);

}

// Update is called once per frame
void Update () {

}
} 

但是它显示以下错误:Unable to open archive file: C:/Users/Chris/Desktop/myDLC/Assets/StreamingAssets/myassetBundle

This is the asset bundle name

1 个答案:

答案 0 :(得分:0)

看看AssetBundle.LoadFromFile

这使您可以从设备的本地存储中加载资产捆绑文件。意味着您不必先上传/下载它们。

它将类似于以下内容:

string fileName = "fooAssetBundle";//name of the assetbundle you want to load

var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, fileName));//Creates a filepath starting at the streamingAssetsPath and appends filename to it. 

var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("MyObject");//Create a GameObject from the assetbundle
Instantiate(prefab);//instantiate the GameObject

myLoadedAssetBundle.Unload(false);//Unload the assetbundle from memory as it isn't used anymore

上面的示例使用Application.StreamingAssetsPath,但这可以是您想要的任何路径,例如Application.PersistentDataPath或外部存储。