在Unity中构建和加载Assetbundle

Gid*_*ons 7 c# xcode unity-game-engine ios assetbundle

我不能让Unity Assetbundles在iOS版本中工作.

在Unity中,我构建了assetsbundles:

using UnityEditor;

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

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

AssetBundle bundleLoadRequest = AssetBundle.LoadFromFile("file://" + Application.dataPath + "/AssetBundles/iOS/" + myassetbundlename.ToString());
Run Code Online (Sandbox Code Playgroud)

和/或

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

(如果没有"file://"前缀,捆绑包将无法在Unity或Xcode中工作)

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

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

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

Pro*_*mer 20

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

设置构建文件夹:

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

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述


构建AssetBundle:

4.下面是构建脚本.

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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

在此输入图像描述


在运行时加载AssetBundle:

5.加载时,Application.streamingAssetsPath应该用来访问该StreamingAssets文件夹.要访问所有文件夹,请使用,Application.streamingAssetsPath + "/AssetBundle/" + assetbunlenameWithoutExtension;.的AssetBundleAssetBundleRequest 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);

    //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;
}
Run Code Online (Sandbox Code Playgroud)

装货前的事情:

.Assetbundle的名称是animals.

.我们想要从动物身上加载的资产/对象的名称Assetbundle是dog一只狗的简单jpg.

在此输入图像描述

Ç.加载很简单,因为:

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

public RawImage image; 

void Start()
{
    StartCoroutine(LoadAsset(nameOfAssetBundle, nameOfObjectToLoad));
}
Run Code Online (Sandbox Code Playgroud)