Hello everyone,
I was able to download my content using addressable cloud (3.19 GB). Now, when I make an update in one of my groups, I want to download this update, how can I do this? The codes I wrote are as I mentioned below;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.UI;
public class DownManager : MonoBehaviour
{
[Header("UI")]
public GameObject waitMassage;
public GameObject downMassage;
public Slider downSlider;
public TextMeshProUGUI sizeInfoText;
public TextMeshProUGUI downValText;
public GameObject restartPanel;
[Header("Label")]
public AssetLabelReference preload;
private long patchSize;
private Dictionary<string, long> patchMap = new Dictionary<string, long>();
void Start()
{
restartPanel.SetActive(false);
waitMassage.SetActive(true);
downMassage.SetActive(false);
StartCoroutine(Initaddressable());
StartCoroutine(CheckUpdateFiles());
}
void ClearCache()
{
var handle = Addressables.ClearDependencyCacheAsync(preload,false);
handle.Completed += operation =>
{
if (operation.Status==UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
{
Debug.Log("Cache Cleared successfully!");
}
else
{
Debug.Log("An error occured: "+operation.OperationException.Message);
}
};
}
IEnumerator CheckUpdateFiles()
{
var labels = new List<string>() { preload.labelString };
patchSize = default;
foreach (var label in labels)
{
var handle = Addressables.GetDownloadSizeAsync(label);
yield return handle;
if (handle.Result > 0)
{
Debug.Log(label);
}
patchSize += handle.Result;
}
if (patchSize > decimal.Zero)
{
waitMassage.SetActive(false);
downMassage.SetActive(true);
sizeInfoText.text = GetFileSize(patchSize);
}
else
{
downValText.text = " 100 % ";
downSlider.value = 1f;
yield return new WaitForSeconds(2f);
LoadingManager.LoadScene("MainMenu");
}
}
IEnumerator Initaddressable()
{
var init = Addressables.InitializeAsync();
yield return init;
}
#region Check Down
private string GetFileSize(long byteCnt)
{
string size = "0 Bytes";
if (byteCnt >= 1073741824.0)
{
size = string.Format("{0:##.##}", byteCnt / 1073741824.0) + " GB";
}
else if (byteCnt >= 1048576.0)
{
size = string.Format("{0:##.##}", byteCnt / 1048576.0) + " MB";
}
else if (byteCnt >= 1024.0)
{
size = string.Format("{0:##.##}", byteCnt / 1024.0) + " KB";
}
else if (byteCnt > 0 && byteCnt < 1024.0)
{
size = byteCnt.ToString() + " Bytes";
}
return size;
}
#endregion
#region Download
public void Button_Download()
{
StartCoroutine(PatchFiles());
}
IEnumerator PatchFiles()
{
var labels = new List<string>() { preload.labelString };
foreach (var label in labels)
{
var handle = Addressables.GetDownloadSizeAsync(label);
yield return handle;
if (handle.Result != decimal.Zero)
{
Debug.Log(label);
StartCoroutine(DownloadLabel(label));
}
}
yield return CheckDownload();
}
IEnumerator DownloadLabel(string label)
{
patchMap.Add(label, 0);
var handle = Addressables.DownloadDependenciesAsync(label, false);
while (!handle.IsDone)
{
patchMap[label] = handle.GetDownloadStatus().DownloadedBytes;
yield return new WaitForEndOfFrame();
}
patchMap[label] = handle.GetDownloadStatus().TotalBytes;
Addressables.Release(handle);
}
IEnumerator CheckDownload()
{
var total = 0f;
downValText.text = "0 %";
while (true)
{
total += patchMap.Sum(tmp => tmp.Value);
downSlider.value = total / patchSize;
downValText.text = (int)(downSlider.value * 100) + " %";
if (total == patchSize)
{
restartPanel.SetActive(true);
break;
}
total = 0f;
yield return new WaitForEndOfFrame();
}
}
#endregion
}