【问题标题】:Can't be loaded because another AssetBundle with the same files is already loaded无法加载,因为已经加载了具有相同文件的另一个 AssetBundle
【发布时间】:2019-03-19 19:13:55
【问题描述】:

我想在 AR 相机中播放视频。我总共有 10 个视频和一个视频播放器。我正在从服务器下载视频播放器作为资产包,名称为 videoplayer.unit3d 并存储到 SD 卡中。当我扫描 imageTarget 时,我正在使用 AssetBundle.LoadFromFile() 函数读取视频资产包文件,并且它第一次工作正常。

如果我扫描第二个 imageTarget,它会显示以下错误

"无法加载,因为另一个具有相同文件的 AssetBundle 是 已加载”

我尝试了bundle.Unload(true);Caching.cleanchache(),但它无法正常工作并抛出相同的错误。也试过bundle.Unload(false);

 private void loadObject(string resourcePath, string objectName, TrackableBehaviour trackableBehaviuor, string videoUrl)
    {

        Debug.Log("Resource path " + resourcePath + " objectName " + objectName);

        Debug.Log("Video Url from sd card   " + videoUrl);

        FileInfo fileInfo = new FileInfo(resourcePath);

        if (!fileInfo.Exists)
            return;


        Debug.Log("File is present");
        AssetBundle bundle = AssetBundle.LoadFromFile(resourcePath, 0, 0);//www.assetBundle;


        Debug.Log("Bundle data is  " + bundle);
        if (bundle == null)
        {
            AndroidJavaObject jObject = new AndroidJavaObject("com.ezvidya.buzzle.activity.UnityActivity");
            jObject.Call("showErrorDialog");
            return;
        }
        else
        {
            AndroidJavaObject jo = new AndroidJavaObject("com.ezvidya.buzzle.activity.UnityActivity");
            jo.Call("closeScanDialog");
        }

        //Load an asset from the loaded bundle
        AssetBundleRequest bundleRequest = bundle.LoadAssetAsync(objectName, typeof(GameObject));

        //bundle.Unload(false);
        Caching.CleanCache();

        //get object
        GameObject cubeFromSDCard = bundleRequest.asset as GameObject;


        if (cubeFromSDCard != null)
        {
            // instantiate augmentation object and parent to trackable
            GameObject augmentation = (GameObject)GameObject.Instantiate(cubeFromSDCard);

            augmentation.transform.parent = trackableBehaviuor.gameObject.transform;

            augmentation.transform.localPosition = cubeFromSDCard.transform.localPosition;//new Vector3(0f, 0f, 0f);
            augmentation.transform.localRotation = cubeFromSDCard.transform.localRotation;//Quaternion.identity;
            augmentation.transform.localEulerAngles = cubeFromSDCard.transform.localEulerAngles;
            augmentation.transform.localScale = cubeFromSDCard.transform.localScale;// new Vector3(22f, 22f, 22f);

            Debug.Log("$$$$$$$$$$$$$$$$  Local Position from asset object " + cubeFromSDCard.transform.localPosition);
            Debug.Log("$$$$$$$$$$$$$$$$  Local Rotation from asset object " + cubeFromSDCard.transform.localEulerAngles);
            Debug.Log("$$$$$$$$$$$$$$$$  Local Scale from asset object " + cubeFromSDCard.transform.localScale);


            Debug.Log("$$$$$$$$$$$$$$$$  Position from asset object " + cubeFromSDCard.transform.position);
            Debug.Log("$$$$$$$$$$$$$$$$  Rotation from asset object " + cubeFromSDCard.transform.eulerAngles);
            Debug.Log("$$$$$$$$$$$$$$$$  Scale from asset object " + cubeFromSDCard.transform.lossyScale);


            // Caching.CleanCache();
            if (videoUrl != null && videoUrl.Length > 0)
            {
                VideoPlaybackBehaviour video = augmentation.GetComponent<VideoPlaybackBehaviour>();

                video.m_autoPlay = true;
                Debug.Log("Autoplay is " + video.AutoPlay);
                video.m_path = videoUrl;

                // Pause other videos before playing this one
                // Play this video on texture where it left off

                //OnTrackingFound(false);

                if (video != null && video.AutoPlay)
                {
                    VideoPlayerHelper.MediaState state = video.VideoPlayer.GetStatus();
                    if (state == VideoPlayerHelper.MediaState.PAUSED ||
                        state == VideoPlayerHelper.MediaState.READY ||
                        state == VideoPlayerHelper.MediaState.STOPPED)
                    {
                        // Pause other videos before playing this one
                        PauseOtherVideos(video);

                        // Play this video on texture where it left off
                        video.VideoPlayer.Play(false, 0);
                    }
                    else if (state == VideoPlayerHelper.MediaState.REACHED_END)
                    {
                        // Pause other videos before playing this one
                        PauseOtherVideos(video);

                        // Play this video from the beginning
                        video.VideoPlayer.Play(false, 0);
                    }

                }                

            }

            augmentation.gameObject.SetActive(true);
        }

        //bundle.Unload(false);
    }

【问题讨论】:

  • 如果您能分享它的代码以便我们研究它,那就太好了。从您所写的内容来看,您的问题似乎与stackoverflow.com/questions/12814214/… 相同,但您似乎已经尝试了所有提到的解决方案。因此,如果您能分享更多关于它的信息,那就太好了。
  • 您好我的代码。请告诉这里有什么问题。
  • 哪行代码导致了这个错误?
  • 你试试这个 bundle.Unload(false);和捆绑=空;可能是因为内存中有一些参考。如果有任何事情发生,请告诉我。这是我在为我的项目制作assetbundle时使用的参考网站。如果你不能解决这个问题可以参考这个theappguruz.com/blog/create-and-download-asset-bundle-in-unity
  • 如果我卸载它的工作没有错误。但是每次我扫描 imageTarget 时都会创建视频播放器,所以如果我扫描 5 次,就会有 5 个类似层的视频播放器如何解决这个问题

标签: c# unity3d assetbundle


【解决方案1】:

我找到了答案,在Instantiate之后,Gameobject只需调用bundle.Unload(false);就解决了我的问题。

【讨论】:

    【解决方案2】:

    我已经使用 add 下面两行解决了这个问题,

    private AssetBundle myLoadedAssetBundle;
    string bundleUrl="";
    IEnumerator LoadAssetBundle ()
    {
        if (myLoadedAssetBundle != null) {
            myLoadedAssetBundle.Unload(false); //scene is unload from here
        }
    
        while (!Caching.ready)
            yield return null;
    
        WWW www = WWW.LoadFromCacheOrDownload(bundleUrl, 1);
        yield return www;
    
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield return null;
        }
        myLoadedAssetBundle = www.assetBundle;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 2023-04-02
      • 2023-03-12
      • 2015-01-08
      • 2014-07-18
      • 2010-11-16
      相关资源
      最近更新 更多