Hello.
We got this script that we have been using to download whole scenes as assetbundles. The problem with this script method, is that it gives the user little control over data. To clear up space, we have a “clear cache” button, which removes everything. So to get more data control, i wanted to rewrite using DownloadHandlerFile, and create folders to store bundles in. So each bundle the user downloads, gets its own folder, and downloads the bundle into said folder. I would then be able to get all folders in “SavedAssetData” into a list, and give the user control over what content to keep.
I have now failed for a full day, ending up with almost what i started with. Folders are created, but i cant data downloaded and saved into them. Hopefully someone can give me some tips/help.
Regards
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.IO;
public class AssetBundleCache : MonoBehaviour
{
[Header("Bundle URL")]
public string manifestPath = "https://www";
public string assetBundlePath = "https://www";
[Header("Save Path")]
public string FolderName = "";
string modelFolderPath;
string saveThisData;
[Header("Loading")]
public GameObject LoadingScreen;
public Slider LoadingBar;
public Text textField;
public static AssetBundle bundle;
[Header("Deactivate Buttons")]
public GameObject[] ObjectsToDeactivate;
[Header("Activate Buttons")]
public GameObject[] ObjectsToActivate;
[HideInInspector]
public string[] scenePath;
AssetBundleManifest manifest;
UnityWebRequest request;
List<AsyncOperation> DownloadOperation = new List<AsyncOperation>();
// Called from button
public void StartDownload()
{
string SavedAssetDataFolder = "SavedAssetData";
modelFolderPath = Path.Combine(Application.dataPath, SavedAssetDataFolder).Replace('/', '\\');
saveThisData = Path.Combine(modelFolderPath, FolderName).Replace('/', '\\');
if (!Directory.Exists(Path.GetDirectoryName(FolderName)))
{
Directory.CreateDirectory(saveThisData);
if (StaticSettingsInfo.EnterDebugMode) { Debug.Log(FolderName + " folder Created in : " + saveThisData); }
}
CreateFolder.RefreshAssetDataFolder();
StartCoroutine(DownloadAndCacheAssetBundle());
}
float totalSceneDownloadProgress;
public IEnumerator DownloadAndCacheAssetBundle()
{
/*Downloadhandler*/
/* var DH = new DownloadHandlerFile(saveThisData, false);
DH.removeFileOnAbort = true;
request.downloadHandler = DH;
Debug.Log("DownloadHandler error " + DH.error); */
Debug.Log("AssetBundle Cache and Download started");
// Load the manifest (from url)
UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(manifestPath);
yield return www.SendWebRequest();
AssetBundle manifestBundle = DownloadHandlerAssetBundle.GetContent(www);
AssetBundleManifest manifest = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
// Use the default cache here
Caching.currentCacheForWriting = Caching.defaultCache;
string[] allAssetBundleNames = manifest.GetAllAssetBundles();
foreach (string assetBundleName in allAssetBundleNames)
{
// Get hash of bundle
Hash128 hash = manifest.GetAssetBundleHash(assetBundleName);
Debug.Log("Hash for bundle " + assetBundleName + " is " + hash);
// Download actual content bundle
/*UnityWebRequest*/
request = UnityWebRequestAssetBundle.GetAssetBundle(assetBundlePath, hash, 0);
DownloadOperation.Add(request.SendWebRequest());
for (int i = 0; i < DownloadOperation.Count; i++)
{
while (!DownloadOperation[i].isDone)
{
totalSceneDownloadProgress = 0;
foreach (AsyncOperation operation in DownloadOperation)
{
totalSceneDownloadProgress += operation.progress;
}
totalSceneDownloadProgress = (totalSceneDownloadProgress / DownloadOperation.Count) * 100f;
textField.text = totalSceneDownloadProgress.ToString("F0") + "%";
LoadingBar.value = Mathf.RoundToInt(totalSceneDownloadProgress);
yield return null;
}
}
// 200 means downloaded from web and Code 0 means loaded from cache.
Debug.Log(request.responseCode);
if (request.responseCode == 0)
{
Debug.Log("Asset loaded from cache");
}
if (request.responseCode == 200)
{
Debug.Log("Asset downloaded from cloud");
LoadingScreen.gameObject.SetActive(true);
}
// Download the actual content bundle
/*AssetBundle*/
bundle = DownloadHandlerAssetBundle.GetContent(request);
// List all the cached versions of the given asset bundle
List<Hash128> listOfCachedVersions = new List<Hash128>();
Caching.GetCachedVersions(assetBundleName, listOfCachedVersions);
Debug.Log("AssetbundleName = " + assetBundleName);
for (int i = 0; i < listOfCachedVersions.Count; i++)
{
Debug.Log("Found cached version of " + assetBundleName + " version: " + listOfCachedVersions[i]);
}
for (int i = 0; i < ObjectsToDeactivate.Length; i++)
{
ObjectsToDeactivate[i].SetActive(false);
}
for (int i = 0; i < ObjectsToActivate.Length; i++)
{
ObjectsToActivate[i].SetActive(true);
}
LoadingScreen.gameObject.SetActive(false);
scenePath = bundle.GetAllScenePaths();
}
}
public void AbortDownload()
{
if (request != null && !request.isDone)
{
request.Abort();
Debug.Log("Download was aborted.", this);
}
else
{
Debug.Log("Not downloading, nothing to abort...", this);
}
}
}