mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
更新demo
更新demo
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 937392854b37d5043808598f2d0e07ec
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public sealed class AllAssetsHandle : HandleBase, IDisposable
|
||||
{
|
||||
private System.Action<AllAssetsHandle> _callback;
|
||||
|
||||
internal AllAssetsHandle(ProviderBase provider) : base(provider)
|
||||
{
|
||||
}
|
||||
internal override void InvokeCallback()
|
||||
{
|
||||
_callback?.Invoke(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 完成委托
|
||||
/// </summary>
|
||||
public event System.Action<AllAssetsHandle> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(AllAssetsHandle)} is invalid");
|
||||
if (Provider.IsDone)
|
||||
value.Invoke(this);
|
||||
else
|
||||
_callback += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(AllAssetsHandle)} is invalid");
|
||||
_callback -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return;
|
||||
Provider.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
this.ReleaseInternal();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
this.ReleaseInternal();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 子资源对象集合
|
||||
/// </summary>
|
||||
public UnityEngine.Object[] AllAssetObjects
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return null;
|
||||
return Provider.AllAssetObjects;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6de8dc2be5f52704abe6db03818edff2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,157 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public sealed class AssetHandle : HandleBase, IDisposable
|
||||
{
|
||||
private System.Action<AssetHandle> _callback;
|
||||
|
||||
internal AssetHandle(ProviderBase provider) : base(provider)
|
||||
{
|
||||
}
|
||||
internal override void InvokeCallback()
|
||||
{
|
||||
_callback?.Invoke(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 完成委托
|
||||
/// </summary>
|
||||
public event System.Action<AssetHandle> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(AssetHandle)} is invalid");
|
||||
if (Provider.IsDone)
|
||||
value.Invoke(this);
|
||||
else
|
||||
_callback += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(AssetHandle)} is invalid");
|
||||
_callback -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return;
|
||||
Provider.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
this.ReleaseInternal();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
this.ReleaseInternal();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象
|
||||
/// </summary>
|
||||
public UnityEngine.Object AssetObject
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return null;
|
||||
return Provider.AssetObject;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源对象
|
||||
/// </summary>
|
||||
/// <typeparam name="TAsset">资源类型</typeparam>
|
||||
public TAsset GetAssetObject<TAsset>() where TAsset : UnityEngine.Object
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return null;
|
||||
return Provider.AssetObject as TAsset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步初始化游戏对象
|
||||
/// </summary>
|
||||
public GameObject InstantiateSync()
|
||||
{
|
||||
return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, null, false);
|
||||
}
|
||||
public GameObject InstantiateSync(Transform parent)
|
||||
{
|
||||
return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, parent, false);
|
||||
}
|
||||
public GameObject InstantiateSync(Transform parent, bool worldPositionStays)
|
||||
{
|
||||
return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, parent, worldPositionStays);
|
||||
}
|
||||
public GameObject InstantiateSync(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
return InstantiateSyncInternal(true, position, rotation, null, false);
|
||||
}
|
||||
public GameObject InstantiateSync(Vector3 position, Quaternion rotation, Transform parent)
|
||||
{
|
||||
return InstantiateSyncInternal(true, position, rotation, parent, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步初始化游戏对象
|
||||
/// </summary>
|
||||
public InstantiateOperation InstantiateAsync()
|
||||
{
|
||||
return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, null, false);
|
||||
}
|
||||
public InstantiateOperation InstantiateAsync(Transform parent)
|
||||
{
|
||||
return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, parent, false);
|
||||
}
|
||||
public InstantiateOperation InstantiateAsync(Transform parent, bool worldPositionStays)
|
||||
{
|
||||
return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, parent, worldPositionStays);
|
||||
}
|
||||
public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
return InstantiateAsyncInternal(true, position, rotation, null, false);
|
||||
}
|
||||
public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent)
|
||||
{
|
||||
return InstantiateAsyncInternal(true, position, rotation, parent, false);
|
||||
}
|
||||
|
||||
private GameObject InstantiateSyncInternal(bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return null;
|
||||
if (Provider.AssetObject == null)
|
||||
return null;
|
||||
|
||||
return InstantiateOperation.InstantiateInternal(Provider.AssetObject, setPositionAndRotation, position, rotation, parent, worldPositionStays);
|
||||
}
|
||||
private InstantiateOperation InstantiateAsyncInternal(bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
|
||||
{
|
||||
string packageName = GetAssetInfo().PackageName;
|
||||
InstantiateOperation operation = new InstantiateOperation(this, setPositionAndRotation, position, rotation, parent, worldPositionStays);
|
||||
OperationSystem.StartOperation(packageName, operation);
|
||||
return operation;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b766125ab790154ca132adb5c77333d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public abstract class HandleBase : IEnumerator
|
||||
{
|
||||
private readonly AssetInfo _assetInfo;
|
||||
internal ProviderBase Provider { private set; get; }
|
||||
|
||||
internal HandleBase(ProviderBase provider)
|
||||
{
|
||||
Provider = provider;
|
||||
_assetInfo = provider.MainAssetInfo;
|
||||
}
|
||||
internal abstract void InvokeCallback();
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源信息
|
||||
/// </summary>
|
||||
public AssetInfo GetAssetInfo()
|
||||
{
|
||||
return _assetInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载报告
|
||||
/// </summary>
|
||||
public DownloadStatus GetDownloadStatus()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
{
|
||||
return DownloadStatus.CreateDefaultStatus();
|
||||
}
|
||||
return Provider.GetDownloadStatus();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前状态
|
||||
/// </summary>
|
||||
public EOperationStatus Status
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return EOperationStatus.None;
|
||||
|
||||
return Provider.Status;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 最近的错误信息
|
||||
/// </summary>
|
||||
public string LastError
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return string.Empty;
|
||||
return Provider.Error;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载进度
|
||||
/// </summary>
|
||||
public float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return 0;
|
||||
return Provider.Progress;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否加载完毕
|
||||
/// </summary>
|
||||
public bool IsDone
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return false;
|
||||
return Provider.IsDone;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 句柄是否有效
|
||||
/// </summary>
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Provider != null && Provider.IsDestroyed == false)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 句柄是否有效
|
||||
/// </summary>
|
||||
internal bool IsValidWithWarning
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Provider != null && Provider.IsDestroyed == false)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Provider == null)
|
||||
YooLogger.Warning($"Operation handle is released : {_assetInfo.AssetPath}");
|
||||
else if (Provider.IsDestroyed)
|
||||
YooLogger.Warning($"Provider is destroyed : {_assetInfo.AssetPath}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放句柄
|
||||
/// </summary>
|
||||
internal void ReleaseInternal()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return;
|
||||
Provider.ReleaseHandle(this);
|
||||
Provider = null;
|
||||
}
|
||||
|
||||
#region 异步操作相关
|
||||
/// <summary>
|
||||
/// 异步操作任务
|
||||
/// </summary>
|
||||
public System.Threading.Tasks.Task Task
|
||||
{
|
||||
get { return Provider.Task; }
|
||||
}
|
||||
|
||||
// 协程相关
|
||||
bool IEnumerator.MoveNext()
|
||||
{
|
||||
return !IsDone;
|
||||
}
|
||||
void IEnumerator.Reset()
|
||||
{
|
||||
}
|
||||
object IEnumerator.Current
|
||||
{
|
||||
get { return Provider; }
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a9f5acda1a4584648af7b47e19a0d24f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public class RawFileHandle : HandleBase, IDisposable
|
||||
{
|
||||
private System.Action<RawFileHandle> _callback;
|
||||
|
||||
internal RawFileHandle(ProviderBase provider) : base(provider)
|
||||
{
|
||||
}
|
||||
internal override void InvokeCallback()
|
||||
{
|
||||
_callback?.Invoke(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 完成委托
|
||||
/// </summary>
|
||||
public event System.Action<RawFileHandle> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(RawFileHandle)} is invalid");
|
||||
if (Provider.IsDone)
|
||||
value.Invoke(this);
|
||||
else
|
||||
_callback += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(RawFileHandle)} is invalid");
|
||||
_callback -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return;
|
||||
Provider.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
this.ReleaseInternal();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
this.ReleaseInternal();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取原生文件的二进制数据
|
||||
/// </summary>
|
||||
public byte[] GetRawFileData()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return null;
|
||||
string filePath = Provider.RawFilePath;
|
||||
return FileUtility.ReadAllBytes(filePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原生文件的文本数据
|
||||
/// </summary>
|
||||
public string GetRawFileText()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return null;
|
||||
string filePath = Provider.RawFilePath;
|
||||
return FileUtility.ReadAllText(filePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取原生文件的路径
|
||||
/// </summary>
|
||||
public string GetRawFilePath()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return string.Empty;
|
||||
return Provider.RawFilePath;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8420ba734d425a4ba9f19173d74503c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,187 @@
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public class SceneHandle : HandleBase
|
||||
{
|
||||
private System.Action<SceneHandle> _callback;
|
||||
internal string PackageName { set; get; }
|
||||
|
||||
internal SceneHandle(ProviderBase provider) : base(provider)
|
||||
{
|
||||
}
|
||||
internal override void InvokeCallback()
|
||||
{
|
||||
_callback?.Invoke(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 完成委托
|
||||
/// </summary>
|
||||
public event System.Action<SceneHandle> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(SceneHandle)} is invalid");
|
||||
if (Provider.IsDone)
|
||||
value.Invoke(this);
|
||||
else
|
||||
_callback += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(SceneHandle)} is invalid");
|
||||
_callback -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
internal void WaitForAsyncComplete()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return;
|
||||
Provider.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 场景名称
|
||||
/// </summary>
|
||||
public string SceneName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return string.Empty;
|
||||
return Provider.SceneName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 场景对象
|
||||
/// </summary>
|
||||
public Scene SceneObject
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return new Scene();
|
||||
return Provider.SceneObject;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 激活场景(当同时存在多个场景时用于切换激活场景)
|
||||
/// </summary>
|
||||
public bool ActivateScene()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return false;
|
||||
|
||||
if (SceneObject.IsValid() && SceneObject.isLoaded)
|
||||
{
|
||||
return SceneManager.SetActiveScene(SceneObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
YooLogger.Warning($"Scene is invalid or not loaded : {SceneObject.name}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解除场景加载挂起操作
|
||||
/// </summary>
|
||||
public bool UnSuspend()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return false;
|
||||
|
||||
if (SceneObject.IsValid())
|
||||
{
|
||||
if (Provider is DatabaseSceneProvider)
|
||||
{
|
||||
var temp = Provider as DatabaseSceneProvider;
|
||||
return temp.UnSuspendLoad();
|
||||
}
|
||||
else if (Provider is BundledSceneProvider)
|
||||
{
|
||||
var temp = Provider as BundledSceneProvider;
|
||||
return temp.UnSuspendLoad();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
YooLogger.Warning($"Scene is invalid : {SceneObject.name}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为主场景
|
||||
/// </summary>
|
||||
public bool IsMainScene()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return false;
|
||||
|
||||
if (Provider is DatabaseSceneProvider)
|
||||
{
|
||||
var temp = Provider as DatabaseSceneProvider;
|
||||
return temp.SceneMode == LoadSceneMode.Single;
|
||||
}
|
||||
else if (Provider is BundledSceneProvider)
|
||||
{
|
||||
var temp = Provider as BundledSceneProvider;
|
||||
return temp.SceneMode == LoadSceneMode.Single;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步卸载子场景
|
||||
/// </summary>
|
||||
public UnloadSceneOperation UnloadAsync()
|
||||
{
|
||||
string packageName = GetAssetInfo().PackageName;
|
||||
|
||||
// 如果句柄无效
|
||||
if (IsValidWithWarning == false)
|
||||
{
|
||||
string error = $"{nameof(SceneHandle)} is invalid.";
|
||||
var operation = new UnloadSceneOperation(error);
|
||||
OperationSystem.StartOperation(packageName, operation);
|
||||
return operation;
|
||||
}
|
||||
|
||||
// 如果是主场景
|
||||
if (IsMainScene())
|
||||
{
|
||||
string error = $"Cannot unload main scene. Use {nameof(YooAssets.LoadSceneAsync)} method to change the main scene !";
|
||||
YooLogger.Error(error);
|
||||
var operation = new UnloadSceneOperation(error);
|
||||
OperationSystem.StartOperation(packageName, operation);
|
||||
return operation;
|
||||
}
|
||||
|
||||
// 卸载子场景
|
||||
{
|
||||
var operation = new UnloadSceneOperation(Provider);
|
||||
OperationSystem.StartOperation(packageName, operation);
|
||||
return operation;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c5d788ba2dd3c5243ae2e0be8e780825
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public sealed class SubAssetsHandle : HandleBase, IDisposable
|
||||
{
|
||||
private System.Action<SubAssetsHandle> _callback;
|
||||
|
||||
internal SubAssetsHandle(ProviderBase provider) : base(provider)
|
||||
{
|
||||
}
|
||||
internal override void InvokeCallback()
|
||||
{
|
||||
_callback?.Invoke(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 完成委托
|
||||
/// </summary>
|
||||
public event System.Action<SubAssetsHandle> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(SubAssetsHandle)} is invalid");
|
||||
if (Provider.IsDone)
|
||||
value.Invoke(this);
|
||||
else
|
||||
_callback += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
throw new System.Exception($"{nameof(SubAssetsHandle)} is invalid");
|
||||
_callback -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return;
|
||||
Provider.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
this.ReleaseInternal();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
this.ReleaseInternal();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 子资源对象集合
|
||||
/// </summary>
|
||||
public UnityEngine.Object[] AllAssetObjects
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return null;
|
||||
return Provider.AllAssetObjects;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取子资源对象
|
||||
/// </summary>
|
||||
/// <typeparam name="TObject">子资源对象类型</typeparam>
|
||||
/// <param name="assetName">子资源对象名称</param>
|
||||
public TObject GetSubAssetObject<TObject>(string assetName) where TObject : UnityEngine.Object
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return null;
|
||||
|
||||
foreach (var assetObject in Provider.AllAssetObjects)
|
||||
{
|
||||
if (assetObject.name == assetName)
|
||||
return assetObject as TObject;
|
||||
}
|
||||
|
||||
YooLogger.Warning($"Not found sub asset object : {assetName}");
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的子资源对象集合
|
||||
/// </summary>
|
||||
/// <typeparam name="TObject">子资源对象类型</typeparam>
|
||||
public TObject[] GetSubAssetObjects<TObject>() where TObject : UnityEngine.Object
|
||||
{
|
||||
if (IsValidWithWarning == false)
|
||||
return null;
|
||||
|
||||
List<TObject> ret = new List<TObject>(Provider.AllAssetObjects.Length);
|
||||
foreach (var assetObject in Provider.AllAssetObjects)
|
||||
{
|
||||
var retObject = assetObject as TObject;
|
||||
if (retObject != null)
|
||||
ret.Add(retObject);
|
||||
}
|
||||
return ret.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76b148be04a698e45a54dd85e64969dd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58462a7dcef164e43878a037395d4417
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,294 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class AssetBundleFileLoader : BundleLoaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None = 0,
|
||||
Download,
|
||||
CheckDownload,
|
||||
Unpack,
|
||||
CheckUnpack,
|
||||
LoadBundleFile,
|
||||
LoadDeliveryFile,
|
||||
CheckLoadFile,
|
||||
Done,
|
||||
}
|
||||
|
||||
private ESteps _steps = ESteps.None;
|
||||
private bool _isWaitForAsyncComplete = false;
|
||||
private bool _isShowWaitForAsyncError = false;
|
||||
private DownloaderBase _unpacker;
|
||||
private DownloaderBase _downloader;
|
||||
private AssetBundleCreateRequest _createRequest;
|
||||
private Stream _managedStream;
|
||||
|
||||
|
||||
public AssetBundleFileLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新
|
||||
/// </summary>
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
|
||||
{
|
||||
_steps = ESteps.Download;
|
||||
FileLoadPath = MainBundleInfo.CachedDataFilePath;
|
||||
}
|
||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
if (MainBundleInfo.Bundle.Encrypted)
|
||||
{
|
||||
_steps = ESteps.Unpack;
|
||||
FileLoadPath = MainBundleInfo.CachedDataFilePath;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.LoadBundleFile;
|
||||
FileLoadPath = MainBundleInfo.BuildinFilePath;
|
||||
}
|
||||
#else
|
||||
_steps = ESteps.LoadBundleFile;
|
||||
FileLoadPath = MainBundleInfo.BuildinFilePath;
|
||||
#endif
|
||||
}
|
||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
|
||||
{
|
||||
_steps = ESteps.LoadBundleFile;
|
||||
FileLoadPath = MainBundleInfo.CachedDataFilePath;
|
||||
}
|
||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromDelivery)
|
||||
{
|
||||
_steps = ESteps.LoadDeliveryFile;
|
||||
FileLoadPath = MainBundleInfo.DeliveryFilePath;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 从服务器下载
|
||||
if (_steps == ESteps.Download)
|
||||
{
|
||||
_downloader = MainBundleInfo.CreateDownloader(int.MaxValue);
|
||||
_downloader.SendRequest();
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
// 2. 检测服务器下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
DownloadProgress = _downloader.DownloadProgress;
|
||||
DownloadedBytes = _downloader.DownloadedBytes;
|
||||
if (_downloader.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_downloader.HasError())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = _downloader.GetLastError();
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.LoadBundleFile;
|
||||
return; //下载完毕等待一帧再去加载!
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 内置文件解压
|
||||
if (_steps == ESteps.Unpack)
|
||||
{
|
||||
int failedTryAgain = 1;
|
||||
_unpacker = MainBundleInfo.CreateUnpacker(failedTryAgain);
|
||||
_unpacker.SendRequest();
|
||||
_steps = ESteps.CheckUnpack;
|
||||
}
|
||||
|
||||
// 4.检测内置文件解压结果
|
||||
if (_steps == ESteps.CheckUnpack)
|
||||
{
|
||||
DownloadProgress = _unpacker.DownloadProgress;
|
||||
DownloadedBytes = _unpacker.DownloadedBytes;
|
||||
if (_unpacker.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_unpacker.HasError())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = _unpacker.GetLastError();
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.LoadBundleFile;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 加载AssetBundle
|
||||
if (_steps == ESteps.LoadBundleFile)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。
|
||||
if (System.IO.File.Exists(FileLoadPath) == false)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = $"Not found assetBundle file : {FileLoadPath}";
|
||||
YooLogger.Error(LastError);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 设置下载进度
|
||||
DownloadProgress = 1f;
|
||||
DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize;
|
||||
|
||||
// 加载AssetBundle资源对象
|
||||
// 注意:解密服务类可能会返回空的对象。
|
||||
if (_isWaitForAsyncComplete)
|
||||
CacheBundle = MainBundleInfo.LoadAssetBundle(FileLoadPath, out _managedStream);
|
||||
else
|
||||
_createRequest = MainBundleInfo.LoadAssetBundleAsync(FileLoadPath, out _managedStream);
|
||||
|
||||
_steps = ESteps.CheckLoadFile;
|
||||
}
|
||||
|
||||
// 6. 加载AssetBundle
|
||||
if (_steps == ESteps.LoadDeliveryFile)
|
||||
{
|
||||
// 设置下载进度
|
||||
DownloadProgress = 1f;
|
||||
DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize;
|
||||
|
||||
// Load assetBundle file
|
||||
if (_isWaitForAsyncComplete)
|
||||
CacheBundle = MainBundleInfo.LoadDeliveryAssetBundle(FileLoadPath);
|
||||
else
|
||||
_createRequest = MainBundleInfo.LoadDeliveryAssetBundleAsync(FileLoadPath);
|
||||
|
||||
_steps = ESteps.CheckLoadFile;
|
||||
}
|
||||
|
||||
// 7. 检测AssetBundle加载结果
|
||||
if (_steps == ESteps.CheckLoadFile)
|
||||
{
|
||||
if (_createRequest != null)
|
||||
{
|
||||
if (_isWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
// 强制挂起主线程(注意:该操作会很耗时)
|
||||
YooLogger.Warning("Suspend the main thread to load unity bundle.");
|
||||
CacheBundle = _createRequest.assetBundle;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_createRequest.isDone == false)
|
||||
return;
|
||||
CacheBundle = _createRequest.assetBundle;
|
||||
}
|
||||
}
|
||||
|
||||
// Check error
|
||||
if (CacheBundle == null)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = $"Failed to load assetBundle : {MainBundleInfo.Bundle.BundleName}";
|
||||
YooLogger.Error(LastError);
|
||||
|
||||
// 注意:当缓存文件的校验等级为Low的时候,并不能保证缓存文件的完整性。
|
||||
// 在AssetBundle文件加载失败的情况下,我们需要重新验证文件的完整性!
|
||||
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
|
||||
{
|
||||
var result = MainBundleInfo.VerifySelf();
|
||||
if (result != EVerifyResult.Succeed)
|
||||
{
|
||||
YooLogger.Error($"Found possibly corrupt file ! {MainBundleInfo.Bundle.CacheGUID} Verify result : {result}");
|
||||
MainBundleInfo.CacheDiscard();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Succeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁
|
||||
/// </summary>
|
||||
public override void Destroy()
|
||||
{
|
||||
base.Destroy();
|
||||
|
||||
if (_managedStream != null)
|
||||
{
|
||||
_managedStream.Close();
|
||||
_managedStream.Dispose();
|
||||
_managedStream = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程等待异步操作完毕
|
||||
/// </summary>
|
||||
public override void WaitForAsyncComplete()
|
||||
{
|
||||
_isWaitForAsyncComplete = true;
|
||||
|
||||
int frame = 1000;
|
||||
while (true)
|
||||
{
|
||||
// 文件解压
|
||||
if (_unpacker != null)
|
||||
{
|
||||
if (_unpacker.IsDone() == false)
|
||||
{
|
||||
_unpacker.WaitForAsyncComplete = true;
|
||||
_unpacker.Update();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 保险机制
|
||||
// 注意:如果需要从WEB端下载资源,可能会触发保险机制!
|
||||
frame--;
|
||||
if (frame == 0)
|
||||
{
|
||||
if (_isShowWaitForAsyncError == false)
|
||||
{
|
||||
_isShowWaitForAsyncError = true;
|
||||
YooLogger.Error($"{nameof(WaitForAsyncComplete)} failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 驱动流程
|
||||
Update();
|
||||
|
||||
// 完成后退出
|
||||
if (IsDone())
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64d0a51f59c2a964eaf97b87db47428f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// WebGL平台加载器
|
||||
/// </summary>
|
||||
internal sealed class AssetBundleWebLoader : BundleLoaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None = 0,
|
||||
LoadWebSiteFile,
|
||||
LoadRemoteFile,
|
||||
CheckLoadFile,
|
||||
Done,
|
||||
}
|
||||
|
||||
private ESteps _steps = ESteps.None;
|
||||
private DownloaderBase _downloader;
|
||||
|
||||
|
||||
public AssetBundleWebLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新
|
||||
/// </summary>
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
|
||||
{
|
||||
_steps = ESteps.LoadRemoteFile;
|
||||
FileLoadPath = string.Empty;
|
||||
}
|
||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
|
||||
{
|
||||
_steps = ESteps.LoadWebSiteFile;
|
||||
FileLoadPath = string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 跨域获取资源包
|
||||
if (_steps == ESteps.LoadRemoteFile)
|
||||
{
|
||||
_downloader = MainBundleInfo.CreateDownloader(int.MaxValue);
|
||||
_downloader.SendRequest(true);
|
||||
_steps = ESteps.CheckLoadFile;
|
||||
}
|
||||
|
||||
// 2. 从站点获取资源包
|
||||
if (_steps == ESteps.LoadWebSiteFile)
|
||||
{
|
||||
_downloader = MainBundleInfo.CreateUnpacker(int.MaxValue);
|
||||
_downloader.SendRequest(true);
|
||||
_steps = ESteps.CheckLoadFile;
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (_steps == ESteps.CheckLoadFile)
|
||||
{
|
||||
DownloadProgress = _downloader.DownloadProgress;
|
||||
DownloadedBytes = _downloader.DownloadedBytes;
|
||||
if (_downloader.IsDone() == false)
|
||||
return;
|
||||
|
||||
CacheBundle = _downloader.GetAssetBundle();
|
||||
if (CacheBundle == null)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = $"AssetBundle file is invalid : {MainBundleInfo.Bundle.BundleName}";
|
||||
YooLogger.Error(LastError);
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Succeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程等待异步操作完毕
|
||||
/// </summary>
|
||||
public override void WaitForAsyncComplete()
|
||||
{
|
||||
if (IsDone() == false)
|
||||
{
|
||||
Status = EStatus.Failed;
|
||||
LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !";
|
||||
YooLogger.Error(LastError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bd1ad395c62c5804589ec9b267ea0484
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,181 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal abstract class BundleLoaderBase
|
||||
{
|
||||
public enum EStatus
|
||||
{
|
||||
None = 0,
|
||||
Succeed,
|
||||
Failed
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 所属资源系统
|
||||
/// </summary>
|
||||
public ResourceManager Impl { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源包文件信息
|
||||
/// </summary>
|
||||
public BundleInfo MainBundleInfo { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数
|
||||
/// </summary>
|
||||
public int RefCount { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 加载状态
|
||||
/// </summary>
|
||||
public EStatus Status { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 最近的错误信息
|
||||
/// </summary>
|
||||
public string LastError { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经销毁
|
||||
/// </summary>
|
||||
public bool IsDestroyed { private set; get; } = false;
|
||||
|
||||
private readonly List<ProviderBase> _providers = new List<ProviderBase>(100);
|
||||
private readonly List<ProviderBase> _removeList = new List<ProviderBase>(100);
|
||||
protected bool IsForceDestroyComplete { private set; get; } = false;
|
||||
internal AssetBundle CacheBundle { set; get; }
|
||||
internal string FileLoadPath { set; get; }
|
||||
internal float DownloadProgress { set; get; }
|
||||
internal ulong DownloadedBytes { set; get; }
|
||||
|
||||
|
||||
public BundleLoaderBase(ResourceManager impl, BundleInfo bundleInfo)
|
||||
{
|
||||
Impl = impl;
|
||||
MainBundleInfo = bundleInfo;
|
||||
RefCount = 0;
|
||||
Status = EStatus.None;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 引用(引用计数递加)
|
||||
/// </summary>
|
||||
public void Reference()
|
||||
{
|
||||
RefCount++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放(引用计数递减)
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
RefCount--;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否完毕(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
return Status == EStatus.Succeed || Status == EStatus.Failed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以销毁
|
||||
/// </summary>
|
||||
public bool CanDestroy()
|
||||
{
|
||||
if (IsDone() == false)
|
||||
return false;
|
||||
|
||||
return RefCount <= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加附属的资源提供者
|
||||
/// </summary>
|
||||
public void AddProvider(ProviderBase provider)
|
||||
{
|
||||
if (_providers.Contains(provider) == false)
|
||||
_providers.Add(provider);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试销毁资源提供者
|
||||
/// </summary>
|
||||
public void TryDestroyProviders()
|
||||
{
|
||||
// 获取移除列表
|
||||
_removeList.Clear();
|
||||
foreach (var provider in _providers)
|
||||
{
|
||||
if (provider.CanDestroy())
|
||||
{
|
||||
_removeList.Add(provider);
|
||||
}
|
||||
}
|
||||
|
||||
// 销毁资源提供者
|
||||
foreach (var provider in _removeList)
|
||||
{
|
||||
_providers.Remove(provider);
|
||||
provider.Destroy();
|
||||
}
|
||||
|
||||
// 移除资源提供者
|
||||
if (_removeList.Count > 0)
|
||||
{
|
||||
Impl.RemoveBundleProviders(_removeList);
|
||||
_removeList.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新
|
||||
/// </summary>
|
||||
public abstract void Update();
|
||||
|
||||
/// <summary>
|
||||
/// 销毁
|
||||
/// </summary>
|
||||
public virtual void Destroy()
|
||||
{
|
||||
IsDestroyed = true;
|
||||
|
||||
// Check fatal
|
||||
if (RefCount > 0)
|
||||
throw new Exception($"Bundle file loader ref is not zero : {MainBundleInfo.Bundle.BundleName}");
|
||||
if (IsDone() == false)
|
||||
throw new Exception($"Bundle file loader is not done : {MainBundleInfo.Bundle.BundleName}");
|
||||
|
||||
if (CacheBundle != null)
|
||||
{
|
||||
CacheBundle.Unload(true);
|
||||
CacheBundle = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 强制销毁资源提供者
|
||||
/// </summary>
|
||||
public void ForceDestroyComplete()
|
||||
{
|
||||
IsForceDestroyComplete = true;
|
||||
|
||||
// 注意:主动轮询更新完成同步加载
|
||||
// 说明:如果正在下载或解压也可以放心销毁。
|
||||
Update();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程等待异步操作完毕
|
||||
/// </summary>
|
||||
public abstract void WaitForAsyncComplete();
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8aaabc43e74af14d8eb3c7c85e19cda
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class DependAssetBundles
|
||||
{
|
||||
/// <summary>
|
||||
/// 依赖的资源包加载器列表
|
||||
/// </summary>
|
||||
internal readonly List<BundleLoaderBase> DependList;
|
||||
|
||||
|
||||
public DependAssetBundles(List<BundleLoaderBase> dpendList)
|
||||
{
|
||||
DependList = dpendList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经完成(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
foreach (var loader in DependList)
|
||||
{
|
||||
if (loader.IsDone() == false)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 依赖资源包是否全部加载成功
|
||||
/// </summary>
|
||||
public bool IsSucceed()
|
||||
{
|
||||
foreach (var loader in DependList)
|
||||
{
|
||||
if (loader.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取某个加载失败的资源包错误信息
|
||||
/// </summary>
|
||||
public string GetLastError()
|
||||
{
|
||||
foreach (var loader in DependList)
|
||||
{
|
||||
if (loader.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
return loader.LastError;
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程等待异步操作完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
foreach (var loader in DependList)
|
||||
{
|
||||
if (loader.IsDone() == false)
|
||||
loader.WaitForAsyncComplete();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加引用计数
|
||||
/// </summary>
|
||||
public void Reference()
|
||||
{
|
||||
foreach (var loader in DependList)
|
||||
{
|
||||
loader.Reference();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 减少引用计数
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
foreach (var loader in DependList)
|
||||
{
|
||||
loader.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源包的调试信息列表
|
||||
/// </summary>
|
||||
internal void GetBundleDebugInfos(List<DebugBundleInfo> output)
|
||||
{
|
||||
foreach (var loader in DependList)
|
||||
{
|
||||
var bundleInfo = new DebugBundleInfo();
|
||||
bundleInfo.BundleName = loader.MainBundleInfo.Bundle.BundleName;
|
||||
bundleInfo.RefCount = loader.RefCount;
|
||||
bundleInfo.Status = loader.Status.ToString();
|
||||
output.Add(bundleInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b7eeccc96e4c31c45bd105b0dbc3fd6e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,188 @@
|
||||
using System.IO;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class RawBundleFileLoader : BundleLoaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
Download,
|
||||
CheckDownload,
|
||||
Unpack,
|
||||
CheckUnpack,
|
||||
CheckFile,
|
||||
Done,
|
||||
}
|
||||
|
||||
private ESteps _steps = ESteps.None;
|
||||
private DownloaderBase _unpacker;
|
||||
private DownloaderBase _downloader;
|
||||
|
||||
|
||||
public RawBundleFileLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新
|
||||
/// </summary>
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
|
||||
{
|
||||
_steps = ESteps.Download;
|
||||
FileLoadPath = MainBundleInfo.CachedDataFilePath;
|
||||
}
|
||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
_steps = ESteps.Unpack;
|
||||
FileLoadPath = MainBundleInfo.CachedDataFilePath;
|
||||
#else
|
||||
_steps = ESteps.CheckFile;
|
||||
FileLoadPath = MainBundleInfo.BuildinFilePath;
|
||||
#endif
|
||||
}
|
||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
|
||||
{
|
||||
_steps = ESteps.CheckFile;
|
||||
FileLoadPath = MainBundleInfo.CachedDataFilePath;
|
||||
}
|
||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromDelivery)
|
||||
{
|
||||
_steps = ESteps.CheckFile;
|
||||
FileLoadPath = MainBundleInfo.DeliveryFilePath;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 下载远端文件
|
||||
if (_steps == ESteps.Download)
|
||||
{
|
||||
_downloader = MainBundleInfo.CreateDownloader(int.MaxValue);
|
||||
_downloader.SendRequest();
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
// 2. 检测下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
DownloadProgress = _downloader.DownloadProgress;
|
||||
DownloadedBytes = _downloader.DownloadedBytes;
|
||||
if (_downloader.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_downloader.HasError())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = _downloader.GetLastError();
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.CheckFile;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 解压内置文件
|
||||
if (_steps == ESteps.Unpack)
|
||||
{
|
||||
int failedTryAgain = 1;
|
||||
_unpacker = MainBundleInfo.CreateUnpacker(failedTryAgain);
|
||||
_unpacker.SendRequest();
|
||||
_steps = ESteps.CheckUnpack;
|
||||
}
|
||||
|
||||
// 4. 检测解压结果
|
||||
if (_steps == ESteps.CheckUnpack)
|
||||
{
|
||||
DownloadProgress = _unpacker.DownloadProgress;
|
||||
DownloadedBytes = _unpacker.DownloadedBytes;
|
||||
if (_unpacker.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_unpacker.HasError())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = _unpacker.GetLastError();
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.CheckFile;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 检测结果
|
||||
if (_steps == ESteps.CheckFile)
|
||||
{
|
||||
// 设置下载进度
|
||||
DownloadProgress = 1f;
|
||||
DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize;
|
||||
|
||||
if (File.Exists(FileLoadPath))
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = $"Raw file not found : {FileLoadPath}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程等待异步操作完毕
|
||||
/// </summary>
|
||||
public override void WaitForAsyncComplete()
|
||||
{
|
||||
int frame = 1000;
|
||||
while (true)
|
||||
{
|
||||
// 文件解压
|
||||
if (_unpacker != null)
|
||||
{
|
||||
if (_unpacker.IsDone() == false)
|
||||
{
|
||||
_unpacker.WaitForAsyncComplete = true;
|
||||
_unpacker.Update();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 保险机制
|
||||
// 注意:如果需要从远端下载资源,可能会触发保险机制!
|
||||
frame--;
|
||||
if (frame == 0)
|
||||
{
|
||||
if (IsDone() == false)
|
||||
{
|
||||
Status = EStatus.Failed;
|
||||
LastError = $"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !";
|
||||
YooLogger.Error(LastError);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 驱动流程
|
||||
Update();
|
||||
|
||||
// 完成后退出
|
||||
if (IsDone())
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fa39f24727abe514093f18787c0c7e27
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,145 @@
|
||||
using System.IO;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// WebGL平台加载器
|
||||
/// </summary>
|
||||
internal class RawBundleWebLoader : BundleLoaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
Download,
|
||||
CheckDownload,
|
||||
Website,
|
||||
CheckWebsite,
|
||||
CheckFile,
|
||||
Done,
|
||||
}
|
||||
|
||||
private ESteps _steps = ESteps.None;
|
||||
private DownloaderBase _website;
|
||||
private DownloaderBase _downloader;
|
||||
|
||||
|
||||
public RawBundleWebLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新
|
||||
/// </summary>
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
|
||||
{
|
||||
_steps = ESteps.Download;
|
||||
FileLoadPath = MainBundleInfo.CachedDataFilePath;
|
||||
}
|
||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
|
||||
{
|
||||
_steps = ESteps.Website;
|
||||
FileLoadPath = MainBundleInfo.CachedDataFilePath;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 下载远端文件
|
||||
if (_steps == ESteps.Download)
|
||||
{
|
||||
_downloader = MainBundleInfo.CreateDownloader(int.MaxValue);
|
||||
_downloader.SendRequest();
|
||||
_steps = ESteps.CheckDownload;
|
||||
}
|
||||
|
||||
// 2. 检测下载结果
|
||||
if (_steps == ESteps.CheckDownload)
|
||||
{
|
||||
DownloadProgress = _downloader.DownloadProgress;
|
||||
DownloadedBytes = _downloader.DownloadedBytes;
|
||||
if (_downloader.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_downloader.HasError())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = _downloader.GetLastError();
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.CheckFile;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 从站点下载
|
||||
if (_steps == ESteps.Website)
|
||||
{
|
||||
_website = MainBundleInfo.CreateUnpacker(int.MaxValue);
|
||||
_website.SendRequest();
|
||||
_steps = ESteps.CheckWebsite;
|
||||
}
|
||||
|
||||
// 4. 检测站点下载
|
||||
if (_steps == ESteps.CheckWebsite)
|
||||
{
|
||||
DownloadProgress = _website.DownloadProgress;
|
||||
DownloadedBytes = _website.DownloadedBytes;
|
||||
if (_website.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_website.HasError())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Failed;
|
||||
LastError = _website.GetLastError();
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.CheckFile;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 检测结果
|
||||
if (_steps == ESteps.CheckFile)
|
||||
{
|
||||
// 设置下载进度
|
||||
DownloadProgress = 1f;
|
||||
DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize;
|
||||
|
||||
_steps = ESteps.Done;
|
||||
if (File.Exists(FileLoadPath))
|
||||
{
|
||||
Status = EStatus.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = EStatus.Failed;
|
||||
LastError = $"Raw file not found : {FileLoadPath}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程等待异步操作完毕
|
||||
/// </summary>
|
||||
public override void WaitForAsyncComplete()
|
||||
{
|
||||
if (IsDone() == false)
|
||||
{
|
||||
Status = EStatus.Failed;
|
||||
LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !";
|
||||
YooLogger.Error(LastError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1d16b689f73611e44bd01a4cc429a6ac
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,82 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class VirtualBundleFileLoader : BundleLoaderBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
CheckFile,
|
||||
Done,
|
||||
}
|
||||
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
public VirtualBundleFileLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新
|
||||
/// </summary>
|
||||
public override void Update()
|
||||
{
|
||||
if (_steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromEditor)
|
||||
{
|
||||
_steps = ESteps.CheckFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 检测结果
|
||||
if (_steps == ESteps.CheckFile)
|
||||
{
|
||||
// 设置下载进度
|
||||
DownloadProgress = 1f;
|
||||
DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize;
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Status = EStatus.Succeed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程等待异步操作完毕
|
||||
/// </summary>
|
||||
public override void WaitForAsyncComplete()
|
||||
{
|
||||
int frame = 1000;
|
||||
while (true)
|
||||
{
|
||||
// 保险机制
|
||||
// 注意:如果需要从远端下载资源,可能会触发保险机制!
|
||||
frame--;
|
||||
if (frame == 0)
|
||||
{
|
||||
if (IsDone() == false)
|
||||
{
|
||||
Status = EStatus.Failed;
|
||||
LastError = $"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !";
|
||||
YooLogger.Error(LastError);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 驱动流程
|
||||
Update();
|
||||
|
||||
// 完成后退出
|
||||
if (IsDone())
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7b9023a940b496549894d9d8872219fb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fcfac83df022948478c32bf11b8e0987
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,132 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public sealed class InstantiateOperation : AsyncOperationBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
Clone,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly AssetHandle _handle;
|
||||
private readonly bool _setPositionAndRotation;
|
||||
private readonly Vector3 _position;
|
||||
private readonly Quaternion _rotation;
|
||||
private readonly Transform _parent;
|
||||
private readonly bool _worldPositionStays;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
/// <summary>
|
||||
/// 实例化的游戏对象
|
||||
/// </summary>
|
||||
public GameObject Result = null;
|
||||
|
||||
|
||||
internal InstantiateOperation(AssetHandle handle, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
|
||||
{
|
||||
_handle = handle;
|
||||
_setPositionAndRotation = setPositionAndRotation;
|
||||
_position = position;
|
||||
_rotation = rotation;
|
||||
_parent = parent;
|
||||
_worldPositionStays = worldPositionStays;
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
_steps = ESteps.Clone;
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.Clone)
|
||||
{
|
||||
if (_handle.IsValidWithWarning == false)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"{nameof(AssetHandle)} is invalid.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (_handle.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_handle.AssetObject == null)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"{nameof(AssetHandle.AssetObject)} is null.";
|
||||
return;
|
||||
}
|
||||
|
||||
// 实例化游戏对象
|
||||
Result = InstantiateInternal(_handle.AssetObject, _setPositionAndRotation, _position, _rotation, _parent, _worldPositionStays);
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消实例化对象操作
|
||||
/// </summary>
|
||||
public void Cancel()
|
||||
{
|
||||
if (IsDone == false)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"User cancelled !";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步实例化结束
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
if (_steps == ESteps.Done)
|
||||
return;
|
||||
_handle.WaitForAsyncComplete();
|
||||
InternalOnUpdate();
|
||||
}
|
||||
|
||||
internal static GameObject InstantiateInternal(UnityEngine.Object assetObject, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
|
||||
{
|
||||
if (assetObject == null)
|
||||
return null;
|
||||
|
||||
if (setPositionAndRotation)
|
||||
{
|
||||
if (parent != null)
|
||||
{
|
||||
GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation, parent);
|
||||
return clone;
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation);
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (parent != null)
|
||||
{
|
||||
GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, parent, worldPositionStays);
|
||||
return clone;
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject);
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e7d3b16d9b01f548b4654e958d43a37
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,99 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 场景卸载异步操作类
|
||||
/// </summary>
|
||||
public sealed class UnloadSceneOperation : AsyncOperationBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
CheckError,
|
||||
PrepareDone,
|
||||
UnLoadScene,
|
||||
Checking,
|
||||
Done,
|
||||
}
|
||||
|
||||
private ESteps _steps = ESteps.None;
|
||||
private readonly string _error;
|
||||
private readonly ProviderBase _provider;
|
||||
private AsyncOperation _asyncOp;
|
||||
|
||||
internal UnloadSceneOperation(string error)
|
||||
{
|
||||
_error = error;
|
||||
}
|
||||
internal UnloadSceneOperation(ProviderBase provider)
|
||||
{
|
||||
_error = null;
|
||||
_provider = provider;
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
_steps = ESteps.CheckError;
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.CheckError)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_error) == false)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = _error;
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.PrepareDone;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.PrepareDone)
|
||||
{
|
||||
if (_provider.IsDone == false)
|
||||
return;
|
||||
|
||||
if (_provider.SceneObject.IsValid() == false)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = "Scene is invalid !";
|
||||
return;
|
||||
}
|
||||
|
||||
if (_provider.SceneObject.isLoaded == false)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = "Scene is not loaded !";
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.UnLoadScene;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.UnLoadScene)
|
||||
{
|
||||
_asyncOp = SceneManager.UnloadSceneAsync(_provider.SceneObject);
|
||||
_provider.ResourceMgr.UnloadSubScene(_provider.SceneName);
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
Progress = _asyncOp.progress;
|
||||
if (_asyncOp.isDone == false)
|
||||
return;
|
||||
_provider.ResourceMgr.TryUnloadUnusedAsset(_provider.MainAssetInfo);
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5299512de5ab5c141ae37dac6ee1721e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c64025c73372d9d4cab02c9cf9436e55
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,122 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class BundledAllAssetsProvider : ProviderBase
|
||||
{
|
||||
private AssetBundleRequest _cacheRequest;
|
||||
|
||||
public BundledAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_steps = ESteps.CheckBundle;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
DependBundles.WaitForAsyncComplete();
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (DependBundles.IsDone() == false)
|
||||
return;
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (DependBundles.IsSucceed() == false)
|
||||
{
|
||||
string error = DependBundles.GetLastError();
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
if (OwnerBundle.CacheBundle == null)
|
||||
{
|
||||
ProcessCacheBundleException();
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Loading;
|
||||
}
|
||||
|
||||
// 2. 加载资源对象
|
||||
if (_steps == ESteps.Loading)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
AllAssetObjects = OwnerBundle.CacheBundle.LoadAllAssets();
|
||||
else
|
||||
AllAssetObjects = OwnerBundle.CacheBundle.LoadAllAssets(MainAssetInfo.AssetType);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync();
|
||||
else
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync(MainAssetInfo.AssetType);
|
||||
}
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (_cacheRequest != null)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
// 强制挂起主线程(注意:该操作会很耗时)
|
||||
YooLogger.Warning("Suspend the main thread to load unity asset.");
|
||||
AllAssetObjects = _cacheRequest.allAssets;
|
||||
}
|
||||
else
|
||||
{
|
||||
Progress = _cacheRequest.progress;
|
||||
if (_cacheRequest.isDone == false)
|
||||
return;
|
||||
AllAssetObjects = _cacheRequest.allAssets;
|
||||
}
|
||||
}
|
||||
|
||||
if (AllAssetObjects == null)
|
||||
{
|
||||
string error;
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
|
||||
else
|
||||
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
else
|
||||
{
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b0e966838827284a9266a9f2237a460
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,122 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class BundledAssetProvider : ProviderBase
|
||||
{
|
||||
private AssetBundleRequest _cacheRequest;
|
||||
|
||||
public BundledAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_steps = ESteps.CheckBundle;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
DependBundles.WaitForAsyncComplete();
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (DependBundles.IsDone() == false)
|
||||
return;
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (DependBundles.IsSucceed() == false)
|
||||
{
|
||||
string error = DependBundles.GetLastError();
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
if (OwnerBundle.CacheBundle == null)
|
||||
{
|
||||
ProcessCacheBundleException();
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Loading;
|
||||
}
|
||||
|
||||
// 2. 加载资源对象
|
||||
if (_steps == ESteps.Loading)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath);
|
||||
else
|
||||
AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath);
|
||||
else
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
|
||||
}
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (_cacheRequest != null)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
// 强制挂起主线程(注意:该操作会很耗时)
|
||||
YooLogger.Warning("Suspend the main thread to load unity asset.");
|
||||
AssetObject = _cacheRequest.asset;
|
||||
}
|
||||
else
|
||||
{
|
||||
Progress = _cacheRequest.progress;
|
||||
if (_cacheRequest.isDone == false)
|
||||
return;
|
||||
AssetObject = _cacheRequest.asset;
|
||||
}
|
||||
}
|
||||
|
||||
if (AssetObject == null)
|
||||
{
|
||||
string error;
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
|
||||
else
|
||||
error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
else
|
||||
{
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca5e4bf0c3efe6742bb57b494487be52
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,52 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class BundledRawFileProvider : ProviderBase
|
||||
{
|
||||
public BundledRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_steps = ESteps.CheckBundle;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
|
||||
// 2. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
RawFilePath = OwnerBundle.FileLoadPath;
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a00889582fd95446b103af1074fa6ba
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,140 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class BundledSceneProvider : ProviderBase
|
||||
{
|
||||
public readonly LoadSceneMode SceneMode;
|
||||
private readonly bool _suspendLoad;
|
||||
private AsyncOperation _asyncOperation;
|
||||
|
||||
public BundledSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
SceneMode = sceneMode;
|
||||
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
|
||||
_suspendLoad = suspendLoad;
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_steps = ESteps.CheckBundle;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
DependBundles.WaitForAsyncComplete();
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (DependBundles.IsDone() == false)
|
||||
return;
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (DependBundles.IsSucceed() == false)
|
||||
{
|
||||
string error = DependBundles.GetLastError();
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Loading;
|
||||
}
|
||||
|
||||
// 2. 加载场景
|
||||
if (_steps == ESteps.Loading)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
// 注意:场景同步加载方法不会立即加载场景,而是在下一帧加载。
|
||||
LoadSceneParameters parameters = new LoadSceneParameters(SceneMode);
|
||||
SceneObject = SceneManager.LoadScene(MainAssetInfo.AssetPath, parameters);
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 注意:如果场景不存在异步加载方法返回NULL
|
||||
// 注意:即使是异步加载也要在当帧获取到场景对象
|
||||
_asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode);
|
||||
if (_asyncOperation != null)
|
||||
{
|
||||
_asyncOperation.allowSceneActivation = !_suspendLoad;
|
||||
_asyncOperation.priority = 100;
|
||||
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
else
|
||||
{
|
||||
string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (_asyncOperation != null)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
// 场景加载无法强制异步转同步
|
||||
YooLogger.Error("The scene is loading asyn !");
|
||||
}
|
||||
else
|
||||
{
|
||||
Progress = _asyncOperation.progress;
|
||||
if (_asyncOperation.isDone == false)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (SceneObject.IsValid())
|
||||
{
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
else
|
||||
{
|
||||
string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解除场景加载挂起操作
|
||||
/// </summary>
|
||||
public bool UnSuspendLoad()
|
||||
{
|
||||
if (_asyncOperation == null)
|
||||
return false;
|
||||
|
||||
_asyncOperation.allowSceneActivation = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38b1b77cff590ca4e808c5068c9bf88b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,122 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class BundledSubAssetsProvider : ProviderBase
|
||||
{
|
||||
private AssetBundleRequest _cacheRequest;
|
||||
|
||||
public BundledSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_steps = ESteps.CheckBundle;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
DependBundles.WaitForAsyncComplete();
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (DependBundles.IsDone() == false)
|
||||
return;
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (DependBundles.IsSucceed() == false)
|
||||
{
|
||||
string error = DependBundles.GetLastError();
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
if (OwnerBundle.CacheBundle == null)
|
||||
{
|
||||
ProcessCacheBundleException();
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Loading;
|
||||
}
|
||||
|
||||
// 2. 加载资源对象
|
||||
if (_steps == ESteps.Loading)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetPath);
|
||||
else
|
||||
AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath);
|
||||
else
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
|
||||
}
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (_cacheRequest != null)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
// 强制挂起主线程(注意:该操作会很耗时)
|
||||
YooLogger.Warning("Suspend the main thread to load unity asset.");
|
||||
AllAssetObjects = _cacheRequest.allAssets;
|
||||
}
|
||||
else
|
||||
{
|
||||
Progress = _cacheRequest.progress;
|
||||
if (_cacheRequest.isDone == false)
|
||||
return;
|
||||
AllAssetObjects = _cacheRequest.allAssets;
|
||||
}
|
||||
}
|
||||
|
||||
if (AllAssetObjects == null)
|
||||
{
|
||||
string error;
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
|
||||
else
|
||||
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
else
|
||||
{
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3763bf52bde23b41a756f0d015cb30d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,25 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class CompletedProvider : ProviderBase
|
||||
{
|
||||
public CompletedProvider(AssetInfo assetInfo) : base(null, string.Empty, assetInfo)
|
||||
{
|
||||
}
|
||||
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
}
|
||||
|
||||
public void SetCompleted(string error)
|
||||
{
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e64fd2ee771f7d498838ebf375a9531
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,111 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class DatabaseAllAssetsProvider : ProviderBase
|
||||
{
|
||||
public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
// 检测资源文件是否存在
|
||||
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
{
|
||||
string error = $"Not found asset : {MainAssetInfo.AssetPath}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.CheckBundle;
|
||||
|
||||
// 注意:模拟异步加载效果提前返回
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Loading;
|
||||
}
|
||||
|
||||
// 2. 加载资源对象
|
||||
if (_steps == ESteps.Loading)
|
||||
{
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
{
|
||||
List<UnityEngine.Object> result = new List<Object>();
|
||||
foreach (var assetPath in OwnerBundle.MainBundleInfo.IncludeAssetsInEditor)
|
||||
{
|
||||
UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetPath);
|
||||
if (mainAsset != null)
|
||||
result.Add(mainAsset);
|
||||
}
|
||||
AllAssetObjects = result.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
List<UnityEngine.Object> result = new List<Object>();
|
||||
foreach (var assetPath in OwnerBundle.MainBundleInfo.IncludeAssetsInEditor)
|
||||
{
|
||||
UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, MainAssetInfo.AssetType);
|
||||
if (mainAsset != null)
|
||||
result.Add(mainAsset);
|
||||
}
|
||||
AllAssetObjects = result.ToArray();
|
||||
}
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (AllAssetObjects == null)
|
||||
{
|
||||
string error;
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null";
|
||||
else
|
||||
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
else
|
||||
{
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c72eb6001f903de46bc72dea0d8b39c5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,93 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class DatabaseAssetProvider : ProviderBase
|
||||
{
|
||||
public DatabaseAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
// 检测资源文件是否存在
|
||||
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
{
|
||||
string error = $"Not found asset : {MainAssetInfo.AssetPath}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.CheckBundle;
|
||||
|
||||
// 注意:模拟异步加载效果提前返回
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Loading;
|
||||
}
|
||||
|
||||
// 2. 加载资源对象
|
||||
if (_steps == ESteps.Loading)
|
||||
{
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(MainAssetInfo.AssetPath);
|
||||
else
|
||||
AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (AssetObject == null)
|
||||
{
|
||||
string error;
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null";
|
||||
else
|
||||
error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
else
|
||||
{
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d4022dd2ea39af5458fb1d61ec997347
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,68 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class DatabaseRawFileProvider : ProviderBase
|
||||
{
|
||||
public DatabaseRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
// 检测资源文件是否存在
|
||||
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
{
|
||||
string error = $"Not found asset : {MainAssetInfo.AssetPath}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.CheckBundle;
|
||||
|
||||
// 注意:模拟异步加载效果提前返回
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
|
||||
// 2. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
RawFilePath = MainAssetInfo.AssetPath;
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 41d0e9bbc5a3a5b4e8b05d60d40d495a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,130 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class DatabaseSceneProvider : ProviderBase
|
||||
{
|
||||
public readonly LoadSceneMode SceneMode;
|
||||
private readonly bool _suspendLoad;
|
||||
private AsyncOperation _asyncOperation;
|
||||
|
||||
public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
SceneMode = sceneMode;
|
||||
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
|
||||
_suspendLoad = suspendLoad;
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
_steps = ESteps.CheckBundle;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Loading;
|
||||
}
|
||||
|
||||
// 2. 加载资源对象
|
||||
if (_steps == ESteps.Loading)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(SceneMode);
|
||||
SceneObject = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
else
|
||||
{
|
||||
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(SceneMode);
|
||||
_asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
|
||||
if (_asyncOperation != null)
|
||||
{
|
||||
_asyncOperation.allowSceneActivation = !_suspendLoad;
|
||||
_asyncOperation.priority = 100;
|
||||
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
else
|
||||
{
|
||||
string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (_asyncOperation != null)
|
||||
{
|
||||
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||
{
|
||||
// 场景加载无法强制异步转同步
|
||||
YooLogger.Error("The scene is loading asyn !");
|
||||
}
|
||||
else
|
||||
{
|
||||
Progress = _asyncOperation.progress;
|
||||
if (_asyncOperation.isDone == false)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (SceneObject.IsValid())
|
||||
{
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
else
|
||||
{
|
||||
string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解除场景加载挂起操作
|
||||
/// </summary>
|
||||
public bool UnSuspendLoad()
|
||||
{
|
||||
if (_asyncOperation == null)
|
||||
return false;
|
||||
|
||||
_asyncOperation.allowSceneActivation = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8252a639423064f498ed22f14912adae
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,104 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class DatabaseSubAssetsProvider : ProviderBase
|
||||
{
|
||||
public DatabaseSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
|
||||
{
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
DebugBeginRecording();
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.None)
|
||||
{
|
||||
// 检测资源文件是否存在
|
||||
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
{
|
||||
string error = $"Not found asset : {MainAssetInfo.AssetPath}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.CheckBundle;
|
||||
|
||||
// 注意:模拟异步加载效果提前返回
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (_steps == ESteps.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
|
||||
{
|
||||
string error = OwnerBundle.LastError;
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
_steps = ESteps.Loading;
|
||||
}
|
||||
|
||||
// 2. 加载资源对象
|
||||
if (_steps == ESteps.Loading)
|
||||
{
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
{
|
||||
AllAssetObjects = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityEngine.Object[] findAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath);
|
||||
List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
|
||||
foreach (var findAsset in findAssets)
|
||||
{
|
||||
if (MainAssetInfo.AssetType.IsAssignableFrom(findAsset.GetType()))
|
||||
result.Add(findAsset);
|
||||
}
|
||||
AllAssetObjects = result.ToArray();
|
||||
}
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (AllAssetObjects == null)
|
||||
{
|
||||
string error;
|
||||
if (MainAssetInfo.AssetType == null)
|
||||
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null";
|
||||
else
|
||||
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(error, EOperationStatus.Failed);
|
||||
}
|
||||
else
|
||||
{
|
||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3837962b901d7ba4abf02a9991ac4c4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,344 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal abstract class ProviderBase : AsyncOperationBase
|
||||
{
|
||||
protected enum ESteps
|
||||
{
|
||||
None = 0,
|
||||
CheckBundle,
|
||||
Loading,
|
||||
Checking,
|
||||
Done,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源提供者唯一标识符
|
||||
/// </summary>
|
||||
public string ProviderGUID { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 所属资源系统
|
||||
/// </summary>
|
||||
public ResourceManager ResourceMgr { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源信息
|
||||
/// </summary>
|
||||
public AssetInfo MainAssetInfo { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的资源对象
|
||||
/// </summary>
|
||||
public UnityEngine.Object AssetObject { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的资源对象集合
|
||||
/// </summary>
|
||||
public UnityEngine.Object[] AllAssetObjects { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的场景对象
|
||||
/// </summary>
|
||||
public UnityEngine.SceneManagement.Scene SceneObject { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 加载的场景名称
|
||||
/// </summary>
|
||||
public string SceneName { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 原生文件路径
|
||||
/// </summary>
|
||||
public string RawFilePath { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数
|
||||
/// </summary>
|
||||
public int RefCount { private set; get; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经销毁
|
||||
/// </summary>
|
||||
public bool IsDestroyed { private set; get; } = false;
|
||||
|
||||
|
||||
protected ESteps _steps = ESteps.None;
|
||||
protected BundleLoaderBase OwnerBundle { private set; get; }
|
||||
protected DependAssetBundles DependBundles { private set; get; }
|
||||
protected bool IsWaitForAsyncComplete { private set; get; } = false;
|
||||
protected bool IsForceDestroyComplete { private set; get; } = false;
|
||||
private readonly List<HandleBase> _handles = new List<HandleBase>();
|
||||
|
||||
|
||||
public ProviderBase(ResourceManager manager, string providerGUID, AssetInfo assetInfo)
|
||||
{
|
||||
ResourceMgr = manager;
|
||||
ProviderGUID = providerGUID;
|
||||
MainAssetInfo = assetInfo;
|
||||
|
||||
// 创建资源包加载器
|
||||
if (manager != null)
|
||||
{
|
||||
OwnerBundle = manager.CreateOwnerAssetBundleLoader(assetInfo);
|
||||
OwnerBundle.Reference();
|
||||
OwnerBundle.AddProvider(this);
|
||||
|
||||
var dependList = manager.CreateDependAssetBundleLoaders(assetInfo);
|
||||
DependBundles = new DependAssetBundles(dependList);
|
||||
DependBundles.Reference();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁资源提供者
|
||||
/// </summary>
|
||||
public void Destroy()
|
||||
{
|
||||
IsDestroyed = true;
|
||||
|
||||
// 检测是否为正常销毁
|
||||
if (IsDone == false)
|
||||
{
|
||||
Error = "User abort !";
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
|
||||
// 释放资源包加载器
|
||||
if (OwnerBundle != null)
|
||||
{
|
||||
OwnerBundle.Release();
|
||||
OwnerBundle = null;
|
||||
}
|
||||
if (DependBundles != null)
|
||||
{
|
||||
DependBundles.Release();
|
||||
DependBundles = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以销毁
|
||||
/// </summary>
|
||||
public bool CanDestroy()
|
||||
{
|
||||
// 注意:在进行资源加载过程时不可以销毁
|
||||
if (_steps == ESteps.Loading || _steps == ESteps.Checking)
|
||||
return false;
|
||||
|
||||
return RefCount <= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建资源句柄
|
||||
/// </summary>
|
||||
public T CreateHandle<T>() where T : HandleBase
|
||||
{
|
||||
// 引用计数增加
|
||||
RefCount++;
|
||||
|
||||
HandleBase handle;
|
||||
if (typeof(T) == typeof(AssetHandle))
|
||||
handle = new AssetHandle(this);
|
||||
else if (typeof(T) == typeof(SceneHandle))
|
||||
handle = new SceneHandle(this);
|
||||
else if (typeof(T) == typeof(SubAssetsHandle))
|
||||
handle = new SubAssetsHandle(this);
|
||||
else if (typeof(T) == typeof(AllAssetsHandle))
|
||||
handle = new AllAssetsHandle(this);
|
||||
else if (typeof(T) == typeof(RawFileHandle))
|
||||
handle = new RawFileHandle(this);
|
||||
else
|
||||
throw new System.NotImplementedException();
|
||||
|
||||
_handles.Add(handle);
|
||||
return handle as T;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void ReleaseHandle(HandleBase handle)
|
||||
{
|
||||
if (RefCount <= 0)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
if (_handles.Remove(handle) == false)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
// 引用计数减少
|
||||
RefCount--;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放所有资源句柄
|
||||
/// </summary>
|
||||
public void ReleaseAllHandles()
|
||||
{
|
||||
for (int i = _handles.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var handle = _handles[i];
|
||||
handle.ReleaseInternal();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
IsWaitForAsyncComplete = true;
|
||||
|
||||
// 注意:主动轮询更新完成同步加载
|
||||
InternalOnUpdate();
|
||||
|
||||
// 验证结果
|
||||
if (IsDone == false)
|
||||
{
|
||||
YooLogger.Warning($"{nameof(WaitForAsyncComplete)} failed to loading : {MainAssetInfo.AssetPath}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 强制销毁资源提供者
|
||||
/// </summary>
|
||||
public void ForceDestroyComplete()
|
||||
{
|
||||
IsForceDestroyComplete = true;
|
||||
|
||||
// 注意:主动轮询更新完成同步加载
|
||||
// 说明:如果资源包未准备完毕也可以放心销毁。
|
||||
InternalOnUpdate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理特殊异常
|
||||
/// </summary>
|
||||
protected void ProcessCacheBundleException()
|
||||
{
|
||||
if (OwnerBundle.IsDestroyed)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
string error = $"The bundle {OwnerBundle.MainBundleInfo.Bundle.BundleName} has been destroyed by unity bugs !";
|
||||
YooLogger.Error(error);
|
||||
InvokeCompletion(Error, EOperationStatus.Failed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 结束流程
|
||||
/// </summary>
|
||||
protected void InvokeCompletion(string error, EOperationStatus status)
|
||||
{
|
||||
DebugEndRecording();
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Error = error;
|
||||
Status = status;
|
||||
|
||||
// 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。
|
||||
// 注意:回调方法如果发生异常,会阻断列表里的后续回调方法!
|
||||
List<HandleBase> tempers = new List<HandleBase>(_handles);
|
||||
foreach (var hande in tempers)
|
||||
{
|
||||
if (hande.IsValid)
|
||||
{
|
||||
hande.InvokeCallback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region 调试信息相关
|
||||
/// <summary>
|
||||
/// 出生的场景
|
||||
/// </summary>
|
||||
public string SpawnScene = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 出生的时间
|
||||
/// </summary>
|
||||
public string SpawnTime = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 加载耗时(单位:毫秒)
|
||||
/// </summary>
|
||||
public long LoadingTime { protected set; get; }
|
||||
|
||||
// 加载耗时统计
|
||||
private Stopwatch _watch = null;
|
||||
|
||||
[Conditional("DEBUG")]
|
||||
public void InitSpawnDebugInfo()
|
||||
{
|
||||
SpawnScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; ;
|
||||
SpawnTime = SpawnTimeToString(UnityEngine.Time.realtimeSinceStartup);
|
||||
}
|
||||
private string SpawnTimeToString(float spawnTime)
|
||||
{
|
||||
float h = UnityEngine.Mathf.FloorToInt(spawnTime / 3600f);
|
||||
float m = UnityEngine.Mathf.FloorToInt(spawnTime / 60f - h * 60f);
|
||||
float s = UnityEngine.Mathf.FloorToInt(spawnTime - m * 60f - h * 3600f);
|
||||
return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
|
||||
}
|
||||
|
||||
[Conditional("DEBUG")]
|
||||
protected void DebugBeginRecording()
|
||||
{
|
||||
if (_watch == null)
|
||||
{
|
||||
_watch = Stopwatch.StartNew();
|
||||
}
|
||||
}
|
||||
|
||||
[Conditional("DEBUG")]
|
||||
private void DebugEndRecording()
|
||||
{
|
||||
if (_watch != null)
|
||||
{
|
||||
LoadingTime = _watch.ElapsedMilliseconds;
|
||||
_watch = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载报告
|
||||
/// </summary>
|
||||
internal DownloadStatus GetDownloadStatus()
|
||||
{
|
||||
DownloadStatus status = new DownloadStatus();
|
||||
status.TotalBytes = (ulong)OwnerBundle.MainBundleInfo.Bundle.FileSize;
|
||||
status.DownloadedBytes = OwnerBundle.DownloadedBytes;
|
||||
foreach (var dependBundle in DependBundles.DependList)
|
||||
{
|
||||
status.TotalBytes += (ulong)dependBundle.MainBundleInfo.Bundle.FileSize;
|
||||
status.DownloadedBytes += dependBundle.DownloadedBytes;
|
||||
}
|
||||
|
||||
if (status.TotalBytes == 0)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
status.IsDone = status.DownloadedBytes == status.TotalBytes;
|
||||
status.Progress = (float)status.DownloadedBytes / status.TotalBytes;
|
||||
return status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源包的调试信息列表
|
||||
/// </summary>
|
||||
internal void GetBundleDebugInfos(List<DebugBundleInfo> output)
|
||||
{
|
||||
var bundleInfo = new DebugBundleInfo();
|
||||
bundleInfo.BundleName = OwnerBundle.MainBundleInfo.Bundle.BundleName;
|
||||
bundleInfo.RefCount = OwnerBundle.RefCount;
|
||||
bundleInfo.Status = OwnerBundle.Status.ToString();
|
||||
output.Add(bundleInfo);
|
||||
|
||||
DependBundles.GetBundleDebugInfos(output);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02cc2ce3da5a88b48942ad96e6061aad
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,103 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class ResourceLoader
|
||||
{
|
||||
private IDecryptionServices _decryption;
|
||||
private IDeliveryLoadServices _delivery;
|
||||
|
||||
public void Init(IDecryptionServices decryption, IDeliveryLoadServices delivery)
|
||||
{
|
||||
_decryption = decryption;
|
||||
_delivery = delivery;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载资源包对象
|
||||
/// </summary>
|
||||
public AssetBundle LoadAssetBundle(BundleInfo bundleInfo, string fileLoadPath, out Stream managedStream)
|
||||
{
|
||||
managedStream = null;
|
||||
if (bundleInfo.Bundle.Encrypted)
|
||||
{
|
||||
if (_decryption == null)
|
||||
{
|
||||
YooLogger.Error($"{nameof(IDecryptionServices)} is null ! when load asset bundle {bundleInfo.Bundle.BundleName}!");
|
||||
return null;
|
||||
}
|
||||
|
||||
DecryptFileInfo fileInfo = new DecryptFileInfo();
|
||||
fileInfo.BundleName = bundleInfo.Bundle.BundleName;
|
||||
fileInfo.FileLoadPath = fileLoadPath;
|
||||
fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC;
|
||||
return _decryption.LoadAssetBundle(fileInfo, out managedStream);
|
||||
}
|
||||
else
|
||||
{
|
||||
return AssetBundle.LoadFromFile(fileLoadPath);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载资源包对象
|
||||
/// </summary>
|
||||
public AssetBundleCreateRequest LoadAssetBundleAsync(BundleInfo bundleInfo, string fileLoadPath, out Stream managedStream)
|
||||
{
|
||||
managedStream = null;
|
||||
if (bundleInfo.Bundle.Encrypted)
|
||||
{
|
||||
if (_decryption == null)
|
||||
{
|
||||
YooLogger.Error($"{nameof(IDecryptionServices)} is null ! when load asset bundle {bundleInfo.Bundle.BundleName}!");
|
||||
return null;
|
||||
}
|
||||
|
||||
DecryptFileInfo fileInfo = new DecryptFileInfo();
|
||||
fileInfo.BundleName = bundleInfo.Bundle.BundleName;
|
||||
fileInfo.FileLoadPath = fileLoadPath;
|
||||
fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC;
|
||||
return _decryption.LoadAssetBundleAsync(fileInfo, out managedStream);
|
||||
}
|
||||
else
|
||||
{
|
||||
return AssetBundle.LoadFromFileAsync(fileLoadPath);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载分发的资源包对象
|
||||
/// </summary>
|
||||
public AssetBundle LoadDeliveryAssetBundle(BundleInfo bundleInfo, string fileLoadPath)
|
||||
{
|
||||
if (_delivery == null)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
// 注意:对于已经加密的资源包,需要开发者自行解密。
|
||||
DeliveryFileInfo fileInfo = new DeliveryFileInfo();
|
||||
fileInfo.BundleName = bundleInfo.Bundle.BundleName;
|
||||
fileInfo.FileLoadPath = fileLoadPath;
|
||||
fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC;
|
||||
fileInfo.Encrypted = bundleInfo.Bundle.Encrypted;
|
||||
return _delivery.LoadAssetBundle(fileInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载分发的资源包对象
|
||||
/// </summary>
|
||||
public AssetBundleCreateRequest LoadDeliveryAssetBundleAsync(BundleInfo bundleInfo, string fileLoadPath)
|
||||
{
|
||||
if (_delivery == null)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
// 注意:对于已经加密的资源包,需要开发者自行解密。
|
||||
DeliveryFileInfo fileInfo = new DeliveryFileInfo();
|
||||
fileInfo.BundleName = bundleInfo.Bundle.BundleName;
|
||||
fileInfo.FileLoadPath = fileLoadPath;
|
||||
fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC;
|
||||
fileInfo.Encrypted = bundleInfo.Bundle.Encrypted;
|
||||
return _delivery.LoadAssetBundleAsync(fileInfo);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3818e5adef4eaea4a85b112f953c4f7b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,484 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class ResourceManager
|
||||
{
|
||||
// 全局场景句柄集合
|
||||
private readonly static Dictionary<string, SceneHandle> _sceneHandles = new Dictionary<string, SceneHandle>(100);
|
||||
private static long _sceneCreateCount = 0;
|
||||
|
||||
private readonly Dictionary<string, ProviderBase> _providerDic = new Dictionary<string, ProviderBase>(5000);
|
||||
private readonly Dictionary<string, BundleLoaderBase> _loaderDic = new Dictionary<string, BundleLoaderBase>(5000);
|
||||
private readonly List<BundleLoaderBase> _loaderList = new List<BundleLoaderBase>(5000);
|
||||
|
||||
private bool _simulationOnEditor;
|
||||
private bool _autoDestroyAssetProvider;
|
||||
private IBundleQuery _bundleQuery;
|
||||
|
||||
/// <summary>
|
||||
/// 所属包裹
|
||||
/// </summary>
|
||||
public readonly string PackageName;
|
||||
|
||||
|
||||
public ResourceManager(string packageName)
|
||||
{
|
||||
PackageName = packageName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
public void Initialize(bool simulationOnEditor, bool autoDestroyAssetProvider, IBundleQuery bundleServices)
|
||||
{
|
||||
_simulationOnEditor = simulationOnEditor;
|
||||
_autoDestroyAssetProvider = autoDestroyAssetProvider;
|
||||
_bundleQuery = bundleServices;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新
|
||||
/// </summary>
|
||||
public void Update()
|
||||
{
|
||||
foreach (var loader in _loaderList)
|
||||
{
|
||||
loader.Update();
|
||||
|
||||
if (_autoDestroyAssetProvider)
|
||||
loader.TryDestroyProviders();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源回收(卸载引用计数为零的资源)
|
||||
/// </summary>
|
||||
public void UnloadUnusedAssets()
|
||||
{
|
||||
for (int i = _loaderList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
BundleLoaderBase loader = _loaderList[i];
|
||||
loader.TryDestroyProviders();
|
||||
}
|
||||
|
||||
for (int i = _loaderList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
BundleLoaderBase loader = _loaderList[i];
|
||||
if (loader.CanDestroy())
|
||||
{
|
||||
string bundleName = loader.MainBundleInfo.Bundle.BundleName;
|
||||
loader.Destroy();
|
||||
_loaderList.RemoveAt(i);
|
||||
_loaderDic.Remove(bundleName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试卸载指定资源的资源包(包括依赖资源)
|
||||
/// </summary>
|
||||
public void TryUnloadUnusedAsset(AssetInfo assetInfo)
|
||||
{
|
||||
if (assetInfo.IsInvalid)
|
||||
{
|
||||
YooLogger.Error($"Failed to unload asset ! {assetInfo.Error}");
|
||||
return;
|
||||
}
|
||||
|
||||
// 卸载主资源包加载器
|
||||
string manBundleName = _bundleQuery.GetMainBundleName(assetInfo);
|
||||
var mainLoader = TryGetAssetBundleLoader(manBundleName);
|
||||
if (mainLoader != null)
|
||||
{
|
||||
mainLoader.TryDestroyProviders();
|
||||
if (mainLoader.CanDestroy())
|
||||
{
|
||||
string bundleName = mainLoader.MainBundleInfo.Bundle.BundleName;
|
||||
mainLoader.Destroy();
|
||||
_loaderList.Remove(mainLoader);
|
||||
_loaderDic.Remove(bundleName);
|
||||
}
|
||||
}
|
||||
|
||||
// 卸载依赖资源包加载器
|
||||
string[] dependBundleNames = _bundleQuery.GetDependBundleNames(assetInfo);
|
||||
foreach (var dependBundleName in dependBundleNames)
|
||||
{
|
||||
var dependLoader = TryGetAssetBundleLoader(dependBundleName);
|
||||
if (dependLoader != null)
|
||||
{
|
||||
if (dependLoader.CanDestroy())
|
||||
{
|
||||
string bundleName = dependLoader.MainBundleInfo.Bundle.BundleName;
|
||||
dependLoader.Destroy();
|
||||
_loaderList.Remove(dependLoader);
|
||||
_loaderDic.Remove(bundleName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 强制回收所有资源
|
||||
/// 注意:加载器在销毁后关联的下载器还会继续下载!
|
||||
/// </summary>
|
||||
public void ForceUnloadAllAssets()
|
||||
{
|
||||
#if UNITY_WEBGL
|
||||
throw new Exception($"WebGL not support invoke {nameof(ForceUnloadAllAssets)}");
|
||||
#else
|
||||
// 注意:因为场景无法异步转同步,需要等待所有场景加载完毕!
|
||||
foreach (var sceneHandlePair in _sceneHandles)
|
||||
{
|
||||
var sceneHandle = sceneHandlePair.Value;
|
||||
if (sceneHandle.PackageName == PackageName)
|
||||
{
|
||||
if (sceneHandle.IsDone == false)
|
||||
throw new Exception($"{nameof(ForceUnloadAllAssets)} cannot be called when loading the scene !");
|
||||
}
|
||||
}
|
||||
|
||||
// 释放所有资源句柄
|
||||
foreach (var provider in _providerDic.Values)
|
||||
{
|
||||
provider.ReleaseAllHandles();
|
||||
}
|
||||
|
||||
// 强制销毁资源提供者
|
||||
foreach (var provider in _providerDic.Values)
|
||||
{
|
||||
provider.ForceDestroyComplete();
|
||||
provider.Destroy();
|
||||
}
|
||||
|
||||
// 强制销毁资源加载器
|
||||
foreach (var loader in _loaderList)
|
||||
{
|
||||
loader.ForceDestroyComplete();
|
||||
loader.Destroy();
|
||||
}
|
||||
|
||||
// 清空数据
|
||||
_providerDic.Clear();
|
||||
_loaderList.Clear();
|
||||
_loaderDic.Clear();
|
||||
ClearSceneHandle();
|
||||
|
||||
// 注意:调用底层接口释放所有资源
|
||||
Resources.UnloadUnusedAssets();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载场景对象
|
||||
/// 注意:返回的场景句柄是唯一的,每个场景句柄对应自己的场景提供者对象。
|
||||
/// 注意:业务逻辑层应该避免同时加载一个子场景。
|
||||
/// </summary>
|
||||
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad, uint priority)
|
||||
{
|
||||
if (assetInfo.IsInvalid)
|
||||
{
|
||||
YooLogger.Error($"Failed to load scene ! {assetInfo.Error}");
|
||||
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
|
||||
completedProvider.SetCompleted(assetInfo.Error);
|
||||
return completedProvider.CreateHandle<SceneHandle>();
|
||||
}
|
||||
|
||||
// 如果加载的是主场景,则卸载所有缓存的场景
|
||||
if (sceneMode == LoadSceneMode.Single)
|
||||
{
|
||||
UnloadAllScene();
|
||||
}
|
||||
|
||||
// 注意:同一个场景的ProviderGUID每次加载都会变化
|
||||
string providerGUID = $"{assetInfo.GUID}-{++_sceneCreateCount}";
|
||||
ProviderBase provider;
|
||||
{
|
||||
if (_simulationOnEditor)
|
||||
provider = new DatabaseSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad);
|
||||
else
|
||||
provider = new BundledSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad);
|
||||
provider.InitSpawnDebugInfo();
|
||||
_providerDic.Add(providerGUID, provider);
|
||||
OperationSystem.StartOperation(PackageName, provider);
|
||||
}
|
||||
|
||||
provider.Priority = priority;
|
||||
var handle = provider.CreateHandle<SceneHandle>();
|
||||
handle.PackageName = PackageName;
|
||||
_sceneHandles.Add(providerGUID, handle);
|
||||
return handle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载资源对象
|
||||
/// </summary>
|
||||
public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority)
|
||||
{
|
||||
if (assetInfo.IsInvalid)
|
||||
{
|
||||
YooLogger.Error($"Failed to load asset ! {assetInfo.Error}");
|
||||
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
|
||||
completedProvider.SetCompleted(assetInfo.Error);
|
||||
return completedProvider.CreateHandle<AssetHandle>();
|
||||
}
|
||||
|
||||
string providerGUID = nameof(LoadAssetAsync) + assetInfo.GUID;
|
||||
ProviderBase provider = TryGetProvider(providerGUID);
|
||||
if (provider == null)
|
||||
{
|
||||
if (_simulationOnEditor)
|
||||
provider = new DatabaseAssetProvider(this, providerGUID, assetInfo);
|
||||
else
|
||||
provider = new BundledAssetProvider(this, providerGUID, assetInfo);
|
||||
provider.InitSpawnDebugInfo();
|
||||
_providerDic.Add(providerGUID, provider);
|
||||
OperationSystem.StartOperation(PackageName, provider);
|
||||
}
|
||||
|
||||
provider.Priority = priority;
|
||||
return provider.CreateHandle<AssetHandle>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载子资源对象
|
||||
/// </summary>
|
||||
public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority)
|
||||
{
|
||||
if (assetInfo.IsInvalid)
|
||||
{
|
||||
YooLogger.Error($"Failed to load sub assets ! {assetInfo.Error}");
|
||||
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
|
||||
completedProvider.SetCompleted(assetInfo.Error);
|
||||
return completedProvider.CreateHandle<SubAssetsHandle>();
|
||||
}
|
||||
|
||||
string providerGUID = nameof(LoadSubAssetsAsync) + assetInfo.GUID;
|
||||
ProviderBase provider = TryGetProvider(providerGUID);
|
||||
if (provider == null)
|
||||
{
|
||||
if (_simulationOnEditor)
|
||||
provider = new DatabaseSubAssetsProvider(this, providerGUID, assetInfo);
|
||||
else
|
||||
provider = new BundledSubAssetsProvider(this, providerGUID, assetInfo);
|
||||
provider.InitSpawnDebugInfo();
|
||||
_providerDic.Add(providerGUID, provider);
|
||||
OperationSystem.StartOperation(PackageName, provider);
|
||||
}
|
||||
|
||||
provider.Priority = priority;
|
||||
return provider.CreateHandle<SubAssetsHandle>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载所有资源对象
|
||||
/// </summary>
|
||||
public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority)
|
||||
{
|
||||
if (assetInfo.IsInvalid)
|
||||
{
|
||||
YooLogger.Error($"Failed to load all assets ! {assetInfo.Error}");
|
||||
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
|
||||
completedProvider.SetCompleted(assetInfo.Error);
|
||||
return completedProvider.CreateHandle<AllAssetsHandle>();
|
||||
}
|
||||
|
||||
string providerGUID = nameof(LoadAllAssetsAsync) + assetInfo.GUID;
|
||||
ProviderBase provider = TryGetProvider(providerGUID);
|
||||
if (provider == null)
|
||||
{
|
||||
if (_simulationOnEditor)
|
||||
provider = new DatabaseAllAssetsProvider(this, providerGUID, assetInfo);
|
||||
else
|
||||
provider = new BundledAllAssetsProvider(this, providerGUID, assetInfo);
|
||||
provider.InitSpawnDebugInfo();
|
||||
_providerDic.Add(providerGUID, provider);
|
||||
OperationSystem.StartOperation(PackageName, provider);
|
||||
}
|
||||
|
||||
provider.Priority = priority;
|
||||
return provider.CreateHandle<AllAssetsHandle>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载原生文件
|
||||
/// </summary>
|
||||
public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority)
|
||||
{
|
||||
if (assetInfo.IsInvalid)
|
||||
{
|
||||
YooLogger.Error($"Failed to load raw file ! {assetInfo.Error}");
|
||||
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
|
||||
completedProvider.SetCompleted(assetInfo.Error);
|
||||
return completedProvider.CreateHandle<RawFileHandle>();
|
||||
}
|
||||
|
||||
string providerGUID = nameof(LoadRawFileAsync) + assetInfo.GUID;
|
||||
ProviderBase provider = TryGetProvider(providerGUID);
|
||||
if (provider == null)
|
||||
{
|
||||
if (_simulationOnEditor)
|
||||
provider = new DatabaseRawFileProvider(this, providerGUID, assetInfo);
|
||||
else
|
||||
provider = new BundledRawFileProvider(this, providerGUID, assetInfo);
|
||||
provider.InitSpawnDebugInfo();
|
||||
_providerDic.Add(providerGUID, provider);
|
||||
OperationSystem.StartOperation(PackageName, provider);
|
||||
}
|
||||
|
||||
provider.Priority = priority;
|
||||
return provider.CreateHandle<RawFileHandle>();
|
||||
}
|
||||
|
||||
internal void UnloadSubScene(string sceneName)
|
||||
{
|
||||
List<string> removeKeys = new List<string>();
|
||||
foreach (var valuePair in _sceneHandles)
|
||||
{
|
||||
var sceneHandle = valuePair.Value;
|
||||
if (sceneHandle.SceneName == sceneName)
|
||||
{
|
||||
// 释放子场景句柄
|
||||
sceneHandle.ReleaseInternal();
|
||||
removeKeys.Add(valuePair.Key);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (string key in removeKeys)
|
||||
{
|
||||
_sceneHandles.Remove(key);
|
||||
}
|
||||
}
|
||||
private void UnloadAllScene()
|
||||
{
|
||||
// 释放所有场景句柄
|
||||
foreach (var valuePair in _sceneHandles)
|
||||
{
|
||||
valuePair.Value.ReleaseInternal();
|
||||
}
|
||||
_sceneHandles.Clear();
|
||||
}
|
||||
private void ClearSceneHandle()
|
||||
{
|
||||
// 释放资源包下的所有场景
|
||||
if (_bundleQuery.ManifestValid())
|
||||
{
|
||||
string packageName = PackageName;
|
||||
List<string> removeList = new List<string>();
|
||||
foreach (var valuePair in _sceneHandles)
|
||||
{
|
||||
if (valuePair.Value.PackageName == packageName)
|
||||
{
|
||||
removeList.Add(valuePair.Key);
|
||||
}
|
||||
}
|
||||
foreach (var key in removeList)
|
||||
{
|
||||
_sceneHandles.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal BundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo)
|
||||
{
|
||||
BundleInfo bundleInfo = _bundleQuery.GetMainBundleInfo(assetInfo);
|
||||
return CreateAssetBundleLoaderInternal(bundleInfo);
|
||||
}
|
||||
internal List<BundleLoaderBase> CreateDependAssetBundleLoaders(AssetInfo assetInfo)
|
||||
{
|
||||
BundleInfo[] depends = _bundleQuery.GetDependBundleInfos(assetInfo);
|
||||
List<BundleLoaderBase> result = new List<BundleLoaderBase>(depends.Length);
|
||||
foreach (var bundleInfo in depends)
|
||||
{
|
||||
BundleLoaderBase dependLoader = CreateAssetBundleLoaderInternal(bundleInfo);
|
||||
result.Add(dependLoader);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
internal void RemoveBundleProviders(List<ProviderBase> removeList)
|
||||
{
|
||||
foreach (var provider in removeList)
|
||||
{
|
||||
_providerDic.Remove(provider.ProviderGUID);
|
||||
}
|
||||
}
|
||||
internal bool HasAnyLoader()
|
||||
{
|
||||
return _loaderList.Count > 0;
|
||||
}
|
||||
|
||||
private BundleLoaderBase CreateAssetBundleLoaderInternal(BundleInfo bundleInfo)
|
||||
{
|
||||
// 如果加载器已经存在
|
||||
string bundleName = bundleInfo.Bundle.BundleName;
|
||||
BundleLoaderBase loader = TryGetAssetBundleLoader(bundleName);
|
||||
if (loader != null)
|
||||
return loader;
|
||||
|
||||
// 新增下载需求
|
||||
if (_simulationOnEditor)
|
||||
{
|
||||
loader = new VirtualBundleFileLoader(this, bundleInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
#if UNITY_WEBGL
|
||||
if (bundleInfo.Bundle.Buildpipeline== EDefaultBuildPipeline.RawFileBuildPipeline.ToString())
|
||||
loader = new RawBundleWebLoader(this, bundleInfo);
|
||||
else
|
||||
loader = new AssetBundleWebLoader(this, bundleInfo);
|
||||
#else
|
||||
if (bundleInfo.Bundle.Buildpipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString())
|
||||
loader = new RawBundleFileLoader(this, bundleInfo);
|
||||
else
|
||||
loader = new AssetBundleFileLoader(this, bundleInfo);
|
||||
#endif
|
||||
}
|
||||
|
||||
_loaderList.Add(loader);
|
||||
_loaderDic.Add(bundleName, loader);
|
||||
return loader;
|
||||
}
|
||||
private BundleLoaderBase TryGetAssetBundleLoader(string bundleName)
|
||||
{
|
||||
if (_loaderDic.TryGetValue(bundleName, out BundleLoaderBase value))
|
||||
return value;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
private ProviderBase TryGetProvider(string providerGUID)
|
||||
{
|
||||
if (_providerDic.TryGetValue(providerGUID, out ProviderBase value))
|
||||
return value;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
#region 调试信息
|
||||
internal List<DebugProviderInfo> GetDebugReportInfos()
|
||||
{
|
||||
List<DebugProviderInfo> result = new List<DebugProviderInfo>(_providerDic.Count);
|
||||
foreach (var provider in _providerDic.Values)
|
||||
{
|
||||
DebugProviderInfo providerInfo = new DebugProviderInfo();
|
||||
providerInfo.AssetPath = provider.MainAssetInfo.AssetPath;
|
||||
providerInfo.SpawnScene = provider.SpawnScene;
|
||||
providerInfo.SpawnTime = provider.SpawnTime;
|
||||
providerInfo.LoadingTime = provider.LoadingTime;
|
||||
providerInfo.RefCount = provider.RefCount;
|
||||
providerInfo.Status = provider.Status.ToString();
|
||||
providerInfo.DependBundleInfos = new List<DebugBundleInfo>();
|
||||
provider.GetBundleDebugInfos(providerInfo.DependBundleInfos);
|
||||
result.Add(providerInfo);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ecc5913758e8234fbba5ef2fd192f86
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user