Some more details about the project:
Unity 2018.3.8
Enable Cached set to True
Testing on Chrome (73.0.3683.86)
Using Asset bundle
Here is the code snippet for loading asset bundle and destroying the object when don’t require:
/// <summary>
/// load assetbundle manifest, check hash, load actual bundle with hash parameter to use caching
/// instantiate gameobject
/// </summary>
/// <param name="bundleURL">full url to assetbundle file</param>
/// <param name="assetName">optional parameter to access specific asset from assetbundle</param>
/// <returns></returns>
IEnumerator DownloadAndCache(string bundleURL, string assetName = "")
{
//loadingImg.gameObject.SetActive(true);
// get current bundle hash from server, random value added to avoid caching
UnityWebRequest www;
// now download the actual bundle, with hashString parameter it uses cached version if available
//www = UnityWebRequest.GetAssetBundle(bundleURL + "?r=" + (Random.value * 9999999), hashString, 0);
www = UnityWebRequestAssetBundle.GetAssetBundle(bundleURL);
// wait for load to finish
//yield return www.Send();
yield return www.SendWebRequest();
if (www.error != null)
{
Debug.LogError("www error: " + www.error);
www.Dispose();
www = null;
yield break;
}
// get bundle from downloadhandler
AssetBundle bundle = ((DownloadHandlerAssetBundle)www.downloadHandler).assetBundle;
GameObject bundlePrefab = null;
// if no asset name is given, take the first/main asset
if (assetName == "")
{
bundlePrefab = (GameObject)bundle.LoadAsset(bundle.GetAllAssetNames()[0]);
}
else
{ // use asset name to access inside bundle
bundlePrefab = (GameObject)bundle.LoadAsset(assetName);
}
// if we got something out
if (bundlePrefab != null)
{
// instantiate at 0,0,0 and without rotation
//nstantiate(bundlePrefab, Vector3.zero, Quaternion.identity);
GameObject abObject = (GameObject)Instantiate(bundlePrefab, gameObject.transform.position, gameObject.transform.rotation);
abObject.transform.parent = this.transform;
if (AssetBundleLoadedAndInstantiated != null)
{
AssetBundleLoadedAndInstantiated(this.name);
}
//SetTreeShaderSettings(abObject);
/*
// fix pink shaders, NOTE: not always needed..
foreach (Renderer r in go.GetComponentsInChildren<Renderer>(includeInactive: true))
{
// FIXME: creates multiple materials, not good
var material = Shader.Find(r.material.shader.name);
r.material.shader = null;
r.material.shader = material;
}*/
}
www.Dispose();
www = null;
// try to cleanup memory
Resources.UnloadUnusedAssets();
bundle.Unload(false);
bundle = null;
//loadingImg.gameObject.SetActive(false);
}
public void DestroyBundleObject()
{
if (this.gameObject.transform.childCount > 0)
{
Destroy(this.gameObject.transform.GetChild(0).gameObject);
Resources.UnloadUnusedAssets();
}
}
I am using Kongregate WebGL utilities (WebGLCachedXMLHttpRequest) which is located in plugin folder(i guess it work automatically) as found that unity own classes are taking much more memory.
var gameInstance = UnityLoader.instantiate("gameContainer", "%UNITY_WEBGL_BUILD_URL%", {onProgress: UnityProgress, Module: {
//TOTAL_MEMORY: 268435456,
// true in order to enable caching of the initially loaded .js, .data and .mem files
CachedXMLHttpRequestLoader : true,
//Set Module.CachedXMLHttpRequestSilent to true in order to disable cache logs in the console.
CachedXMLHttpRequestSilent : true ,
// Set Module.CachedXMLHttpRequestDisable to true in order to fully disable indexedDB caching.
//CachedXMLHttpRequestDisable : true,
}});
Remember other than this i have no important thing to share with you. On more thing is that i am not clear caching, and whenever user leave the tab i call this function.
Here is the javascript
function ViewerAppQuit(){
if(gameInstance!=null)
{
gameInstance.SendMessage("GameFocusControl", "ViewerQuit");
document.getElementById("gameContainer").outerHTML = "";
}
}
while the C# code is:
public void ViewerQuit()
{
Debug.Log("Application Quit");
Application.Quit();
}
If you require more information i am available. Thanks