c# - 无法加载,因为已经加载了另一个具有相同文件的 AssetBundle

标签 c# unity3d assetbundle

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

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

"Can't be loaded because another AssetBundle with the same files is already loaded"

我已经尝试了 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);
    }

最佳答案

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

关于c# - 无法加载,因为已经加载了另一个具有相同文件的 AssetBundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46741153/

相关文章:

c# - WebApi POST 可以在没有 [FromBody] 的情况下工作吗?

c# - 使用 onServerClick 获取 href 值

c# - 网站中实体模型的命名空间

audio - Unity声音没有播放

unity3d - Unity - Sprite Packer 被禁用

c# - Assetbundle 不适用于 iOS

c# - 在我的LINQ查询中添加OR?

linux - 如何为 Linux (Arch) 手动安装旧版本的 unity (2019)

c# - 团结 : Cannot load scene from asset bundle

javascript - 将脚本添加到 HTML 中的 assetbundle