在 Unity 中构建和加载 Assetbundle
Build and load Assetbundles in Unity
我无法让 Unity Assetbundles 在 iOS 构建中工作。
在 Unity 中我构建了资产包:
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());
and/or
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 中104=] 并构建它,然后在 运行 时间内加载它。
设置构建文件夹:
1。 Select 图片文件等资产。在本例中,即 "dog.jpeg" 文件。请参阅 "Inspector" 选项卡中的菜单。有时,AssetBundle 选项是隐藏的,将其向上拖动即可显示。有关如何执行此操作的信息,请参见下面的动画 gif。 默认的 AssetBundle 是 "None"。单击 "None" 选项然后转到 "New" 选项并创建新的 AssetBundle 并将其命名为 "animals"
2。在 Assets 文件夹中创建一个名为 StreamingAssets
的文件夹。这是我们要将 AssetBundle 构建到的文件夹。拼写很重要,并且区分大小写,因此请确保正确命名。
3。在 StreamingAssets
文件夹中创建子文件夹以保存 AssetBundle。对于此示例,将此文件夹命名为 AssetBundles
,以便您可以使用它来识别其中的内容。
构建 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();
}
}
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;
}
加载前的注意事项:
A。 Assetbundle 的名称是 animals
.
B。我们要从动物 Assetbundle 加载的 asset/object 的名称是 dog
这是一只狗的简单 jpg。
C。加载很简单:
string nameOfAssetBundle = "animals";
string nameOfObjectToLoad = "dog";
public RawImage image;
void Start()
{
StartCoroutine(LoadAsset(nameOfAssetBundle, nameOfObjectToLoad));
}
我还有一个建议可能更适合像我这样想要更多基于 .Net 的开发人员。您可以考虑使用 Unity 构建 "Launcher"。我会使用 Unity 构建它,因此它仍然是跨平台的。接下来,考虑将您的更新捆绑在 DLL 中,因为 Unity 支持 .Net 框架或将它们作为图像存储在 Web 文件夹中。您可以使用 Webclient 获取数据。文章在这里:
启动器可能是获取或更新内容的更好方式。自从我玩指环王 Online 以来,我一直在考虑这个想法,我喜欢他们的 Launcher。所以我想为自己的游戏制作一个。在您的代码中,您将使用 "DownloadFile(Uri, String)" 来获取数据。我会使用 API 或其他东西来首先检查您当前的游戏版本或 DLL 等。然后检查数据库中的最新版本。最后,API 或其他东西可以构建所需的新文件或所需更新文件的列表,然后循环请求文件并下载你需要的文件。
我无法让 Unity Assetbundles 在 iOS 构建中工作。
在 Unity 中我构建了资产包:
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());
and/or
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 中104=] 并构建它,然后在 运行 时间内加载它。
设置构建文件夹:
1。 Select 图片文件等资产。在本例中,即 "dog.jpeg" 文件。请参阅 "Inspector" 选项卡中的菜单。有时,AssetBundle 选项是隐藏的,将其向上拖动即可显示。有关如何执行此操作的信息,请参见下面的动画 gif。 默认的 AssetBundle 是 "None"。单击 "None" 选项然后转到 "New" 选项并创建新的 AssetBundle 并将其命名为 "animals"
2。在 Assets 文件夹中创建一个名为 StreamingAssets
的文件夹。这是我们要将 AssetBundle 构建到的文件夹。拼写很重要,并且区分大小写,因此请确保正确命名。
3。在 StreamingAssets
文件夹中创建子文件夹以保存 AssetBundle。对于此示例,将此文件夹命名为 AssetBundles
,以便您可以使用它来识别其中的内容。
构建 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();
}
}
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;
}
加载前的注意事项:
A。 Assetbundle 的名称是 animals
.
B。我们要从动物 Assetbundle 加载的 asset/object 的名称是 dog
这是一只狗的简单 jpg。
C。加载很简单:
string nameOfAssetBundle = "animals";
string nameOfObjectToLoad = "dog";
public RawImage image;
void Start()
{
StartCoroutine(LoadAsset(nameOfAssetBundle, nameOfObjectToLoad));
}
我还有一个建议可能更适合像我这样想要更多基于 .Net 的开发人员。您可以考虑使用 Unity 构建 "Launcher"。我会使用 Unity 构建它,因此它仍然是跨平台的。接下来,考虑将您的更新捆绑在 DLL 中,因为 Unity 支持 .Net 框架或将它们作为图像存储在 Web 文件夹中。您可以使用 Webclient 获取数据。文章在这里:
启动器可能是获取或更新内容的更好方式。自从我玩指环王 Online 以来,我一直在考虑这个想法,我喜欢他们的 Launcher。所以我想为自己的游戏制作一个。在您的代码中,您将使用 "DownloadFile(Uri, String)" 来获取数据。我会使用 API 或其他东西来首先检查您当前的游戏版本或 DLL 等。然后检查数据库中的最新版本。最后,API 或其他东西可以构建所需的新文件或所需更新文件的列表,然后循环请求文件并下载你需要的文件。