NBC修改
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e23bf04f44304300ad16382736556ec2
|
||||
timeCreated: 1677058068
|
||||
@@ -1,56 +0,0 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal class AssetLoadFromDatabase : IAssetLoader
|
||||
{
|
||||
public static IAssetLoader CreateInstance()
|
||||
{
|
||||
return new AssetLoadFromDatabase();
|
||||
}
|
||||
|
||||
private AssetProvider _provider;
|
||||
|
||||
public void Start(AssetProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var assetInfo = _provider.AssetInfo;
|
||||
var path = assetInfo.Path;
|
||||
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(path);
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
{
|
||||
_provider.SetStatus(NTaskStatus.Success, $"Not found asset : {path}");
|
||||
return;
|
||||
}
|
||||
|
||||
UnityEngine.Object obj;
|
||||
if (assetInfo.AssetType == null)
|
||||
obj = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetInfo.Path);
|
||||
else
|
||||
obj = UnityEditor.AssetDatabase.LoadAssetAtPath(assetInfo.Path, assetInfo.AssetType);
|
||||
|
||||
if (obj == null)
|
||||
{
|
||||
_provider.SetStatus(NTaskStatus.Fail);
|
||||
}
|
||||
else
|
||||
{
|
||||
_provider.Asset = obj;
|
||||
_provider.SetStatus(NTaskStatus.Success);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
Update();
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73a25ea4518949d28d090a974722dbee
|
||||
timeCreated: 1678286041
|
||||
@@ -1,144 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal class AssetLoaderFromBundle : IAssetLoader
|
||||
{
|
||||
public static IAssetLoader CreateInstance()
|
||||
{
|
||||
return new AssetLoaderFromBundle();
|
||||
}
|
||||
|
||||
private enum Steps
|
||||
{
|
||||
LoadDependency,
|
||||
LoadAsset,
|
||||
}
|
||||
|
||||
private AssetProvider _provider;
|
||||
private AssetInfo _assetInfo;
|
||||
|
||||
private AssetBundleRequest _cacheRequest;
|
||||
private Dependency _dependency;
|
||||
private Steps _steps = Steps.LoadDependency;
|
||||
private bool _isWaitForAsyncComplete;
|
||||
|
||||
private AssetBundleRequest _assetBundleRequest;
|
||||
public AssetBundle _assetBundle;
|
||||
|
||||
public void Start(AssetProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
_assetInfo = provider.AssetInfo;
|
||||
|
||||
_dependency = Dependency.GetAssetDependency(_assetInfo);
|
||||
_dependency.Retain();
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (_steps == Steps.LoadDependency)
|
||||
{
|
||||
if (_isWaitForAsyncComplete)
|
||||
{
|
||||
_dependency.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (!_dependency.IsDone) return;
|
||||
|
||||
if (!_dependency.IsSucceed)
|
||||
{
|
||||
Debug.LogError("error");
|
||||
SetStatus("dependency fail");
|
||||
}
|
||||
|
||||
var provider = _dependency.GetMainBundledProvider();
|
||||
if (provider != null && provider.AssetBundle != null)
|
||||
{
|
||||
_assetBundle = provider.AssetBundle;
|
||||
|
||||
if (_isWaitForAsyncComplete)
|
||||
{
|
||||
if (_provider.IsAll)
|
||||
{
|
||||
SetStatus(_assetBundle.LoadAssetWithSubAssets(_assetInfo.Path, _assetInfo.AssetType));
|
||||
}
|
||||
else
|
||||
{
|
||||
SetStatus(_assetBundle.LoadAsset(_assetInfo.Path, _assetInfo.AssetType));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_assetBundleRequest = _provider.IsAll
|
||||
? _assetBundle.LoadAssetWithSubAssetsAsync(_assetInfo.Path, _assetInfo.AssetType)
|
||||
: _assetBundle.LoadAssetAsync(_assetInfo.Path, _assetInfo.AssetType);
|
||||
_steps = Steps.LoadAsset;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//失败,后续补全失败逻辑
|
||||
Debug.LogError("error1");
|
||||
SetStatus("error");
|
||||
}
|
||||
}
|
||||
else if (_steps == Steps.LoadAsset)
|
||||
{
|
||||
if (!_assetBundleRequest.isDone) return;
|
||||
if (_provider.IsAll)
|
||||
{
|
||||
SetStatus(_assetBundleRequest.allAssets);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetStatus(_assetBundleRequest.asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
_isWaitForAsyncComplete = true;
|
||||
|
||||
int frame = 1000;
|
||||
while (true)
|
||||
{
|
||||
frame--;
|
||||
if (frame == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Update();
|
||||
|
||||
if (_provider.IsDone)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
_dependency.Release();
|
||||
_assetBundleRequest = null;
|
||||
}
|
||||
|
||||
|
||||
private void SetStatus(Object asset)
|
||||
{
|
||||
_provider.Asset = asset;
|
||||
_provider.SetStatus(NTaskStatus.Success);
|
||||
}
|
||||
|
||||
private void SetStatus(Object[] allAsset)
|
||||
{
|
||||
_provider.AllAsset = allAsset;
|
||||
_provider.SetStatus(NTaskStatus.Success);
|
||||
}
|
||||
|
||||
private void SetStatus(string error)
|
||||
{
|
||||
_provider.SetStatus(NTaskStatus.Fail, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4f15a2f6e208403e8e55d45dc7e01fd1
|
||||
timeCreated: 1677551825
|
||||
@@ -1,113 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal class BundleLoaderFromDownload : IBundleLoader
|
||||
{
|
||||
private enum Steps
|
||||
{
|
||||
Download,
|
||||
Load,
|
||||
Check,
|
||||
Done,
|
||||
}
|
||||
|
||||
private BundledProvider _provider;
|
||||
private Steps _steps = Steps.Download;
|
||||
private AssetBundleCreateRequest _createRequest;
|
||||
private string _downloadPath;
|
||||
private string _loadPath;
|
||||
private bool _isWaitForAsyncComplete;
|
||||
private DownloadFileTask _downloadFileTask;
|
||||
|
||||
public AssetBundle Bundle { set; get; }
|
||||
|
||||
public void Start(BundledProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
var bundleData = provider.BundleInfo.Bundle;
|
||||
_downloadPath = bundleData.RemoteDataFilePath;
|
||||
_loadPath = bundleData.CachedDataFilePath;
|
||||
_downloadFileTask = new DownloadFileTask(_downloadPath, bundleData.CachedDataFilePath);
|
||||
_downloadFileTask.Run();
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (_steps == Steps.Download)
|
||||
{
|
||||
if (!_downloadFileTask.IsDone) return;
|
||||
Addressable.UpdateBundleInfo(_provider.BundleInfo.Bundle.Name);
|
||||
_steps = Steps.Load;
|
||||
}
|
||||
|
||||
if (_steps == Steps.Load)
|
||||
{
|
||||
if (_isWaitForAsyncComplete)
|
||||
Bundle = AssetBundle.LoadFromFile(_loadPath);
|
||||
else
|
||||
_createRequest = AssetBundle.LoadFromFileAsync(_loadPath);
|
||||
_steps = Steps.Check;
|
||||
}
|
||||
|
||||
if (_steps == Steps.Check)
|
||||
{
|
||||
if (_createRequest != null)
|
||||
{
|
||||
if (_isWaitForAsyncComplete)
|
||||
{
|
||||
Bundle = _createRequest.assetBundle;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_createRequest.isDone)
|
||||
return;
|
||||
Bundle = _createRequest.assetBundle;
|
||||
}
|
||||
}
|
||||
|
||||
if (Bundle == null)
|
||||
{
|
||||
_steps = Steps.Done;
|
||||
_provider.SetStatus(NTaskStatus.Fail,
|
||||
$"failed load assetBundle : {_provider.BundleInfo.Bundle.Name}");
|
||||
Debug.LogError(_provider.ErrorMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
_provider.SetStatus(NTaskStatus.Success);
|
||||
_provider.AssetBundle = Bundle;
|
||||
_steps = Steps.Done;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
_isWaitForAsyncComplete = true;
|
||||
|
||||
int frame = 1000;
|
||||
while (true)
|
||||
{
|
||||
// 保险机制
|
||||
frame--;
|
||||
if (frame == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Update();
|
||||
_downloadFileTask.Process();
|
||||
|
||||
if (_provider.IsDone)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
_createRequest = null;
|
||||
_downloadFileTask = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b2dee1496d7247acb00dbb7af88a016c
|
||||
timeCreated: 1677558190
|
||||
@@ -1,98 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal class BundleLoaderFromLocal : IBundleLoader
|
||||
{
|
||||
private enum Steps
|
||||
{
|
||||
Load,
|
||||
Check,
|
||||
Done,
|
||||
}
|
||||
|
||||
private BundledProvider _provider;
|
||||
private Steps _steps = Steps.Load;
|
||||
private AssetBundleCreateRequest _createRequest;
|
||||
private string _loadPath;
|
||||
private bool _isWaitForAsyncComplete;
|
||||
|
||||
public AssetBundle Bundle { set; get; }
|
||||
|
||||
public void Start(BundledProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
var info = provider.BundleInfo;
|
||||
_loadPath = info.BundlePath;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (_steps == Steps.Load)
|
||||
{
|
||||
if (_isWaitForAsyncComplete)
|
||||
Bundle = AssetBundle.LoadFromFile(_loadPath);
|
||||
else
|
||||
_createRequest = AssetBundle.LoadFromFileAsync(_loadPath);
|
||||
_steps = Steps.Check;
|
||||
}
|
||||
|
||||
if (_steps == Steps.Check)
|
||||
{
|
||||
if (_createRequest != null)
|
||||
{
|
||||
if (_isWaitForAsyncComplete)
|
||||
{
|
||||
Bundle = _createRequest.assetBundle;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_createRequest.isDone)
|
||||
return;
|
||||
Bundle = _createRequest.assetBundle;
|
||||
}
|
||||
}
|
||||
|
||||
if (Bundle == null)
|
||||
{
|
||||
_steps = Steps.Done;
|
||||
_provider.SetStatus(NTaskStatus.Fail,
|
||||
$"failed load assetBundle : {_provider.BundleInfo.Bundle.Name}");
|
||||
Debug.LogError(_provider.ErrorMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
_provider.SetStatus(NTaskStatus.Success);
|
||||
_provider.AssetBundle = Bundle;
|
||||
_steps = Steps.Done;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
_isWaitForAsyncComplete = true;
|
||||
|
||||
int frame = 1000;
|
||||
while (true)
|
||||
{
|
||||
// 保险机制
|
||||
frame--;
|
||||
if (frame == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Update();
|
||||
|
||||
if (_provider.IsDone)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
_createRequest = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b1157be68cf14e4ea3b8dcb2eab1f59c
|
||||
timeCreated: 1677552109
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public interface IAssetLoader
|
||||
{
|
||||
void Start(AssetProvider provider);
|
||||
void Update();
|
||||
void WaitForAsyncComplete();
|
||||
void Destroy();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 66d37ccc10d2432497d403f43d45e6d9
|
||||
timeCreated: 1677549879
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal interface IBundleLoader
|
||||
{
|
||||
void Start(BundledProvider provider);
|
||||
void Update();
|
||||
void WaitForAsyncComplete();
|
||||
void Destroy();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 033c5ad1ff974e368aac38947b3702c8
|
||||
timeCreated: 1677549943
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public interface ISceneLoader
|
||||
{
|
||||
void Start(SceneProvider provider);
|
||||
void Update();
|
||||
void WaitForAsyncComplete();
|
||||
void Destroy();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: de7de727380a4f8695304aded82565aa
|
||||
timeCreated: 1677832119
|
||||
@@ -1,101 +0,0 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal class SceneLoadFromDatabase : ISceneLoader
|
||||
{
|
||||
public static ISceneLoader CreateInstance()
|
||||
{
|
||||
return new SceneLoadFromDatabase();
|
||||
}
|
||||
|
||||
private enum Steps
|
||||
{
|
||||
LoadDependency,
|
||||
LoadScene,
|
||||
}
|
||||
|
||||
private bool _isWaitForAsyncComplete;
|
||||
private SceneProvider _provider;
|
||||
private AssetInfo _assetInfo;
|
||||
private Steps _steps;
|
||||
private AsyncOperation _asyncOperation;
|
||||
|
||||
public void Start(SceneProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
_assetInfo = provider.AssetInfo;
|
||||
_steps = Steps.LoadDependency;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (_steps == Steps.LoadDependency)
|
||||
{
|
||||
var scenePath = _assetInfo.Path;
|
||||
LoadSceneParameters loadSceneParameters = new LoadSceneParameters
|
||||
{
|
||||
loadSceneMode = _provider.SceneMode
|
||||
};
|
||||
if (_isWaitForAsyncComplete)
|
||||
{
|
||||
UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode(scenePath, loadSceneParameters);
|
||||
SetStatus();
|
||||
}
|
||||
else
|
||||
{
|
||||
_asyncOperation =
|
||||
UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(scenePath,
|
||||
loadSceneParameters);
|
||||
_steps = Steps.LoadScene;
|
||||
}
|
||||
}
|
||||
else if (_steps == Steps.LoadScene)
|
||||
{
|
||||
if (!_asyncOperation.isDone) return;
|
||||
SetStatus();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
_isWaitForAsyncComplete = true;
|
||||
|
||||
int frame = 1000;
|
||||
while (true)
|
||||
{
|
||||
frame--;
|
||||
if (frame == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Update();
|
||||
|
||||
if (_provider.IsDone)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void SetStatus()
|
||||
{
|
||||
var sceneObj = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
||||
_provider.SceneObject = sceneObj;
|
||||
_provider.SetStatus(NTaskStatus.Success);
|
||||
}
|
||||
|
||||
private void SetStatus(string error)
|
||||
{
|
||||
_provider.SetStatus(NTaskStatus.Fail, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 46af8c940cb3447a970be02ea0aeea4a
|
||||
timeCreated: 1677833907
|
||||
@@ -1,143 +0,0 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal class SceneLoaderFromBundle : ISceneLoader
|
||||
{
|
||||
public static ISceneLoader CreateInstance()
|
||||
{
|
||||
return new SceneLoaderFromBundle();
|
||||
}
|
||||
|
||||
private enum Steps
|
||||
{
|
||||
LoadDependency,
|
||||
LoadScene,
|
||||
}
|
||||
|
||||
private Dependency _dependency;
|
||||
private AssetInfo _assetInfo;
|
||||
private Steps _steps = Steps.LoadDependency;
|
||||
private SceneProvider _provider;
|
||||
|
||||
private bool _isWaitForAsyncComplete;
|
||||
private AsyncOperation _asyncOperation;
|
||||
|
||||
public void Start(SceneProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
var newAsset = true;
|
||||
if (_assetInfo == null)
|
||||
{
|
||||
_assetInfo = provider.AssetInfo;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_assetInfo.GUID == provider.AssetInfo.GUID)
|
||||
{
|
||||
newAsset = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
_assetInfo = provider.AssetInfo;
|
||||
}
|
||||
}
|
||||
|
||||
if (newAsset || _dependency == null)
|
||||
{
|
||||
_dependency = Dependency.GetAssetDependency(_assetInfo);
|
||||
_dependency.Retain();
|
||||
}
|
||||
|
||||
_steps = Steps.LoadDependency;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (_steps == Steps.LoadDependency)
|
||||
{
|
||||
if (_isWaitForAsyncComplete)
|
||||
{
|
||||
_dependency.WaitForAsyncComplete();
|
||||
}
|
||||
else if (!_dependency.IsDone) return;
|
||||
|
||||
if (!_dependency.IsSucceed)
|
||||
{
|
||||
Debug.LogError("error");
|
||||
SetStatus("dependency fail");
|
||||
}
|
||||
|
||||
var provider = _dependency.GetMainBundledProvider();
|
||||
if (provider != null && provider.AssetBundle != null)
|
||||
{
|
||||
var scenePath = Path.GetFileNameWithoutExtension(_assetInfo.Path);
|
||||
if (_isWaitForAsyncComplete)
|
||||
{
|
||||
SceneManager.LoadScene(scenePath, _provider.SceneMode);
|
||||
SetStatus();
|
||||
}
|
||||
else
|
||||
{
|
||||
_asyncOperation = SceneManager.LoadSceneAsync(scenePath, _provider.SceneMode);
|
||||
_asyncOperation.allowSceneActivation = true;
|
||||
_steps = Steps.LoadScene;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//失败,后续补全失败逻辑
|
||||
Debug.LogError("error1");
|
||||
SetStatus("error");
|
||||
}
|
||||
}
|
||||
else if (_steps == Steps.LoadScene)
|
||||
{
|
||||
if (!_asyncOperation.isDone) return;
|
||||
SetStatus();
|
||||
}
|
||||
}
|
||||
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
_isWaitForAsyncComplete = true;
|
||||
|
||||
int frame = 1000;
|
||||
while (true)
|
||||
{
|
||||
frame--;
|
||||
if (frame == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Update();
|
||||
|
||||
if (_provider.IsDone)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
_dependency.Release();
|
||||
_asyncOperation = null;
|
||||
}
|
||||
|
||||
|
||||
private void SetStatus()
|
||||
{
|
||||
var sceneObj = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
||||
_provider.SceneObject = sceneObj;
|
||||
_provider.SetStatus(NTaskStatus.Success);
|
||||
}
|
||||
|
||||
|
||||
private void SetStatus(string error)
|
||||
{
|
||||
_provider.SetStatus(NTaskStatus.Fail, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: febc5527b0844fbc8d46f8315bafee24
|
||||
timeCreated: 1678027484
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30c848f308bc4088a66095eb6f34e549
|
||||
timeCreated: 1677052587
|
||||
@@ -1,117 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class AssetProvider : ProviderBase
|
||||
{
|
||||
public static Func<IAssetLoader> CreateLoader { get; set; } = AssetLoaderFromBundle.CreateInstance;
|
||||
|
||||
private IAssetLoader _loader;
|
||||
|
||||
public AssetInfo AssetInfo { get; internal set; }
|
||||
|
||||
public bool IsAll { get; set; }
|
||||
|
||||
public UnityEngine.Object Asset { get; set; }
|
||||
|
||||
public UnityEngine.Object[] AllAsset { get; set; }
|
||||
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
if (_loader == null) _loader = CreateLoader();
|
||||
_loader.Start(this);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
#if DEBUG
|
||||
DebugRecord();
|
||||
#endif
|
||||
if (IsDone) return NTaskStatus.Success;
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
_loader.WaitForAsyncComplete();
|
||||
}
|
||||
else
|
||||
{
|
||||
_loader.Update();
|
||||
}
|
||||
|
||||
return base.OnProcess();
|
||||
}
|
||||
|
||||
public override void Destroy()
|
||||
{
|
||||
Debug.Log($"卸载资源==={AssetInfo.Path}");
|
||||
base.Destroy();
|
||||
_assets.Remove(this);
|
||||
_loader.Destroy();
|
||||
_loader = null;
|
||||
}
|
||||
|
||||
#region Static
|
||||
|
||||
private static readonly List<AssetProvider> _assets = new List<AssetProvider>();
|
||||
|
||||
internal static List<AssetProvider> GetAssetProviders()
|
||||
{
|
||||
return _assets;
|
||||
}
|
||||
|
||||
internal static void ReleaseAllAssets(bool force = true)
|
||||
{
|
||||
foreach (var asset in _assets)
|
||||
{
|
||||
asset.Release(force);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void ReleaseAllAssetsByTag(string[] tags, bool force = true)
|
||||
{
|
||||
foreach (var asset in _assets)
|
||||
{
|
||||
if (asset.AssetInfo.HasTag(tags))
|
||||
{
|
||||
asset.Release(force);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static AssetProvider GetAssetProvider(AssetInfo assetInfo, bool isAll = false)
|
||||
{
|
||||
if (assetInfo == null) return null;
|
||||
AssetProvider provider = null;
|
||||
foreach (var asset in _assets)
|
||||
{
|
||||
if (asset.AssetInfo == assetInfo)
|
||||
{
|
||||
provider = asset;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (provider == null)
|
||||
{
|
||||
provider = new AssetProvider
|
||||
{
|
||||
AssetInfo = assetInfo,
|
||||
IsAll = isAll
|
||||
};
|
||||
#if DEBUG
|
||||
provider.InitDebugInfo();
|
||||
#endif
|
||||
_assets.Add(provider);
|
||||
}
|
||||
|
||||
provider.Run();
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e767bd7f49546e5af835e60bc4ca363
|
||||
timeCreated: 1677057856
|
||||
@@ -1,107 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal class BundledProvider : ProviderBase
|
||||
{
|
||||
public static Func<IBundleLoader> CreateLoader { get; set; } = null;
|
||||
|
||||
private IBundleLoader _loader;
|
||||
public BundleInfo BundleInfo;
|
||||
public AssetBundle AssetBundle { get; set; }
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
_loader.Start(this);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
#if DEBUG
|
||||
DebugRecord();
|
||||
#endif
|
||||
if (IsDone) return NTaskStatus.Success;
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
_loader.WaitForAsyncComplete();
|
||||
}
|
||||
else
|
||||
{
|
||||
_loader.Update();
|
||||
}
|
||||
|
||||
return base.OnProcess();
|
||||
}
|
||||
|
||||
public override void Destroy()
|
||||
{
|
||||
Debug.Log($"卸载Bundle==={BundleInfo.Bundle.Name}");
|
||||
base.Destroy();
|
||||
_bundled.Remove(this);
|
||||
_loader.Destroy();
|
||||
if (AssetBundle != null)
|
||||
{
|
||||
AssetBundle.Unload(true);
|
||||
AssetBundle = null;
|
||||
}
|
||||
|
||||
_loader = null;
|
||||
}
|
||||
|
||||
#region Static
|
||||
|
||||
private static readonly List<BundledProvider> _bundled = new List<BundledProvider>();
|
||||
|
||||
internal static List<BundledProvider> GetBundleProviders()
|
||||
{
|
||||
return _bundled;
|
||||
}
|
||||
|
||||
internal static BundledProvider GetBundleProvider(BundleInfo bundleInfo)
|
||||
{
|
||||
BundledProvider provider = null;
|
||||
foreach (var bundled in _bundled)
|
||||
{
|
||||
if (bundled.BundleInfo == bundleInfo)
|
||||
{
|
||||
provider = bundled;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (provider == null)
|
||||
{
|
||||
provider = new BundledProvider
|
||||
{
|
||||
BundleInfo = bundleInfo
|
||||
};
|
||||
provider._loader = GetBundleLoader(provider);
|
||||
#if DEBUG
|
||||
provider.InitDebugInfo();
|
||||
#endif
|
||||
_bundled.Add(provider);
|
||||
}
|
||||
|
||||
provider.Run();
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
internal static IBundleLoader GetBundleLoader(BundledProvider provider)
|
||||
{
|
||||
var loader = CreateLoader?.Invoke();
|
||||
if (loader != null) return loader;
|
||||
var bundleInfo = provider.BundleInfo;
|
||||
if (bundleInfo.LoadMode == BundleLoadMode.LoadFromRemote)
|
||||
{
|
||||
return new BundleLoaderFromDownload();
|
||||
}
|
||||
|
||||
return new BundleLoaderFromLocal();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5796a93598ef48cfbe829a4fd48b1cc2
|
||||
timeCreated: 1677057801
|
||||
@@ -1,99 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal class Dependency
|
||||
{
|
||||
/// <summary>
|
||||
/// 依赖的资源包加载器列表
|
||||
/// </summary>
|
||||
internal readonly List<BundledProvider> DependBundles;
|
||||
|
||||
public Dependency(List<BundledProvider> dependBundles)
|
||||
{
|
||||
DependBundles = dependBundles;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经完成(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (var bundle in DependBundles)
|
||||
{
|
||||
if (!bundle.IsDone)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSucceed
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (var loader in DependBundles)
|
||||
{
|
||||
if (loader.Status != NTaskStatus.Success)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
foreach (var bundle in DependBundles)
|
||||
{
|
||||
if (!bundle.IsDone)
|
||||
bundle.WaitForAsyncComplete();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Retain()
|
||||
{
|
||||
foreach (var request in DependBundles)
|
||||
{
|
||||
// request.Retain();
|
||||
}
|
||||
}
|
||||
|
||||
public void Release()
|
||||
{
|
||||
foreach (var request in DependBundles)
|
||||
{
|
||||
request.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public BundledProvider GetMainBundledProvider()
|
||||
{
|
||||
return DependBundles.Count > 0 ? DependBundles[0] : null;
|
||||
}
|
||||
|
||||
|
||||
#region Static
|
||||
|
||||
internal static Dependency GetAssetDependency(AssetInfo assetInfo)
|
||||
{
|
||||
var bundleInfos = Addressable.GetAllDependBundleInfos(assetInfo);
|
||||
List<BundledProvider> list = new List<BundledProvider>();
|
||||
foreach (var info in bundleInfos)
|
||||
{
|
||||
list.Add(BundledProvider.GetBundleProvider(info));
|
||||
}
|
||||
|
||||
var dep = new Dependency(list);
|
||||
return dep;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1f18e197df9a4a1dbca3cb4913cb4f4d
|
||||
timeCreated: 1677551909
|
||||
@@ -1,110 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public abstract class ProviderBase : NTask, IRecyclable
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否已经销毁
|
||||
/// </summary>
|
||||
public bool IsDestroyed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以销毁
|
||||
/// </summary>
|
||||
public bool CanDestroy => IsDone && RefCount <= 0;
|
||||
|
||||
protected bool IsWaitForAsyncComplete { private set; get; } = false;
|
||||
|
||||
public void SetStatus(NTaskStatus status, string info = "")
|
||||
{
|
||||
Status = status;
|
||||
_errorMsg = info;
|
||||
}
|
||||
|
||||
internal virtual void Run()
|
||||
{
|
||||
Retain();
|
||||
if (!IsDone && !IsRunning)
|
||||
{
|
||||
Run(TaskRunner.ProviderRunner);
|
||||
}
|
||||
}
|
||||
|
||||
#region RefCounter
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数
|
||||
/// </summary>
|
||||
public int RefCount { get; private set; }
|
||||
|
||||
public void Retain()
|
||||
{
|
||||
RefCount++;
|
||||
}
|
||||
|
||||
public void Release(bool force = false)
|
||||
{
|
||||
RefCount--;
|
||||
if (force) RefCount = 0;
|
||||
if (RefCount > 0) return;
|
||||
//释放资源
|
||||
Recycler.Add(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 销毁资源对象
|
||||
/// </summary>
|
||||
public virtual void Destroy()
|
||||
{
|
||||
IsDestroyed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
IsWaitForAsyncComplete = true;
|
||||
Process();
|
||||
}
|
||||
|
||||
#region Debug
|
||||
|
||||
#if DEBUG
|
||||
public string LoadScene = string.Empty;
|
||||
public string LoadTime = string.Empty;
|
||||
public long LoadTotalTime { protected set; get; }
|
||||
|
||||
// 加载耗时统计
|
||||
private bool _isRecording;
|
||||
private Stopwatch _watch;
|
||||
|
||||
internal void InitDebugInfo()
|
||||
{
|
||||
LoadScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
|
||||
LoadTime = DateTime.Now.ToString("hh:mm:ss");
|
||||
}
|
||||
|
||||
protected void DebugRecord()
|
||||
{
|
||||
if (_isRecording == false)
|
||||
{
|
||||
_isRecording = true;
|
||||
_watch = Stopwatch.StartNew();
|
||||
}
|
||||
|
||||
if (_watch == null) return;
|
||||
if (!IsDone) return;
|
||||
LoadTotalTime = _watch.ElapsedMilliseconds;
|
||||
_watch = null;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0582024b8cee499496844a7c5c204286
|
||||
timeCreated: 1677051990
|
||||
@@ -1,122 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class SceneProvider : ProviderBase
|
||||
{
|
||||
public static Func<ISceneLoader> CreateLoader { get; set; } = SceneLoaderFromBundle.CreateInstance;
|
||||
private ISceneLoader _loader;
|
||||
|
||||
public AssetInfo AssetInfo { get; internal set; }
|
||||
|
||||
public Scene SceneObject { get; set; }
|
||||
|
||||
public LoadSceneMode SceneMode;
|
||||
private AsyncOperation _asyncOp;
|
||||
private int _priority;
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
_loader.Start(this);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
#if DEBUG
|
||||
DebugRecord();
|
||||
#endif
|
||||
if (IsDone) return NTaskStatus.Success;
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
_loader.WaitForAsyncComplete();
|
||||
}
|
||||
else
|
||||
{
|
||||
_loader.Update();
|
||||
}
|
||||
|
||||
return base.OnProcess();
|
||||
}
|
||||
|
||||
public override void Destroy()
|
||||
{
|
||||
base.Destroy();
|
||||
_scenes.Remove(this);
|
||||
_loader.Destroy();
|
||||
_loader = null;
|
||||
}
|
||||
|
||||
internal override void Run()
|
||||
{
|
||||
Retain();
|
||||
if (!IsRunning)
|
||||
{
|
||||
Run(TaskRunner.ProviderRunner);
|
||||
}
|
||||
}
|
||||
|
||||
#region Static
|
||||
|
||||
private static readonly List<SceneProvider> _scenes = new List<SceneProvider>();
|
||||
|
||||
internal static List<SceneProvider> GetSceneProviders()
|
||||
{
|
||||
return _scenes;
|
||||
}
|
||||
|
||||
internal static void ReleaseAllAssets(bool force = true)
|
||||
{
|
||||
foreach (var asset in _scenes)
|
||||
{
|
||||
asset.Release(force);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void ReleaseAllAssetsByTag(string[] tags, bool force = true)
|
||||
{
|
||||
foreach (var asset in _scenes)
|
||||
{
|
||||
if (asset.AssetInfo.HasTag(tags))
|
||||
{
|
||||
asset.Release(force);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static SceneProvider GetSceneProvider(AssetInfo assetInfo, bool additive = false)
|
||||
{
|
||||
SceneProvider provider = null;
|
||||
foreach (var scene in _scenes)
|
||||
{
|
||||
if (scene.AssetInfo == assetInfo)
|
||||
{
|
||||
provider = scene;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (provider == null)
|
||||
{
|
||||
provider = new SceneProvider
|
||||
{
|
||||
AssetInfo = assetInfo,
|
||||
_loader = CreateLoader(),
|
||||
SceneMode = additive ? LoadSceneMode.Additive : LoadSceneMode.Single
|
||||
};
|
||||
#if DEBUG
|
||||
provider.InitDebugInfo();
|
||||
#endif
|
||||
_scenes.Add(provider);
|
||||
}
|
||||
|
||||
provider.Run();
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64b0e1cc71b24c5c86d2229c56991e95
|
||||
timeCreated: 1677831975
|
||||
Reference in New Issue
Block a user