165 lines
4.6 KiB
C#
165 lines
4.6 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Drawing;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.AddressableAssets;
|
||
|
|
using UnityEngine.AddressableAssets.ResourceLocators;
|
||
|
|
using UnityEngine.Events;
|
||
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
||
|
|
|
||
|
|
public class AddressableManager : MonoBehaviour
|
||
|
|
{
|
||
|
|
[SerializeField]
|
||
|
|
string preloadLabelName = "preload";
|
||
|
|
|
||
|
|
public UnityAction OnInitialized;
|
||
|
|
public UnityAction<bool> NotifiyCatalogUpdated;
|
||
|
|
public UnityAction<long> NotifiySizeDownloaded;
|
||
|
|
public UnityAction<bool> NotifiyDownloadFinished;
|
||
|
|
public UnityAction<bool> OnSucceedLoadOutGameObject;
|
||
|
|
public UnityAction<bool> OnSucceedLoadOutGameImage;
|
||
|
|
public UnityAction<bool> OnSucceedLoadInGameObject;
|
||
|
|
public UnityAction<bool> OnSucceedLoadInGameImage;
|
||
|
|
|
||
|
|
private AsyncOperationHandle DownloadHandle;
|
||
|
|
private bool isInitialized = false;
|
||
|
|
private long totalDownloadSize;
|
||
|
|
|
||
|
|
private float dataLoadPercent;
|
||
|
|
public float DataLoadPercent
|
||
|
|
{
|
||
|
|
get { return dataLoadPercent; }
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Start()
|
||
|
|
{
|
||
|
|
Addressables.InitializeAsync().Completed += Initialized;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void DownloadData()
|
||
|
|
{
|
||
|
|
Debug.Log("base: AddressableManager: DownloadData");
|
||
|
|
|
||
|
|
if (isInitialized == false)
|
||
|
|
{
|
||
|
|
Addressables.InitializeAsync().Completed += (result) =>
|
||
|
|
{
|
||
|
|
isInitialized = true;
|
||
|
|
|
||
|
|
UpdateCatalog();
|
||
|
|
OnInitialized?.Invoke();
|
||
|
|
};
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
UpdateCatalog();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public float GetDownloadDataPercent()
|
||
|
|
{
|
||
|
|
return DownloadHandle.IsValid() ? DownloadHandle.PercentComplete : 0.0f;
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool IsDownloadValid()
|
||
|
|
{
|
||
|
|
return DownloadHandle.IsValid();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Initialized(AsyncOperationHandle<IResourceLocator> result)
|
||
|
|
{
|
||
|
|
Debug.Log("base: AddressableManager: OnInitialized");
|
||
|
|
|
||
|
|
isInitialized = true;
|
||
|
|
OnInitialized?.Invoke();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void UpdateCatalog()
|
||
|
|
{
|
||
|
|
Debug.Log("base: AddressableManager: UpdateCatalog");
|
||
|
|
|
||
|
|
Addressables.CheckForCatalogUpdates().Completed += (result) =>
|
||
|
|
{
|
||
|
|
Debug.Log("base: AddressableManager: CheckForCatalogUpdates");
|
||
|
|
|
||
|
|
var catalogToUpdate = result.Result;
|
||
|
|
if (catalogToUpdate.Count > 0)
|
||
|
|
{
|
||
|
|
Addressables.UpdateCatalogs(catalogToUpdate).Completed += OnCatalogUpdated;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
StartDownloadData();
|
||
|
|
NotifiyCatalogUpdated?.Invoke(true);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnCatalogUpdated(AsyncOperationHandle<List<IResourceLocator>> result)
|
||
|
|
{
|
||
|
|
Debug.Log("base: AddressableManager: OnCatalogUpdated");
|
||
|
|
|
||
|
|
if (result.Status == AsyncOperationStatus.Succeeded)
|
||
|
|
{
|
||
|
|
DownloadSize();
|
||
|
|
|
||
|
|
NotifiyCatalogUpdated?.Invoke(true);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
NotifiyCatalogUpdated?.Invoke(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void DownloadSize()
|
||
|
|
{
|
||
|
|
Debug.Log("base: AddressableManager: DownloadSize");
|
||
|
|
|
||
|
|
Addressables.GetDownloadSizeAsync(preloadLabelName).Completed += OnSizeDownloaded;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnSizeDownloaded(AsyncOperationHandle<long> result)
|
||
|
|
{
|
||
|
|
Debug.Log("base: AddressableManager: OnSizeDownloaded");
|
||
|
|
|
||
|
|
totalDownloadSize = result.Result;
|
||
|
|
StartDownloadData();
|
||
|
|
|
||
|
|
NotifiySizeDownloaded?.Invoke(totalDownloadSize);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void StartDownloadData()
|
||
|
|
{
|
||
|
|
Debug.Log("base: AddressableManager: StartDownloadData");
|
||
|
|
|
||
|
|
DownloadHandle = Addressables.DownloadDependenciesAsync(preloadLabelName);
|
||
|
|
DownloadHandle.Completed += OnDependenciesDownloaded;
|
||
|
|
|
||
|
|
Addressables.Release(DownloadHandle);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnDependenciesDownloaded(AsyncOperationHandle result)
|
||
|
|
{
|
||
|
|
Debug.Log("base: AddressableManager: OnDependenciesDownloaded");
|
||
|
|
|
||
|
|
switch (result.Status)
|
||
|
|
{
|
||
|
|
case AsyncOperationStatus.Succeeded:
|
||
|
|
Debug.Log("base: AddressableManager: OnDependenciesDownloaded: Succeeded");
|
||
|
|
NotifiyDownloadFinished?.Invoke(true);
|
||
|
|
break;
|
||
|
|
|
||
|
|
case AsyncOperationStatus.Failed:
|
||
|
|
Debug.Log("base: AddressableManager: OnDependenciesDownloaded: Failed");
|
||
|
|
NotifiyDownloadFinished?.Invoke(false);
|
||
|
|
break;
|
||
|
|
|
||
|
|
case AsyncOperationStatus.None:
|
||
|
|
Debug.Log("base: AddressableManager: OnDependenciesDownloaded: None");
|
||
|
|
NotifiyDownloadFinished?.Invoke(false);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|