首页 文章

在Unity中构建和加载Assetbundle

提问于
浏览
3

我不能让Unity Assetbundles在iOS版本中工作 .

在Unity中,我构建了assetsbundles:

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中运行它并收到此错误:

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

它可能与某种方式设置正确的路径有关,但由于我之后将assetbundle文件夹复制到Xcode项目,问题仍然存在 .

1 回答

  • 9

    在下面的这个例子中,我将演示如何将名为 "dog" 的新资产添加到名为 "animals" 的AssetBundle中,然后构建它,然后在运行时加载它 .

    Setting Up Build Folders:

    1 . 选择图像文件等资产 . 在这种情况下,那是 "dog.jpeg" 文件 . 请参阅"Inspector"选项卡中的菜单 . 有时,它被隐藏的AssetBundle选项,将其拖动以显示它 . 请参阅下面的动画gif,了解如何执行此操作 . 默认的AssetBundle是"None" . 单击 "None" 选项,然后转到 "New" 选项并创建新的AssetBundle并将其命名为"animals"

    enter image description here

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

    enter image description here

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

    enter image description here


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

    enter image description here

    B . 转到Assets - > Build AssetBundle菜单来构建您的AssetBudle .

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

    enter image description here


    Loading the AssetBundle during run-time

    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;
    }
    

    装货前的事情:

    A . Assetbundle的名称是 animals .

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

    enter image description here

    C . 加载很简单,因为:

    string nameOfAssetBundle = "animals";
    string nameOfObjectToLoad = "dog";
    
    public RawImage image; 
    
    void Start()
    {
        StartCoroutine(LoadAsset(nameOfAssetBundle, nameOfObjectToLoad));
    }
    

相关问题