12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using UnityEngine;
- using UnityEngine.Networking;
- using XGame.Framework.Asyncs;
- namespace XGame.Framework.Asset
- {
- internal class WebRequestAssetBundleAsync : Async, IAssetAsync
- {
- private UnityWebRequest request;
- private AsyncOperation operation;
- public string Path { get; internal set; }
- public uint Offset { get; internal set; }
- /// <summary>
- /// 此处先存bundleName
- /// </summary>
- public string AddressableName { get; internal set; }
- public override float Progress
- {
- get => operation != null ? operation.progress : 0;
- protected set => base.Progress = value;
- }
- public void Start()
- {
- //Log.Warn($"AssetBundleCreateRequestAsync Start. Path:{Path}");
- request = UnityWebRequestAssetBundle.GetAssetBundle(Path);
- operation = request.SendWebRequest();
- operation.completed += OnCompleted;
- }
- void OnCompleted(AsyncOperation async)
- {
- if (!async.isDone)
- {
- AssetsLog.Warn($"Assetbundle operation error. name:{AddressableName} Path:{Path}");
- }
- else if (request == null || !request.isDone)
- {
- AssetsLog.Warn($"Assetbundle request error. name:{AddressableName} Path:{Path}");
- }
- Completed();
- }
- public object GetResult()
- {
- object result = null;
- if (request != null)
- {
- //Log.Warn($"AssetBundleCreateRequestAsync Get bundle:{request.assetBundle? .GetInstanceID()??0} Path:{Path}");
- result = DownloadHandlerAssetBundle.GetContent(request);
- }
- if (result == null)
- {
- AssetsLog.Warn($"Assetbundle is null. name:{AddressableName} Path:{Path} request:{request != null}");
- }
- Clear();
- return result;
- }
- void Clear()
- {
- request = null;
- operation = null;
- }
- }
- }
|