【问题标题】:Build and load Assetbundles in Unity在 Unity 中构建和加载 Assetbundle
【发布时间】:2018-04-12 08:32:47
【问题描述】:

我无法让 Unity Assetbundle 在 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());

和/或

WWW wwww = WWW.LoadFromCacheOrDownload("file://" + Application.dataPath + "/AssetBundles/iOS/" + myassetbundlename.ToString(), 4); 

(如果没有“file://”前缀,bundle 将无法在 Unity 和 Xcode 中运行)

我将项目构建到 Xcode 并在 Xcode 中运行它并收到此错误:

无法打开存档文件: /Users/user/Documents/Workspaces/unityproject/Assets/AssetBundles/iOS/lchairanimations

这可能与设置正确的路径有关,但由于我后来将assetbundle文件夹复制到Xcode项目,问题仍然存在。

【问题讨论】:

  • 您是从 Internet 下载此资产包吗?
  • 不,我尝试在本地访问它。尝试了 LoadFromCacheOrDownload 和 AssetBundle.LoadFromFile,我还尝试在 Unity 构建中使用绝对路径 - Xcode 找不到或由于某种原因无法打开包。
  • 我的回答解决了您的问题吗?我希望它会。告诉我
  • 刚刚试了一下:是的!谢谢!

标签: c# ios xcode unity3d assetbundle


【解决方案1】:

在下面的示例中,我将演示如何将名为 "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;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 加载的资产/对象的名称是 dog 这是狗的简单 jpg。

C。加载很简单:

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

public RawImage image; 

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

【讨论】:

  • 添加到程序员的优秀答案 - 请注意,编辑器中 AssetBundle“名称”框右侧的框充当扩展。如果您在编辑器中设置了扩展名,则需要加载 Application.streamingAssetsPath + "/AssetBundle/" + assetbunlenameWithoutExtension + myAssetBundleExtension; 以便 Unity 找到它。这有点棘手,因为在编辑器中它看起来仍然有一个没有扩展名的副本,但实际上没有,如果您在 Finder/Explorer 中导航到该位置,您可以清楚地看到。
  • 很好的例子,让我对 AssetBundles 感到困惑的事情(诚然我需要尝试一下)是,如果我有一个分配给 AssetBundle 的预制件,我是否仍然将它拖到场景中并使用它像那样,还是我需要永远只通过代码将它实例化到场景中,以避免在应用程序构建时重复打包。或者,如果一个场景本身被分配给一个资产包,我是否将该场景从传统的构建过程中排除。 (文件 > 构建设置 > 构建中的场景)
  • @Programmer 感谢您的扩展解释。是否可以在 AssetBundle 中添加自定义文件(SQLite 数据库)?
【解决方案2】:

我还有一个建议可能更适合像我这样想要更多基于 .Net 的开发人员。您可以考虑使用 Unity 构建“启动器”。我会使用 Unity 构建它,因此它仍然是跨平台的。接下来,考虑将更新捆绑在 DLL 中,因为 Unity 支持 .Net 框架,或者将它们作为图像存储在 Web 文件夹中。您可以使用 Webclient 获取数据。文章在这里:

https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=netframework-4.8#System_Net_WebClient_DownloadFile_System_Uri_System_String_

启动器可能是获取或更新内容的更好方式。自从我玩《指环王 Online》以来,我一直在玩弄这个想法,我喜欢他们的 Launcher。所以我想为自己的游戏制作一个。在您的代码中,您将使用“DownloadFile(Uri, String)”来获取您的数据。我会使用 API 或其他东西首先检查您当前的游戏或 DLL 版本等。然后检查数据库的最新版本。最后,API 或其他东西可以构建所需的新文件或所需更新文件的列表,然后循环请求文件并下载您需要的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多