首页 文章

Assetbundles不适用于iOS

提问于
浏览
1

我正在为Unity开发一个适用于iPhone的AR应用程序,它从网络服务器下载资产包并将它们附加到图像目标 . 到目前为止几乎所有东西都在工作 - 除了当应用程序尝试加载资产包时,我收到以下错误:

'asset / bundle / url'无法加载,因为它不是使用正确的版本或构建目标构建的 .

我使用以下脚本创建了assetsbundles,稍微修改了Unity文档:

using UnityEditor;

public class CreateAssetBundles
{
    [MenuItem ("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles ()
    {
        BuildPipeline.BuildAssetBundles ("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.iOS);
    }
}

然后我使用scp将assetbundles上传到服务器 . 该应用程序可以下载assetbundles,但无法加载它们 . 这是代码:

IEnumerator DownloadAndCache (string username, string modelName){
        // Wait for the Caching system to be ready
        while (!Caching.ready) {
            Debug.Log ("Waiting on caching");
            yield return null;
        }

        //Compute request url for server
        UriBuilder uriBuilder = new UriBuilder();
        uriBuilder.Scheme = "http";
        uriBuilder.Host = BaseURL;
        uriBuilder.Path = username.Trim() + "/" + modelName.Trim();
        string BundleURL = uriBuilder.ToString ();
        Debug.Log ("Attempting to download " + BundleURL);

        // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
        using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
            yield return www;
            if (www.error != null) {
                Debug.Log ("Download error");
                throw new Exception ("WWW download had an error:" + www.error);
            }
            AssetBundle bundle = www.assetBundle;
            Debug.Log ("Got assets "+string.Join(",", bundle.GetAllAssetNames ()));
            GameObject loadedModel = Instantiate(bundle.LoadAllAssets()[0]) as GameObject;

            //attach to the image target
            loadedModel.transform.parent = ImageTargetTemplate.gameObject.transform;
            loadedModel.transform.position = new Vector3 (0, 0, 0);
            loadedModel.transform.localScale = new Vector3 (1, 1, 1);

            // Unload the AssetBundles compressed contents to conserve memory
            bundle.Unload(false);

        } // memory is freed from the web stream (www.Dispose() gets called implicitly)
    }

到目前为止,我尝试过:

  • 设置构建目标BuildTarget.iPhone - 没有区别

  • 使用Unity的assetbundle manager API构建assetsbundles

  • 在iOS的构建选项中取消选中"strip engine code" - 我在另一个论坛上听说这可能会导致问题 .

任何帮助赞赏 .

-UPDATE-
我根据Programmer 's suggestion and switched the target platform from 901045 to 901046 in the player settings panel. I'修改了BuildAssetBundle脚本,非常确定这是解决问题的原因 .

1 回答

  • 4

    这意味着在构建资产包时,目标设备不是iOS . 将目标更改为iOS,然后重建资产包 .

    重新编译并重新上传到iOS,这应该可以解决您的问题 .

    如果那不起作用那么......如果在脚本后端设置为IL2CPP时构建了AssetBundle,但是随后将它们与.NET脚本后端一起使用,那么也会发生该错误 . 解决方案是在通过播放器设置更改为.NET脚本后端后重建您的资产包 .

    同时尝试用 EditorUserBuildSettings.activeBuildTarget 替换 BuildTarget.iPhone ,以便该Bundle将自动为当前选择的任何构建目标构建 .

相关问题