Update
This commit is contained in:
ALEXTANG
2022-05-27 21:31:25 +08:00
parent dfb0376415
commit c2c5730e3b
4 changed files with 56 additions and 31 deletions

View File

@@ -3,6 +3,9 @@ using System.Collections.Generic;
namespace TEngine namespace TEngine
{ {
/// <summary>
/// 封装的AssetBundle Plus
/// </summary>
public class AssetBundleData public class AssetBundleData
{ {
public AssetBundle Bundle; public AssetBundle Bundle;
@@ -120,12 +123,18 @@ namespace TEngine
if (_assets != null) if (_assets != null)
{ {
if (_path2Index.ContainsKey(assetPath)) if (_path2Index.ContainsKey(assetPath))
{
_assets[_path2Index[assetPath]] = assetData; _assets[_path2Index[assetPath]] = assetData;
}
else else
{
TLogger.LogError($"Try to set asset data '{assetPath}' which not in the bundle '{_name}'"); TLogger.LogError($"Try to set asset data '{assetPath}' which not in the bundle '{_name}'");
}
} }
else else
{
TLogger.LogError($"Try to set asset data in the bundle '{_name}' which not include any asset"); TLogger.LogError($"Try to set asset data in the bundle '{_name}' which not include any asset");
}
} }
/// <summary> /// <summary>
@@ -187,14 +196,17 @@ namespace TEngine
for (int i = 0; i < Dependencies.Length; ++i) for (int i = 0; i < Dependencies.Length; ++i)
{ {
if (!Dependencies[i]._isInvokeLoad) if (!Dependencies[i]._isInvokeLoad)
{
Dependencies[i].LoadAsync(OnDependenciesAsyncLoadComplete, true); Dependencies[i].LoadAsync(OnDependenciesAsyncLoadComplete, true);
}
else else
{
TLogger.LogWarning($"Dependency cycle detected for '{Dependencies[i]._name}'"); TLogger.LogWarning($"Dependency cycle detected for '{Dependencies[i]._name}'");
}
} }
_asyncLoadRequest = new AsyncLoadRequest _asyncLoadRequest = new AsyncLoadRequest
{ {
Request = AssetBundle.LoadFromFileAsync(FileSystem.GetAssetBundlePathInVersion(_name), 0, Offset) Request = AssetBundle.LoadFromFileAsync(FileSystem.GetAssetBundlePathInVersion(_name), 0, Offset),
,
DependedRefCount = bDepended ? 1 : 0 DependedRefCount = bDepended ? 1 : 0
}; };
_asyncLoadRequest.Request.completed += OnAssetBundleLoadComplete; _asyncLoadRequest.Request.completed += OnAssetBundleLoadComplete;

View File

@@ -5,7 +5,7 @@ using UnityEngine.SceneManagement;
namespace TEngine namespace TEngine
{ {
/// <summary> /// <summary>
/// 资源配置器 负责资源路径 AssetBundle映射管理AssetBundle、Asset数据 /// 资源配置
/// </summary> /// </summary>
public class AssetConfig public class AssetConfig
{ {

View File

@@ -7,17 +7,20 @@ namespace TEngine
{ {
public class AssetData public class AssetData
{ {
#region Proprieties
private string _name;
private string _path;
private string _fullPath;
private int _refCount;
private bool _useSubAsset;
private AssetBundleData _refBundle; private AssetBundleData _refBundle;
private Object _assetObject; private Object _assetObject;
private Dictionary<string, UnityEngine.Object> _subAssetObjects; private Dictionary<string, UnityEngine.Object> _subAssetObjects;
private string _kPath;
private string _fPath;
private string _name;
private int _refCount;
private bool _useSubAsset;
private AsyncOperation _asyncLoadRequest; private AsyncOperation _asyncLoadRequest;
private event System.Action<AssetData> _onAsyncLoadComplete; private event System.Action<AssetData> _onAsyncLoadComplete;
#endregion
#region Public Proprities
public UnityEngine.Object AssetObject => _assetObject; public UnityEngine.Object AssetObject => _assetObject;
public UnityEngine.Object this[string key] public UnityEngine.Object this[string key]
@@ -39,7 +42,7 @@ namespace TEngine
} }
else else
{ {
TLogger.LogError($"Can not get sub asset({key}) from {_fPath}"); TLogger.LogError($"Can not get sub asset({key}) from {_fullPath}");
} }
} }
} }
@@ -49,9 +52,9 @@ namespace TEngine
} }
} }
public string Path => _kPath; public string Path => _path;
public string FullPath => _fPath; public string FullPath => _fullPath;
public string Name => _name; public string Name => _name;
@@ -74,19 +77,20 @@ namespace TEngine
_onAsyncLoadComplete -= value; _onAsyncLoadComplete -= value;
} }
} }
#endregion
public AssetData(string path = "", AssetBundleData refBundle = null, UnityEngine.Object assetObject = null) public AssetData(string path = "", AssetBundleData refBundle = null, UnityEngine.Object assetObject = null)
{ {
_kPath = path; _path = path;
if (!string.IsNullOrEmpty(_kPath)) if (!string.IsNullOrEmpty(_path))
{ {
int ei = _kPath.LastIndexOf('/'); int ei = _path.LastIndexOf('/');
_name = ei >= 0 ? _kPath.Substring(ei + 1) : _kPath; _name = ei >= 0 ? _path.Substring(ei + 1) : _path;
_fPath = $"{AssetConfig.AssetRootPath}/{_kPath}"; _fullPath = $"{AssetConfig.AssetRootPath}/{_path}";
} }
else else
{ {
_fPath = System.String.Empty; _fullPath = System.String.Empty;
} }
_assetObject = assetObject; _assetObject = assetObject;
@@ -99,14 +103,15 @@ namespace TEngine
internal void SetAsPackageAsset(string path) internal void SetAsPackageAsset(string path)
{ {
_fPath = path; _fullPath = path;
} }
#region
public void AddRef() public void AddRef()
{ {
++_refCount; ++_refCount;
#if UNITY_EDITOR #if UNITY_EDITOR
TLogger.LogInfoSuccessd($"Add AssetData {_fPath} _refCount = {_refCount}"); TLogger.LogInfoSuccessd($"Add AssetData {_fullPath} _refCount = {_refCount}");
#endif #endif
} }
@@ -117,19 +122,21 @@ namespace TEngine
{ {
--_refCount; --_refCount;
#if UNITY_EDITOR #if UNITY_EDITOR
TLogger.LogInfoSuccessd($"Dec AssetData {_fPath} _refCount = {_refCount}"); TLogger.LogInfoSuccessd($"Dec AssetData {_fullPath} _refCount = {_refCount}");
#endif #endif
if (_refCount <= 0) if (_refCount <= 0)
{ {
if (_refBundle != null) if (_refBundle != null)
{ {
_refBundle.DecRef(bNoDelay); _refBundle.DecRef(bNoDelay);
_refBundle.SetAsset(_kPath, null); _refBundle.SetAsset(_path, null);
Unload(); Unload();
} }
} }
} }
#endregion
#region
/// <summary> /// <summary>
/// 同步加载Asset /// 同步加载Asset
/// </summary> /// </summary>
@@ -145,26 +152,26 @@ namespace TEngine
{ {
if (_useSubAsset) if (_useSubAsset)
{ {
UnityEngine.Object[] subAssets = _refBundle.Bundle.LoadAssetWithSubAssets(_fPath); UnityEngine.Object[] subAssets = _refBundle.Bundle.LoadAssetWithSubAssets(_fullPath);
if (subAssets != null && subAssets.Length > 0) if (subAssets != null && subAssets.Length > 0)
{ {
ProcessSubAssets(subAssets); ProcessSubAssets(subAssets);
} }
else else
{ {
TLogger.LogError($"Can not load the asset '{_fPath}'"); TLogger.LogError($"Can not load the asset '{_fullPath}'");
} }
} }
else else
{ {
UnityEngine.Object assetObject = _refBundle.Bundle.LoadAsset(_fPath); UnityEngine.Object assetObject = _refBundle.Bundle.LoadAsset(_fullPath);
if (assetObject != null) if (assetObject != null)
{ {
_assetObject = assetObject; _assetObject = assetObject;
} }
else else
{ {
TLogger.LogError($"Can not load the asset '{_fPath}'"); TLogger.LogError($"Can not load the asset '{_fullPath}'");
} }
} }
} }
@@ -203,7 +210,7 @@ namespace TEngine
{ {
if (_asyncLoadRequest == null) if (_asyncLoadRequest == null)
{ {
_asyncLoadRequest = bundleData.Bundle.LoadAssetWithSubAssetsAsync(_fPath); _asyncLoadRequest = bundleData.Bundle.LoadAssetWithSubAssetsAsync(_fullPath);
_asyncLoadRequest.completed += OnAssetLoadComplete; _asyncLoadRequest.completed += OnAssetLoadComplete;
} }
} }
@@ -218,7 +225,7 @@ namespace TEngine
{ {
if (_asyncLoadRequest == null) if (_asyncLoadRequest == null)
{ {
_asyncLoadRequest = bundleData.Bundle.LoadAssetAsync(_fPath); _asyncLoadRequest = bundleData.Bundle.LoadAssetAsync(_fullPath);
_asyncLoadRequest.completed += OnAssetLoadComplete; _asyncLoadRequest.completed += OnAssetLoadComplete;
} }
} }
@@ -229,7 +236,9 @@ namespace TEngine
} }
} }
else else
{
TLogger.LogError("Return a mismatch AssetBundleData for OnAssetBundleDataLoadComplete"); TLogger.LogError("Return a mismatch AssetBundleData for OnAssetBundleDataLoadComplete");
}
} }
void OnAssetLoadComplete(AsyncOperation asyncOperation) void OnAssetLoadComplete(AsyncOperation asyncOperation)
@@ -249,7 +258,7 @@ namespace TEngine
} }
else else
{ {
TLogger.LogError($"Can not load the asset '{_fPath}'"); TLogger.LogError($"Can not load the asset '{_fullPath}'");
} }
} }
else else
@@ -257,7 +266,7 @@ namespace TEngine
_assetObject = assetBundleRequest.asset; _assetObject = assetBundleRequest.asset;
if (_assetObject == null) if (_assetObject == null)
{ {
TLogger.LogError($"Can not load the asset '{_fPath}'"); TLogger.LogError($"Can not load the asset '{_fullPath}'");
} }
} }
} }
@@ -297,7 +306,7 @@ namespace TEngine
_subAssetObjects.Add(name, sprites[i]); _subAssetObjects.Add(name, sprites[i]);
} }
} }
else else
{ {
for (int i = 0; i < subAssets.Length; ++i) for (int i = 0; i < subAssets.Length; ++i)
{ {
@@ -343,8 +352,9 @@ namespace TEngine
} }
else else
{ {
TLogger.LogWarning($"Try to unload refcount > 0 asset({_fPath})!"); TLogger.LogWarning($"Try to unload refcount > 0 asset({_fullPath})!");
} }
} }
#endregion
} }
} }

View File

@@ -3,6 +3,9 @@ using UnityEngine;
namespace TEngine namespace TEngine
{ {
/// <summary>
/// 通用资源加载接口
/// </summary>
public static class TResources public static class TResources
{ {
#region #region