From c2c5730e3bceadfdce8fcf8b7d0e11dd897c91c0 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Fri, 27 May 2022 21:31:25 +0800 Subject: [PATCH] Update Update --- Assets/TEngine/Runtime/Res/AssetBundleData.cs | 16 ++++- Assets/TEngine/Runtime/Res/AssetConfig.cs | 2 +- Assets/TEngine/Runtime/Res/AssetData.cs | 66 +++++++++++-------- Assets/TEngine/Runtime/Res/TResource.cs | 3 + 4 files changed, 56 insertions(+), 31 deletions(-) diff --git a/Assets/TEngine/Runtime/Res/AssetBundleData.cs b/Assets/TEngine/Runtime/Res/AssetBundleData.cs index 2ef3d362..4ad32d02 100644 --- a/Assets/TEngine/Runtime/Res/AssetBundleData.cs +++ b/Assets/TEngine/Runtime/Res/AssetBundleData.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; namespace TEngine { + /// + /// 封装的AssetBundle Plus + /// public class AssetBundleData { public AssetBundle Bundle; @@ -120,12 +123,18 @@ namespace TEngine if (_assets != null) { if (_path2Index.ContainsKey(assetPath)) + { _assets[_path2Index[assetPath]] = assetData; + } else + { TLogger.LogError($"Try to set asset data '{assetPath}' which not in the bundle '{_name}'"); + } } else + { TLogger.LogError($"Try to set asset data in the bundle '{_name}' which not include any asset"); + } } /// @@ -187,14 +196,17 @@ namespace TEngine for (int i = 0; i < Dependencies.Length; ++i) { if (!Dependencies[i]._isInvokeLoad) + { Dependencies[i].LoadAsync(OnDependenciesAsyncLoadComplete, true); + } else + { TLogger.LogWarning($"Dependency cycle detected for '{Dependencies[i]._name}'"); + } } _asyncLoadRequest = new AsyncLoadRequest { - Request = AssetBundle.LoadFromFileAsync(FileSystem.GetAssetBundlePathInVersion(_name), 0, Offset) - , + Request = AssetBundle.LoadFromFileAsync(FileSystem.GetAssetBundlePathInVersion(_name), 0, Offset), DependedRefCount = bDepended ? 1 : 0 }; _asyncLoadRequest.Request.completed += OnAssetBundleLoadComplete; diff --git a/Assets/TEngine/Runtime/Res/AssetConfig.cs b/Assets/TEngine/Runtime/Res/AssetConfig.cs index d6e02f24..55467a31 100644 --- a/Assets/TEngine/Runtime/Res/AssetConfig.cs +++ b/Assets/TEngine/Runtime/Res/AssetConfig.cs @@ -5,7 +5,7 @@ using UnityEngine.SceneManagement; namespace TEngine { /// - /// 资源配置器 负责资源路径 AssetBundle映射,管理AssetBundle、Asset数据 + /// 资源配置 /// public class AssetConfig { diff --git a/Assets/TEngine/Runtime/Res/AssetData.cs b/Assets/TEngine/Runtime/Res/AssetData.cs index 19d38dcc..e2ca1323 100644 --- a/Assets/TEngine/Runtime/Res/AssetData.cs +++ b/Assets/TEngine/Runtime/Res/AssetData.cs @@ -7,17 +7,20 @@ namespace TEngine { public class AssetData { + #region Proprieties + private string _name; + private string _path; + private string _fullPath; + private int _refCount; + private bool _useSubAsset; private AssetBundleData _refBundle; private Object _assetObject; private Dictionary _subAssetObjects; - private string _kPath; - private string _fPath; - private string _name; - private int _refCount; - private bool _useSubAsset; private AsyncOperation _asyncLoadRequest; private event System.Action _onAsyncLoadComplete; + #endregion + #region Public Proprities public UnityEngine.Object AssetObject => _assetObject; public UnityEngine.Object this[string key] @@ -39,7 +42,7 @@ namespace TEngine } 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; @@ -74,19 +77,20 @@ namespace TEngine _onAsyncLoadComplete -= value; } } + #endregion public AssetData(string path = "", AssetBundleData refBundle = null, UnityEngine.Object assetObject = null) { - _kPath = path; - if (!string.IsNullOrEmpty(_kPath)) + _path = path; + if (!string.IsNullOrEmpty(_path)) { - int ei = _kPath.LastIndexOf('/'); - _name = ei >= 0 ? _kPath.Substring(ei + 1) : _kPath; - _fPath = $"{AssetConfig.AssetRootPath}/{_kPath}"; + int ei = _path.LastIndexOf('/'); + _name = ei >= 0 ? _path.Substring(ei + 1) : _path; + _fullPath = $"{AssetConfig.AssetRootPath}/{_path}"; } else { - _fPath = System.String.Empty; + _fullPath = System.String.Empty; } _assetObject = assetObject; @@ -99,14 +103,15 @@ namespace TEngine internal void SetAsPackageAsset(string path) { - _fPath = path; + _fullPath = path; } + #region 计数引用 public void AddRef() { ++_refCount; #if UNITY_EDITOR - TLogger.LogInfoSuccessd($"Add AssetData {_fPath} _refCount = {_refCount}"); + TLogger.LogInfoSuccessd($"Add AssetData {_fullPath} _refCount = {_refCount}"); #endif } @@ -117,19 +122,21 @@ namespace TEngine { --_refCount; #if UNITY_EDITOR - TLogger.LogInfoSuccessd($"Dec AssetData {_fPath} _refCount = {_refCount}"); + TLogger.LogInfoSuccessd($"Dec AssetData {_fullPath} _refCount = {_refCount}"); #endif if (_refCount <= 0) { if (_refBundle != null) { _refBundle.DecRef(bNoDelay); - _refBundle.SetAsset(_kPath, null); + _refBundle.SetAsset(_path, null); Unload(); } } } + #endregion + #region 加载与卸载 /// /// 同步加载Asset /// @@ -145,26 +152,26 @@ namespace TEngine { if (_useSubAsset) { - UnityEngine.Object[] subAssets = _refBundle.Bundle.LoadAssetWithSubAssets(_fPath); + UnityEngine.Object[] subAssets = _refBundle.Bundle.LoadAssetWithSubAssets(_fullPath); if (subAssets != null && subAssets.Length > 0) { ProcessSubAssets(subAssets); } else { - TLogger.LogError($"Can not load the asset '{_fPath}'"); + TLogger.LogError($"Can not load the asset '{_fullPath}'"); } } else { - UnityEngine.Object assetObject = _refBundle.Bundle.LoadAsset(_fPath); + UnityEngine.Object assetObject = _refBundle.Bundle.LoadAsset(_fullPath); if (assetObject != null) { _assetObject = assetObject; } 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) { - _asyncLoadRequest = bundleData.Bundle.LoadAssetWithSubAssetsAsync(_fPath); + _asyncLoadRequest = bundleData.Bundle.LoadAssetWithSubAssetsAsync(_fullPath); _asyncLoadRequest.completed += OnAssetLoadComplete; } } @@ -218,7 +225,7 @@ namespace TEngine { if (_asyncLoadRequest == null) { - _asyncLoadRequest = bundleData.Bundle.LoadAssetAsync(_fPath); + _asyncLoadRequest = bundleData.Bundle.LoadAssetAsync(_fullPath); _asyncLoadRequest.completed += OnAssetLoadComplete; } } @@ -229,7 +236,9 @@ namespace TEngine } } else + { TLogger.LogError("Return a mismatch AssetBundleData for OnAssetBundleDataLoadComplete"); + } } void OnAssetLoadComplete(AsyncOperation asyncOperation) @@ -249,7 +258,7 @@ namespace TEngine } else { - TLogger.LogError($"Can not load the asset '{_fPath}'"); + TLogger.LogError($"Can not load the asset '{_fullPath}'"); } } else @@ -257,7 +266,7 @@ namespace TEngine _assetObject = assetBundleRequest.asset; 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]); } } - else + else { for (int i = 0; i < subAssets.Length; ++i) { @@ -343,8 +352,9 @@ namespace TEngine } else { - TLogger.LogWarning($"Try to unload refcount > 0 asset({_fPath})!"); + TLogger.LogWarning($"Try to unload refcount > 0 asset({_fullPath})!"); } } + #endregion } } diff --git a/Assets/TEngine/Runtime/Res/TResource.cs b/Assets/TEngine/Runtime/Res/TResource.cs index 33590708..528cb031 100644 --- a/Assets/TEngine/Runtime/Res/TResource.cs +++ b/Assets/TEngine/Runtime/Res/TResource.cs @@ -3,6 +3,9 @@ using UnityEngine; namespace TEngine { + /// + /// 通用资源加载接口 + /// public static class TResources { #region 同步加载