How To Load A Local File as an AssetBundle with WWW

若如初见. 提交于 2019-12-11 17:56:53

问题


So I have been trying to make it possible for users to load a .obj file and read it as an AssetBundle, but I can't figure it out.

I have figured out how to get the path of the file, but I can't load it as an asset bundle, it just returns null.

Here is my code :

        WWW bundleRequest = new WWW(@"file://" + pathName);

        while (!bundleRequest.isDone)
        {
            yield return null;
        }

        AssetBundle bundle = null;
        if (bundleRequest.bytesDownloaded > 0)
        {
            AssetBundleCreateRequest myRequest = AssetBundle.LoadFromMemoryAsync(bundleRequest.bytes);
            while (!myRequest.isDone)
            {
                Debug.Log("loading....");
                yield return null;
            }
            if (myRequest.assetBundle != null)
            {
                bundle = myRequest.assetBundle;
                GameObject model = null;
                if (bundle != null)
                {
                    AssetBundleRequest newRequest = bundle.LoadAssetAsync<GameObject>("Test");
                    while (!newRequest.isDone)
                    {
                        Debug.Log("loading ASSET....");
                        yield return null;
                    }
                    model = (GameObject)newRequest.asset;

                    bundle.Unload(false);
                }
            }
            else
            {
                Debug.LogError("COULDN'T DOWNLOAD ASSET BUNDLE FROM URL");
            }
        }
        else
        {
            Debug.LogError("COULDN'T DOWNLOAD ASSET BUNDLE FROM URL");
        }

pathName here is: "C:\\Users\\mySuperCoolName\\OneDrive\\Documents\\Fun\\Programming\\Ungoing projects\\ThiefCop\\Unity Mobile\\Assets\\Prefabs\\TestOBJ.obj". Everything seems to work until AssetBundleCreateRequest when AssetBundle.LoadFromMemoryAsync() is called, where myRequest.assetBundle == null even if the file was downloaded correctly.

I also get an error which probably is linked with my problem :
I have searched for what it meant but I couldn't find...

It is really hard to explain what I mean, but I really hope you can find an answer to this, I've been searching for hours and between us, I don't understand much of File loadind and Reading...

Don't hesitate to ask if you didn't understand my bad english...

Thank you in advance :)


回答1:


https://docs.unity3d.com/ScriptReference/BuildPipeline.BuildAssetBundles.html

To be short :

  • Import your object in Unity
  • Give it a AssetBundle name (click on it then on bottom of the inspector view)
  • Call this function


来源:https://stackoverflow.com/questions/54562488/how-to-load-a-local-file-as-an-assetbundle-with-www

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!