在下面的示例中,我将演示如何添加名为"dog"到我们的 AssetBundle 命名为“动物”并构建它,然后在运行时加载它。
设置构建文件夹:
1。选择图像文件等资产。在这种情况下,就是“狗.jpeg”文件。请参阅“检查器”选项卡中的菜单。有时,AssetBundle 选项是隐藏的,向上拖动即可显示。请参阅下面的动画 gif 了解如何执行此操作。默认的AssetBundle是“None”。单击"None"选项,然后转到"New"选项并创建新的 AssetBundle 并将其命名为“animals”
2。创建一个名为的文件夹StreamingAssets
在资产文件夹中。这是我们将在其中构建 AssetBundle 的文件夹。拼写很重要,并且区分大小写,因此请确保正确命名。
3。在中创建子文件夹StreamingAssets
保存 AssetBundle 的文件夹。对于此示例,将此文件夹命名为AssetBundles
这样你就可以用它来识别其中的内容。
构建 AssetBundle:
4。下面是构建脚本。
A。创建一个名为ExportAssetBundles
并将其放入名为的文件夹中"Editor"在 Assets 文件夹中,然后将以下代码复制到其中:
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。通过以下方式构建您的 AssetBudleAssets --> 构建AssetBundle menu.
您应该在以下位置看到构建的 AssetBundlesAssets/StreamingAssets/AssetBundles
目录。如果没有,请刷新“项目”选项卡。
在运行时加载 AssetBundle:
5。加载时,Application.streamingAssetsPath
应该用于访问StreamingAssets
文件夹。要访问所有文件夹,请使用Application.streamingAssetsPath + "/AssetBundle/" + assetbunlenameWithoutExtension;
. The AssetBundle
and 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 的名称是animals
.
B。我们要从动物 Assetbundle 加载的资产/对象的名称是dog
这是一张简单的狗的 jpg 图像。
C。加载很简单,如下所示:
string nameOfAssetBundle = "animals";
string nameOfObjectToLoad = "dog";
public RawImage image;
void Start()
{
StartCoroutine(LoadAsset(nameOfAssetBundle, nameOfObjectToLoad));
}