c# – 在Unity中构建和加载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);
    }
 }

它们在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中运行它并收到此错误:

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

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

解决方法:

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

设置构建文件夹:

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

c# – 在Unity中构建和加载Assetbundle

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

c# – 在Unity中构建和加载Assetbundle

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

c# – 在Unity中构建和加载Assetbundle

构建AssetBundle:

4.下面是构建脚本.

A.创建一个名为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();
    }
}

c# – 在Unity中构建和加载Assetbundle

B.转到资产来构建您的AssetBudle – >构建AssetBundle菜单.

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

c# – 在Unity中构建和加载Assetbundle

在运行时加载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);

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

装货前的事情:

A. Assetbundle的名称是动物.

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

c# – 在Unity中构建和加载Assetbundle

C.装载很简单,因为:

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

public RawImage image; 

void Start()
{
    StartCoroutine(LoadAsset(nameOfAssetBundle, nameOfObjectToLoad));
}
上一篇:AssetBundle包/ab包加载材质球材质丢失


下一篇:AssetBundleMaster_ReadMe_EN