在下面的示例中,我将演示如何将名为 "dog" 的新资产添加到名为 "animals" 的 AssetBundle 中并构建它,然后在运行期间加载它 -时间。
设置构建文件夹:
1。选择图片文件等资产。在本例中,即为 "dog.jpeg" 文件。请参阅“检查器”选项卡中的菜单。有时,AssetBundle 选项是隐藏的,向上拖动即可显示。请参阅下面的动画 gif 了解如何执行此操作。 默认 AssetBundle 是“无”。点击 "None" 选项,然后转到 "New" 选项并创建新 AssetBundle 并将其命名为“animals”
2。在 Assets 文件夹中创建一个名为 StreamingAssets 的文件夹。这是我们要将 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();
}
}
B。通过转到 Assets --> Build AssetBundle 菜单来构建您的 AssetBudle。
您应该会在 Assets/StreamingAssets/AssetBundles 目录中看到已构建的 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;
}
加载前注意事项:
一个。 Assetbundle 的名称是animals。
B。我们要从动物 Assetbundle 加载的资产/对象的名称是 dog 这是狗的简单 jpg。
C。加载很简单:
string nameOfAssetBundle = "animals";
string nameOfObjectToLoad = "dog";
public RawImage image;
void Start()
{
StartCoroutine(LoadAsset(nameOfAssetBundle, nameOfObjectToLoad));
}