Skip to content

Commit

Permalink
Disable caching on Unity 2022.1+ for WebGL platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-redcellgames committed May 19, 2024
1 parent c524d01 commit c6b1457
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
12 changes: 11 additions & 1 deletion Scripts/AssetBundleDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System;
#if UNITY_2022_1_OR_NEWER && UNITY_WEBGL
// Caching is not supported on WebGL platforms on Unity 2022.1+
// https://docs.unity3d.com/2022.1/Documentation/ScriptReference/Caching.html
#define ABM_DISABLE_CACHING
#endif

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -53,6 +59,10 @@ public AssetBundleDownloader(string baseUri)
if (!this.baseUri.EndsWith("/")) {
this.baseUri += "/";
}

#if ABM_DISABLE_CACHING
cachingDisabled = true;
#endif
}

/// <summary>
Expand Down
16 changes: 15 additions & 1 deletion Scripts/AssetBundleManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System;
#if UNITY_2022_1_OR_NEWER && UNITY_WEBGL
// Caching is not supported on WebGL platforms on Unity 2022.1+
// https://docs.unity3d.com/2022.1/Documentation/ScriptReference/Caching.html
#define ABM_DISABLE_CACHING
#endif

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
Expand Down Expand Up @@ -242,10 +248,12 @@ private void GetManifest(string bundleName, bool getFreshManifest, Action<AssetB
// Find the first cached version and then get the "next" one.
manifestVersion = (uint)PlayerPrefs.GetInt(MANIFEST_PLAYERPREFS_KEY, 0) + 1;

#if !ABM_DISABLE_CACHING
// The PlayerPrefs value may have been wiped so we have to calculate what the next uncached manifest version is.
while (Caching.IsVersionCached(bundleName, new Hash128(0, 0, 0, manifestVersion))) {
manifestVersion++;
}
#endif
}

GetManifestInternal(bundleName, manifestVersion, 0);
Expand Down Expand Up @@ -295,7 +303,9 @@ private void OnInitializationComplete(AssetBundle manifestBundle, string bundleN
} else {
Manifest = manifestBundle.LoadAsset<AssetBundleManifest>("assetbundlemanifest");
PlayerPrefs.SetInt(MANIFEST_PLAYERPREFS_KEY, (int)version);
#if !ABM_DISABLE_CACHING
Caching.ClearOtherCachedVersions(bundleName, new Hash128(0, 0, 0, version));
#endif
}

if (Manifest == null) {
Expand Down Expand Up @@ -565,7 +575,11 @@ public bool IsVersionCached(string bundleName)
if (Manifest == null) return false;
if (useHash) bundleName = GetHashedBundleName(bundleName);
if (string.IsNullOrEmpty(bundleName)) return false;
#if ABM_DISABLE_CACHING
return false;
#else
return Caching.IsVersionCached(bundleName, Manifest.GetAssetBundleHash(bundleName));
#endif
}

/// <summary>
Expand Down

0 comments on commit c6b1457

Please sign in to comment.