Help us understand the problem. What is going on with this article?

AssetBundleManagerのwwwをUnityWebRequestに変えて、さらにキャッシュの古いバージョンを削除してみる

More than 3 years have passed since last update.

環境

Unity2017.1.0f3

概要

wwwは使用しないでUnityWebRequestを使用したほうがいいらしいのでテスト
また、Caching.ClearOtherCachedVersionsを使用して、キャッシュの古いバージョンを削除します

変更箇所

        // Where we actuall call WWW to download the assetBundle.
        static protected bool LoadAssetBundleInternal (string assetBundleName, bool isLoadingAssetBundleManifest)
        {
            // Already loaded.
            LoadedAssetBundle bundle = null;
            m_LoadedAssetBundles.TryGetValue(assetBundleName, out bundle);
            if (bundle != null)
            {
                bundle.m_ReferencedCount++;
                return true;
            }

            // @TODO: Do we need to consider the referenced count of WWWs?
            // In the demo, we never have duplicate WWWs as we wait LoadAssetAsync()/LoadLevelAsync() to be finished before calling another LoadAssetAsync()/LoadLevelAsync().
            // But in the real case, users can call LoadAssetAsync()/LoadLevelAsync() several times then wait them to be finished which might have duplicate WWWs.
            if (m_DownloadingWWWs.ContainsKey(assetBundleName) )
                return true;

        //  WWW download = null;
            UnityWebRequest download = null;
            string url = m_BaseDownloadingURL + assetBundleName;

            // For manifest assetbundle, always download it as we don't have hash for it.
            if (isLoadingAssetBundleManifest)
            //  download = new WWW(url);
                download = UnityWebRequest.GetAssetBundle( url );
            else
            {
                download = UnityWebRequest.GetAssetBundle( url, m_AssetBundleManifest.GetAssetBundleHash(assetBundleName), 0 );
                //Unity2017 バージョン違いを消す
                if( Caching.ClearOtherCachedVersions( assetBundleName, m_AssetBundleManifest.GetAssetBundleHash(assetBundleName ) ) == false )
                    Debug.LogWarning( "ClearOtherCachedVersions" );
            }
            //  download = WWW.LoadFromCacheOrDownload(url, m_AssetBundleManifest.GetAssetBundleHash(assetBundleName), 0); 
            download.Send();
            m_DownloadingWWWs.Add(assetBundleName, download);

            return false;
        }
        void Update()
        {
            // Collect all the finished WWWs.
            var keysToRemove = new List<string>();
            foreach (var keyValue in m_DownloadingWWWs)
            {
            //  WWW download = keyValue.Value;
                var download = keyValue.Value;

                // If downloading fails.
                if (download.error != null)
                {
                    m_DownloadingErrors.Add(keyValue.Key, string.Format("Failed downloading bundle {0} from {1}: {2}", keyValue.Key, download.url, download.error));
                    keysToRemove.Add(keyValue.Key);
                    continue;
                }

                // If downloading succeeds.
                if(download.isDone)
                {
                //  AssetBundle bundle = download.assetBundle;
                    AssetBundle bundle = ((DownloadHandlerAssetBundle)download.downloadHandler).assetBundle;
                    if (bundle == null)
                    {
                        m_DownloadingErrors.Add(keyValue.Key, string.Format("{0} is not a valid asset bundle.", keyValue.Key));
                        keysToRemove.Add(keyValue.Key);
                        continue;
                    }

                    //Debug.Log("Downloading " + keyValue.Key + " is done at frame " + Time.frameCount);
                //  m_LoadedAssetBundles.Add(keyValue.Key, new LoadedAssetBundle( download.assetBundle ) );
                    m_LoadedAssetBundles.Add(keyValue.Key, new LoadedAssetBundle( bundle ) );
                    keysToRemove.Add(keyValue.Key);

                    //Unity2017キャッシュの確認
                    var currentCache = Caching.currentCacheForWriting;
                    var versions = new List<Hash128>();
                    var assetbundleName = Path.GetFileName( download.url );
                    Caching.GetCachedVersions( assetbundleName, versions );
                    Debug.Log( "CacheVersionCount:" + bundle.name + ":" + versions.Count );
                    Debug.Log( "CachePath:" + currentCache.path );
                    Debug.Log( "CacheSize:" + currentCache.spaceOccupied + "/" + currentCache.maximumAvailableStorageSpace );
                }
            }

            // Remove the finished WWWs.
            foreach( var key in keysToRemove)
            {
            //  WWW download = m_DownloadingWWWs[key];
                var download = m_DownloadingWWWs[key];
                m_DownloadingWWWs.Remove(key);
                download.Dispose();
            }

            // Update all in progress operations
            for (int i=0;i<m_InProgressOperations.Count;)
            {
                if (!m_InProgressOperations[i].Update())
                {
                    m_InProgressOperations.RemoveAt(i);
                }
                else
                    i++;
            }
        }
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした