简体   繁体   English

Assetbundles无法在iOS上使用

[英]Assetbundles not working with iOS

I am developing an AR app for the iPhone in Unity which downloads asset bundles from a webserver and attaches them to image targets. 我正在Unity中为iPhone开发一个AR应用程序,该应用程序可从网络服务器下载资产捆绑包并将其附加到图像目标。 So far almost everything is working--except that when the app tries to load the asset bundle, I get the following error: 到目前为止,几乎所有功能都可以正常工作-除非应用程序尝试加载资产捆绑包时,出现以下错误:

'asset/bundle/url' can't be loaded because it was not built with the right version or build target. 无法加载“资产/捆绑/网址”,因为它不是使用正确的版本或构建目标构建的。

I created the assetbundles using the following script, modified slightly from the Unity documentation: 我使用以下脚本创建了资产捆绑包,并从Unity文档中对其进行了稍微修改:

using UnityEditor;

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

I then uploaded the assetbundles to the server using scp. 然后,我使用scp将资产捆绑包上传到服务器。 The app downloads the assetbundles fine but cannot load them. 该应用程序可以很好地下载资产捆绑包,但无法加载它们。 Here's the code: 这是代码:

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

So far I have tried: 到目前为止,我已经尝试过:

  • Setting the build target BuildTarget.iPhone - no difference 设置构建目标BuildTarget.iPhone-没有区别
  • Building the assetbundles using the assetbundle manager API from Unity 使用Unity中的assetbundle Manager API构建资产束
  • Unchecking "strip engine code" in the build options for iOS - I heard on another forum that this can cause problems. 在iOS的构建选项中取消选中“条带引擎代码”-在另一个论坛上听说,这可能会导致问题。

Any help appreciated. 任何帮助表示赞赏。

-UPDATE- -更新-
I modified the BuildAssetBundle script per Programmer's suggestion and switched the target platform from "iPhone" to "Universal" in the player settings panel. 我按照程序员的建议修改了BuildAssetBundle脚本,并在播放器设置面板中将目标平台从“ iPhone”切换为“ Universal”。 I'm pretty sure this is what resolved the problem. 我很确定这是解决问题的方法。

This means that your target device was not iOS when you built the Asset bundle. 这意味着在构建资产捆绑包时,您的目标设备不是iOS。 Change your target to iOS then rebuild the Asset Bundle. 将目标更改为iOS,然后重建资产捆绑包。

Recompile and re-upload to iOS and that should solve your problem. 重新编译并重新上传到iOS,这应该可以解决您的问题。

If that didnt work then... If you built your AssetBundle while the scripting backend was set to IL2CPP, but then you use them with the .NET scripting backend, that error would occur too. 如果那没有用,那么...如果在脚本后端设置为IL2CPP的同时构建了AssetBundle,但是随后将它们与.NET脚本后端一起使用,也会发生该错误。 Solution is to rebuild your Asset Bundle after changing to .NET scripting backend through the player settings. 解决方案是在通过播放器设置更改为.NET脚本后端后,重建资产捆绑包。

Also try replacing BuildTarget.iPhone with EditorUserBuildSettings.activeBuildTarget so that that Bundle would automatically build for any build target currently selected. 也可以尝试更换BuildTarget.iPhoneEditorUserBuildSettings.activeBuildTarget ,使得包会自动建立当前选择的构建目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM