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

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

问题描述

我无法让Unity Assetbundles在iOS版本中运行。



在Unity中我使用UnityEditor构建资产包:

 ; 

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

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

  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中运行)



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


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


它可能与某种方式设置正确的路径有关,但由于我之后将assetbundle文件夹复制到Xcode项目,问题仍然存在。

解决方案

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



设置构建文件夹:



<强> 1 即可。选择图像文件等资产。在这种情况下,这是dog.jpeg文件。请参阅检查器选项卡中的菜单。有时,它被隐藏的AssetBundle选项,将其拖动以显示它。请参阅下面的动画gif,了解如何执行此操作。 默认的AssetBundle是无。点击选项,然后转到新建选项并创建新的AssetBundle并将其命名为animals





2 。在Assets文件夹中创建名为 StreamingAssets 的文件夹。这是我们要将AssetBundle构建到的文件夹。拼写计数并且区分大小写,因此请确保正确命名。





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








构建AssetBundle:



4 。以下是构建脚本。



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

 使用System.IO; 
使用UnityEditor;
使用UnityEngine;

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

//为Windows平台构建
BuildPipeline.BuildAssetBundles(filePath,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);

//取消注释为其他平台构建
//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);

//刷新Project文件夹
AssetDatabase.Refresh();
}
}






在运行时加载AssetBundle



5 。加载时,应使用 Application.streamingAssetsPath 来访问 StreamingAssets 文件夹。要访问所有文件夹,请使用 Application.streamingAssetsPath +/ AssetBundle /+ assetbunlenameWithoutExtension; AssetBundle AssetBundleRequest API用于加载AssetBundle。由于这是一张图片,因此将 Texture2D 传递给他们。如果使用预制件,则传递 GameObject ,然后实例化它。请参阅代码中的注释,了解应在何处进行更改。建议使用 Path.Combine 来组合路径名,以便下面的代码改为使用它。



是一个简单的加载函数:

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

//加载animalsAssetBundle
var assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(filePath);
收益率返回assetBundleCreateRequest;

AssetBundle asseBundle = assetBundleCreateRequest.assetBundle;

//加载 狗 资产(使用的Texture2D因为它是一个纹理使用游戏物体如果预制。)
AssetBundleRequest资产= asseBundle.LoadAssetAsync<&的Texture2D GT;(objectNameToLoad);
收益率回报资产;

//检索对象(使用Texture2D,因为它是纹理。如果预制,则使用GameObject)
Texture2D loadedAsset = asset.asset as Texture2D;

//使用加载的loadedAsset对象执行某些操作(例如,加载到RawImage)
image.texture = loadedAsset;
}

加载前的事项说明:



A 。 Assetbundle的名称是 animals



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





C 。加载很简单,因为:

  string nameOfAssetBundle =animals; 
string nameOfObjectToLoad =dog;

public RawImage image;

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


I cannot get Unity Assetbundles working in an iOS build.

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);
    }
 }

And they work fine in Unity. using them with

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

and/or

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

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

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

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

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.

解决方案

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.

Setting Up Build Folders:

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. 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. 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.


Building AssetBundle:

4. Below is the build script.

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. Build your AssetBudle by going to Assets --> Build AssetBundle menu.

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


Loading the AssetBundle during run-time:

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. Name of Assetbundle is animals.

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. The loading is simple as this:

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

public RawImage image; 

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

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

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