LoginSignup
12
11

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-07-11

環境

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++;
            }
        }
12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11