Files
2024-08-23 15:49:34 +08:00

51 lines
1.6 KiB
C#

using UnityEngine;
namespace AssetManagement
{
public class BundleLoader : AsyncLoadUnit
{
public AssetBundle bundle { get; private set; }
private AssetBundleCreateRequest _bundleRequest;
public override float GetProgress()
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (state)
{
case AsyncLoadUnitState.Done:
return 1f;
case AsyncLoadUnitState.Loading:
return _bundleRequest == null ? 0f : _bundleRequest.progress;
default:
return 0f;
}
}
protected override AsyncOperation StartOperation(out string startError)
{
var path = pathHub.GetPath(name);
if (string.IsNullOrEmpty(path))
{
startError = "Path Error: cannot get load path for asset " + name;
return null;
}
else
{
startError = string.Empty;
_bundleRequest = AssetBundle.LoadFromFileAsync(path + AssetConst.bundleVariant);
return _bundleRequest;
}
}
protected override string HandleOperation(AsyncOperation operation)
{
if (operation == _bundleRequest)
{
bundle = _bundleRequest.assetBundle;
return bundle == null ? "Load Error: cannot load asset bundle from path " + name : string.Empty;
}
else
return "Process Error: completed operation is not the original one!";
}
}
}