在 Unity 中构建和加载 Assetbundle [英] Build and load Assetbundles in Unity

查看:42
本文介绍了在 Unity 中构建和加载 Assetbundle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 iOS 版本中使用 Unity Assetbundle.

I cannot get Unity Assetbundles working in an iOS build.

在 Unity 中,我构建了资产包:

In Unity I build the assetbundles:

using UnityEditor;

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

它们在 Unity 中运行良好.使用它们

And they work fine in Unity. using them with

AssetBundle bundleLoadRequest = AssetBundle.LoadFromFile("file://" + Application.dataPath + "/AssetBundles/iOS/" + myassetbundlename.ToString());

和/或

WWW wwww = WWW.LoadFromCacheOrDownload("file://" + Application.dataPath + "/AssetBundles/iOS/" + myassetbundlename.ToString(), 4); 

(没有file://"前缀,这些包在 Unity 和 Xcode 中都不起作用)

(Without the "file://" prefix, the bundles won't work in Unity nor Xcode)

我将项目构建到 Xcode 并在 Xcode 中运行它并收到此错误:

I build the project to Xcode and run it in Xcode and receive this error:

无法打开存档文件:/Users/user/Documents/Workspaces/unityproject/Assets/AssetBundles/iOS/lchairanimations

Unable to open archive file: /Users/user/Documents/Workspaces/unityproject/Assets/AssetBundles/iOS/lchairanimations

这可能与设置正确的路径有某种关系,但是当我之后将 assetbundle 文件夹复制到 Xcode 项目时,问题仍然存在.

It might be related somehow to setting the correct path, but as I have copied the assetbundle folder afterwards to Xcode project, the problem persists.

推荐答案

在下面的这个例子中,我将演示如何将名为 "dog" 的新资产添加到名为 "animals 的 AssetBundle" 并构建它,然后在运行时加载它.

In this example below, I will demonstrate how to add new asset called "dog" to our AssetBundle named "animals" and build it then load it during run-time.

设置构建文件夹:

1.选择资产,例如图像文件.在本例中,它是 "dog.jpeg" 文件.请参阅检查器"选项卡中的菜单.有时,AssetBundle 选项是隐藏的,向上拖动以显示它.请参阅下面的动画 gif 以了解如何执行此操作.默认的 AssetBundle 是无".单击无"选项,然后转到新建"选项并创建新的 AssetBundle 并将其命名为animals"

1. Select the asset such as image file. In this case, that's the "dog.jpeg" file. See the menu in the "Inspector" tab. Sometimes, the AssetBundle option it is hidden, drag it up to show it. See the animated gif below for how to do this. The default AssetBundle is "None". Click on the "None" option then go to the "New" option and create new AssetBundle and name it "animals"

2.在 Assets 文件夹中创建一个名为 StreamingAssets 的文件夹.这是我们要在其中构建 AssetBundle 的文件夹.拼写很重要,并且区分大小写,因此请确保正确命名.

2. Create a folder named StreamingAssets in the Assets folder. This is the folder we are going to build the AssetBundle into. Spelling counts and it's case sensitive so make sure to name it correctly.

3.在 StreamingAssets 文件夹中创建子文件夹来保存 AssetBundle.对于此示例,将此文件夹命名为 AssetBundles,以便您可以使用它来识别其中的内容.

3. Create sub-folder in the StreamingAssets folder to hold the AssetBundle. For this example, name this folder AssetBundles so that you can use it to recognize what's in it.

构建 AssetBundle:

4.下面是构建脚本.

A.创建一个名为 ExportAssetBundles 的脚本并将其放入 Assets 文件夹中名为 "Editor" 的文件夹中,然后将下面的代码复制到其中:

A. Create a script named ExportAssetBundles and put it in a folder named "Editor" in the Assets folder then copy the code below inside it:

using System.IO;
using UnityEditor;
using UnityEngine;

public class ExportAssetBundles
{
    [MenuItem("Assets/Build AssetBundle")]
    static void ExportResource()
    {
        string folderName = "AssetBundles";
        string filePath = Path.Combine(Application.streamingAssetsPath, folderName);

        //Build for Windows platform
        BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);

        //Uncomment to build for other platforms
        //BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.iOS);
        //BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.Android);
        //BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.WebGL);
        //BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.StandaloneOSX);

        //Refresh the Project folder
        AssetDatabase.Refresh();
    }
}

B.转到 Assets --> Build AssetBundle 菜单来构建您的 AssetBudle.

B. Build your AssetBudle by going to Assets --> Build AssetBundle menu.

您应该在 Assets/StreamingAssets/AssetBundles 目录中看到构建的 AssetBundles.如果没有,请刷新项目"选项卡.

You should see the built AssetBundles inside the Assets/StreamingAssets/AssetBundles directory. If not, refresh the Project tab.

在运行时加载 AssetBundle:

5.加载时,应该使用Application.streamingAssetsPath 访问StreamingAssets 文件夹.要访问所有使用的文件夹,Application.streamingAssetsPath + "/AssetBundle/" + assetbunlenameWithoutExtension;.AssetBundleAssetBundleRequest API 用于加载 AssetBundle.由于这是一个图像,Texture2D 被传递给它们.如果使用预制件,请传递 GameObject 而不是实例化它.有关应在何处进行这些更改,请参阅代码中的注释.推荐使用 Path.Combine 来组合路径名,所以下面的代码应该使用它.

5. When loading it, Application.streamingAssetsPath should be used to access the StreamingAssets folder. To access all the folders use, Application.streamingAssetsPath + "/AssetBundle/" + assetbunlenameWithoutExtension;. The AssetBundle and AssetBundleRequest API are used to load the AssetBundle. Since this is an image, Texture2D is passed to them. If using a prefab, pass GameObject instead then instantiate it. See comment in code for where these changes should be made. It is recommended to use Path.Combine to combine path names so the code below should use that instead.

下面是一个简单的加载函数:

Below is a simple loading function:

IEnumerator LoadAsset(string assetBundleName, string objectNameToLoad)
{
    string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundles");
    filePath = System.IO.Path.Combine(filePath, assetBundleName);

    //Load "animals" AssetBundle
    var assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(filePath);
    yield return assetBundleCreateRequest;

    AssetBundle asseBundle = assetBundleCreateRequest.assetBundle;

    //Load the "dog" Asset (Use Texture2D since it's a Texture. Use GameObject if prefab)
    AssetBundleRequest asset = asseBundle.LoadAssetAsync<Texture2D>(objectNameToLoad);
    yield return asset;

    //Retrieve the object (Use Texture2D since it's a Texture. Use GameObject if prefab)
    Texture2D loadedAsset = asset.asset as Texture2D;

    //Do something with the loaded loadedAsset  object (Load to RawImage for example) 
    image.texture = loadedAsset;
}

加载笔记前要做的事情:

Things to before loading note:

A.Assetbundle 的名称是 animals.

A. Name of Assetbundle is animals.

B.我们要从动物 Assetbundle 加载的资产/对象的名称是 dog 这是一个简单的狗的 jpg.

B. Name of the asset/object we want to load from the animals Assetbundle is dog This is a simple jpg of a dog.

C.加载很简单:

string nameOfAssetBundle = "animals";
string nameOfObjectToLoad = "dog";

public RawImage image; 

void Start()
{
    StartCoroutine(LoadAsset(nameOfAssetBundle, nameOfObjectToLoad));
}

这篇关于在 Unity 中构建和加载 Assetbundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆