gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:02:10 26 4
gpt4 key购买 nike

我无法在 iOS 构建中使用 Unity Assetbundle。

在 Unity 中我构建了 Assets 包:

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" 的新 Assets 添加到我们名为 "animals" 的 AssetBundle 中并构建它,然后在运行期间加载它-时间。

设置构建文件夹:

1。选择图像文件等 Assets 。在本例中,它是 “dog.jpeg” 文件。请参阅“检查器”选项卡中的菜单。有时,AssetBundle 选项是隐藏的,将其向上拖动即可显示。有关如何执行此操作的信息,请参见下面的动画 gif。 默认的 AssetBundle 是“无”。点击“None”选项,然后转到“New”选项并创建新的AssetBundle并将其命名为“animals”

enter image description here

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

enter image description here

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

enter image description here


构建 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();
}
}

enter image description here

B。通过转到 Assets --> Build AssetBundle 菜单构建您的 AssetBudle。

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

enter image description here


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

加载前的注意事项:

一个。 Assetbundle 的名称是 animals

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

enter image description here

C。加载很简单:

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

public RawImage image;

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

关于c# - 在 Unity 中构建和加载 Assetbundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47030894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com