暂存修改 尝试添加热重载插件

This commit is contained in:
2024-09-23 22:56:51 +08:00
parent 5b79a2d4bb
commit 8f824e0671
373 changed files with 3835 additions and 5709 deletions

View File

@ -23,13 +23,13 @@ public class LoadAssetBundle : MonoBehaviour
public const string atlasPathConvertTable = "AtlasPathConvert";
public const string BUNDLE_MAIN_BASE_UI = "MainBase";
public const string BUNDLE_PATH_MODEL = "model/";
public const string BUNDLE_PATH_PET = "model/";
public const string BUNDLE_PATH_EFFECT = "effect/";
public const string BUNDLE_PATH_UI = "ui/";
public const string BUNDLE_PATH_SOUND = "sounds/";
public const string BUNDLE_PATH_SPRITE = "ui/sprite/commonitem";
public const string BUNDLE_PATH_Other = "other/";
public const string BUNDLE_PATH_MODEL = "model/";
public const string BUNDLE_PATH_PET = "model/";
public const string BUNDLE_PATH_EFFECT = "effect/";
public const string BUNDLE_PATH_UI = "ui/";
public const string BUNDLE_PATH_SOUND = "sounds/";
public const string BUNDLE_PATH_SPRITE = "ui/sprite/commonitem";
public const string BUNDLE_PATH_Other = "other/";
public const string bundleFileExtension = AssetConst.bundleVariant;
public const string BUNDLE_PATH_GAMERES = "gameres";
@ -44,11 +44,11 @@ public class LoadAssetBundle : MonoBehaviour
public const string uiSpriteBundleHeader = "ui/sprite/";
private readonly LoadAssetRequestList<GameObject> _gameObjectList = new LoadAssetRequestList<GameObject>();
private readonly LoadAssetRequestList<Object> _assetList = new LoadAssetRequestList<Object>();
private readonly LoadAssetRequestList<AudioClip> _audioList = new LoadAssetRequestList<AudioClip>();
private readonly LoadAssetRequestList<TextAsset> _textAssetList = new LoadAssetRequestList<TextAsset>();
private readonly LoadAssetRequestList<Object> _assetList = new LoadAssetRequestList<Object>();
private readonly LoadAssetRequestList<AudioClip> _audioList = new LoadAssetRequestList<AudioClip>();
private readonly LoadAssetRequestList<TextAsset> _textAssetList = new LoadAssetRequestList<TextAsset>();
private readonly LoadSpriteRequestList _spriteList = new LoadSpriteRequestList();
private readonly LoadSpriteRequestList _spriteList = new LoadSpriteRequestList();
private readonly LoadRawImageRequestList _rawImageList = new LoadRawImageRequestList();
public AssetPathHub pathHub { get; private set; }
@ -75,20 +75,22 @@ public class LoadAssetBundle : MonoBehaviour
}
var dependencyPath =
AssetConst.persistentDataPath.Open(AssetUtils.GetTextMd5(AssetConst.versionFile) + AssetConst.bundleVariant);
AssetConst.persistentDataPath.Open(AssetUtils.GetTextMd5(AssetConst.versionFile) +
AssetConst.bundleVariant);
AssetDependencyInfo dependencyInfo = null;
if (!File.Exists(dependencyPath))
{
Debug.LogError(
"Cannot find dependency file. You should start the game from AssetUpdate instead of Login!");
"Cannot find dependency file. You should start the game from AssetUpdate instead of Login!");
}
else
dependencyInfo = AssetDependencyInfo.Create(dependencyPath);
Debug.Log("LoaderAssetsBundle");
if (dependencyInfo == null)
{
Debug.LogError("Unable to load dependency file! This is not recoverable!");
LiteConfirmWin.Open("资源错误", "无法加载资源依赖!\n需要重启游戏修复",
new LiteConfirmButtonData("确定", () => SdkControl.instance.CloseGame()));
new LiteConfirmButtonData("确定", () => SdkControl.instance.CloseGame()));
}
else
{
@ -103,7 +105,7 @@ public class LoadAssetBundle : MonoBehaviour
// 后台下载限制到最大一个;主动加载限制到最大三个;
downloadHub.Init(pathHub, new List<AsyncLoadRule>
{
new AsyncLoadRule(0, 1),
new AsyncLoadRule(0, 1),
new AsyncLoadRule(int.MaxValue, 3)
});
downloadHub.onComplete += pathHub.DownloadComplete;
@ -116,6 +118,7 @@ public class LoadAssetBundle : MonoBehaviour
assetManager.Init(bundleManager);
}
}
DontDestroyOnLoad(gameObject);
}
@ -124,7 +127,7 @@ public class LoadAssetBundle : MonoBehaviour
if (!AssetUpdateManager.useResources)
AssetManager.instance.bundleManager.downloadHub.StartBackGroundDownload();
}
public static void CreateInstance()
{
var loadObject = new GameObject("LoadAssetBundle");
@ -144,20 +147,20 @@ public class LoadAssetBundle : MonoBehaviour
private bool IsBundleAndAssetValid(string bundleName, string assetName)
{
return !string.IsNullOrEmpty(bundleName) &&
!string.IsNullOrEmpty(assetName) &&
!bundleName.Equals("-1") &&
!string.IsNullOrEmpty(assetName) &&
!bundleName.Equals("-1") &&
!assetName.Equals("-1");
}
private void LoadAssetFromList<T>(string bundleName, string assetName, LoadBundleAssetCallback<T> callback,
Hashtable hashtable, LoadAssetRequestList<T> list, bool archive) where T : Object
private void LoadAssetFromList<T>(string bundleName, string assetName, LoadBundleAssetCallback<T> callback,
Hashtable hashtable, LoadAssetRequestList<T> list, bool archive) where T : Object
{
// 已知问题,即使已经加载的资源,仍然会产生一次列表推入和回调监听,造成少量性能浪费
// 暂时不修改这个结构,保持当前强健的逻辑流程
list.AddCallback(bundleName, assetName, callback, hashtable);
LoadAssetInternal<T>(bundleName, assetName);
}
private void RemoveAllCallback(string bundleName, string assetName)
{
_gameObjectList.RemoveAllCallback(bundleName, assetName);
@ -168,19 +171,19 @@ public class LoadAssetBundle : MonoBehaviour
_rawImageList.RemoveAllCallback(bundleName, assetName);
}
public void LoadGameObject(string bundleName, string assetName, LoadBundleAssetCallback<GameObject> callback,
Hashtable hashParam, bool archive = false)
public void LoadGameObject(string bundleName, string assetName, LoadBundleAssetCallback<GameObject> callback,
Hashtable hashParam, bool archive = false)
{
if (IsBundleAndAssetValid(bundleName, assetName))
{
bundleName += assetName;
bundleName = FixBundleName(bundleName);
bundleName = FixBundleName(bundleName);
LoadAssetFromList(bundleName, assetName, callback, hashParam, _gameObjectList, archive);
}
}
public void LoadAsset(string bundleName, string assetName, LoadBundleAssetCallback<Object> callback,
Hashtable hashParam, bool archive = false)
public void LoadAsset(string bundleName, string assetName, LoadBundleAssetCallback<Object> callback,
Hashtable hashParam, bool archive = false)
{
if (IsBundleAndAssetValid(bundleName, assetName))
{
@ -225,8 +228,8 @@ public class LoadAssetBundle : MonoBehaviour
}
}
public void LoadUI(string bundleName, string assetName, LoadBundleAssetCallback<GameObject> callback,
Hashtable hashParam, bool archive = true)
public void LoadUI(string bundleName, string assetName, LoadBundleAssetCallback<GameObject> callback,
Hashtable hashParam, bool archive = true)
{
if (IsBundleAndAssetValid(bundleName, assetName))
{
@ -235,8 +238,8 @@ public class LoadAssetBundle : MonoBehaviour
}
}
public void LoadSoundAsync(string bundlePath, string assetName, LoadBundleAssetCallback<AudioClip> callback,
Hashtable hashParam)
public void LoadSoundAsync(string bundlePath, string assetName, LoadBundleAssetCallback<AudioClip> callback,
Hashtable hashParam)
{
if (!string.IsNullOrEmpty(assetName))
if (IsBundleAndAssetValid(bundlePath, assetName))
@ -254,7 +257,7 @@ public class LoadAssetBundle : MonoBehaviour
if (image.texture == null)
{
image.enabled = false;
valid = true;
valid = true;
}
else
{
@ -278,13 +281,14 @@ public class LoadAssetBundle : MonoBehaviour
if (image.sprite == null)
{
image.enabled = false;
valid = true;
valid = true;
}
else
{
valid = !image.sprite.name.Equals(assetName);
}
}
if (valid && IsBundleAndAssetValid(BUNDLE_PATH_SPRITE, assetName))
{
// 试图校正精灵包名
@ -306,7 +310,7 @@ public class LoadAssetBundle : MonoBehaviour
#endif
AssetManager.instance.LoadAsset<T>(bundleName, assetName, this, OnAssetLoadedFinish);
}
private void OnAssetLoadedFinish<T>(string bundleName, string assetName, T asset) where T : Object
{
_gameObjectList.FinishAsset(bundleName, assetName, asset);
@ -316,14 +320,14 @@ public class LoadAssetBundle : MonoBehaviour
_spriteList.FinishAsset(bundleName, assetName, asset);
_rawImageList.FinishAsset(bundleName, assetName, asset);
}
#if UNITY_EDITOR
[Serializable]
public class EditorBundleList
{
private readonly Dictionary<string, string> _assetDataList = new Dictionary<string, string>();
public EditorBundleList(IEnumerable<string> assets)
{
foreach (var asset in assets)
@ -343,27 +347,27 @@ public class LoadAssetBundle : MonoBehaviour
private class EditorResourceAsync
{
public readonly string bundleName;
public readonly string assetName;
public readonly string bundleName;
public readonly string assetName;
public readonly AsyncOperation operation;
public EditorResourceAsync(string bundleName, string assetName, AsyncOperation operation)
{
this.bundleName = bundleName;
this.assetName = assetName;
this.operation = operation;
this.assetName = assetName;
this.operation = operation;
}
}
public const string assetPathHeader = "Assets/Project3D/Resources";
public const string uiSpriteAssetPath = "Assets/Project3D/Sprites";
public const string bundleToAssetPath = "Assets/Editor/Config/BundleToAsset" + AssetConst.bytesExtension;
private Dictionary<string, string> _uiCommonPathDict;
private Dictionary<string, EditorBundleList> _resourcesDict;
private Dictionary<string, string> _uiCommonPathDict;
private Dictionary<string, EditorBundleList> _resourcesDict;
private const int _maxAsyncCount = 1;
private readonly List<EditorResourceAsync> _asyncList = new List<EditorResourceAsync>();
private readonly Queue<MyTuple<string, string>> _pendingList = new Queue<MyTuple<string, string>>();
private const int _maxAsyncCount = 1;
private readonly List<EditorResourceAsync> _asyncList = new List<EditorResourceAsync>();
private readonly Queue<MyTuple<string, string>> _pendingList = new Queue<MyTuple<string, string>>();
private void OnResourcesLoad(AsyncOperation operation)
{
@ -379,6 +383,7 @@ public class LoadAssetBundle : MonoBehaviour
OnAssetLoadedFinish(async.bundleName, async.assetName, resourceRequest.asset);
}
}
while (_asyncList.Count < _maxAsyncCount && _pendingList.Count > 0)
{
var task = _pendingList.Dequeue();
@ -398,6 +403,7 @@ public class LoadAssetBundle : MonoBehaviour
using (var fs = File.OpenRead(dataPath))
_resourcesDict = bitFormatter.Deserialize(fs) as Dictionary<string, EditorBundleList>;
}
if (_resourcesDict == null)
_resourcesDict = new Dictionary<string, EditorBundleList>();
}
@ -405,19 +411,20 @@ public class LoadAssetBundle : MonoBehaviour
EditorBundleList list;
if (!_resourcesDict.TryGetValue(bundleName, out list))
{
var assets = AssetDatabase.GetAssetPathsFromAssetBundle(bundleName + AssetConst.bundleVariant).
Where(a => a.StartsWith(assetPathHeader)).
Select(a => a.RemoveExtension().Substring(assetPathHeader.Length + 1)).ToArray();
var assets = AssetDatabase.GetAssetPathsFromAssetBundle(bundleName + AssetConst.bundleVariant)
.Where(a => a.StartsWith(assetPathHeader))
.Select(a => a.RemoveExtension().Substring(assetPathHeader.Length + 1)).ToArray();
if (assets.Length > 0)
{
list = new EditorBundleList(assets);
list = new EditorBundleList(assets);
_resourcesDict[bundleName] = list;
var bitFormatter = new BinaryFormatter();
var dataPath = Application.dataPath.MoveUp().Open(bundleToAssetPath);
var dataPath = Application.dataPath.MoveUp().Open(bundleToAssetPath);
using (var fs = File.OpenWrite(dataPath))
bitFormatter.Serialize(fs, _resourcesDict);
}
}
var path = list != null
? list.GetAssetPath(assetName.ToLower())
: string.Empty;
@ -434,7 +441,7 @@ public class LoadAssetBundle : MonoBehaviour
request.completed += OnResourcesLoad;
}
}
private void LoadForSimulateMode(string bundleName, string assetName, Type assetType)
{
Object asset = null;
@ -484,7 +491,7 @@ public class LoadAssetBundle : MonoBehaviour
#region load init
public static LoadAssetBundle Instance { get; private set; }
// public void UnloadBundle(string bundleName)
// {
// Debug.Log("Unload Bundle: " + bundleName);
@ -512,7 +519,7 @@ public class LoadAssetBundle : MonoBehaviour
// }
return bundleName.RemoveExtension().ToLower();
}
// /// <summary>
// /// 加载一个AssetBundle如果Bundle已经加载就返回true
// /// </summary>
@ -521,7 +528,7 @@ public class LoadAssetBundle : MonoBehaviour
// bundleName = FixBundleName(bundleName);
// AssetManager.instance.Preload(bundleName, this, OnAssetBundleLoaded);
// }
#endregion
#region
@ -530,6 +537,7 @@ public class LoadAssetBundle : MonoBehaviour
/// 预加载场景电影Bundle
/// </summary>
private readonly List<int> _movieId = new List<int>();
private string _preloadScene;
private static string GetMovieName(int movieId)
@ -544,6 +552,7 @@ public class LoadAssetBundle : MonoBehaviour
movieName = movieData.GetResPathbyIndex(profession);
}
}
return movieName;
}
@ -553,7 +562,7 @@ public class LoadAssetBundle : MonoBehaviour
if (!string.IsNullOrEmpty(movieName))
Instance.LoadGameObject(BUNDLE_PATH_MODEL, movieName, null, null);
}
private void RemoveMoviePreload(int movieId)
{
var movieName = GetMovieName(movieId);
@ -564,7 +573,7 @@ public class LoadAssetBundle : MonoBehaviour
Instance.UnloadAsset(bundleName, movieName);
}
}
public void StartPreloadScene(string sceneName, int movieId)
{
#if UNITY_EDITOR
@ -580,6 +589,7 @@ public class LoadAssetBundle : MonoBehaviour
CleanPreloadScene();
_preloadScene = sceneName;
}
if (!_movieId.Contains(movieId))
AddMoviePreload(movieId);
}
@ -591,9 +601,11 @@ public class LoadAssetBundle : MonoBehaviour
RemoveMoviePreload(id);
_movieId.Clear();
}
#endregion
#region loadScene
/// <summary>
/// 是否允许加载场景
/// </summary>
@ -602,7 +614,7 @@ public class LoadAssetBundle : MonoBehaviour
{
return !AssetManager.instance.IsLoadingScene();
}
public static string GetSceneBundleName(string sceneName)
{
var bundleName = "scene/" + sceneName;
@ -615,7 +627,7 @@ public class LoadAssetBundle : MonoBehaviour
if (!AssetUpdateManager.useResources)
AssetManager.instance.UnloadAllScene();
}
/// <summary>
/// 加载主要场景 - 主要场景加载时,会卸载之前的主要场景
/// </summary>
@ -624,21 +636,23 @@ public class LoadAssetBundle : MonoBehaviour
bool load;
if (AssetUpdateManager.useResources)
{
load = true;
load = true;
loadHandle = null;
SceneManager.LoadScene(assetName);
}
else
{
var bundleName = GetSceneBundleName(assetName);
var current = AssetManager.instance.GetCurrentSceneHandle();
var current = AssetManager.instance.GetCurrentSceneHandle();
load = current == null || current.sceneName == assetName;
if (!load)
load = AllowLoadScene();
loadHandle = load ? AssetManager.instance.LoadScene(bundleName, assetName, mode, allowReload: true) : null;
}
return load;
}
#endregion
#region loadGameRes
@ -647,7 +661,7 @@ public class LoadAssetBundle : MonoBehaviour
{
get { return FixBundleName(BUNDLE_PATH_GAMERES); }
}
#endregion
#region LoadTable
@ -660,8 +674,8 @@ public class LoadAssetBundle : MonoBehaviour
}
#if UNITY_EDITOR
public const string tablePath = "Assets/Project3D/BundleData/Tables3D/";
private Dictionary<string, string> _luaFiles;
public const string tablePath = "Assets/Project3D/BundleData/Tables3D/";
private Dictionary<string, string> _luaFiles;
#endif
private TextAsset LoadTextAsset(string bundleName, string assetName)
{
@ -676,10 +690,11 @@ public class LoadAssetBundle : MonoBehaviour
_luaFiles = new Dictionary<string, string>();
var luaPath = "Assets".Open("Project").Open("Script").Open("LuaScripts") + '/';
var scripts = AssetDatabase.GetAllAssetPaths().Where(a => a.StartsWith(luaPath))
.Where(a => AssetConst.textExtension.Equals(Path.GetExtension(a)));
.Where(a => AssetConst.textExtension.Equals(Path.GetExtension(a)));
foreach (var script in scripts)
_luaFiles.Add(Path.GetFileNameWithoutExtension(script).ToLower(), script);
}
string path;
if (_luaFiles.TryGetValue(assetName.ToLower(), out path))
result = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
@ -693,15 +708,21 @@ public class LoadAssetBundle : MonoBehaviour
}
else
Debug.LogError(string.Format("Unable to load TextAsset {1} from bundle {0}!", bundleName, assetName));
return result;
}
#endif
if (AssetManager.instance == null)
{
throw new Exception("AssetManager is not initialized!");
}
result = AssetManager.instance.LoadAssetSync<TextAsset>(bundleName, assetName);
return result;
}
#endregion
#region LoadScript
// 注之前逻辑是和LoadTable在一起但是没有监听过这个AssetBundle是否加载结束的
@ -714,11 +735,11 @@ public class LoadAssetBundle : MonoBehaviour
}
#endregion
#region Atlas Path Convert
private Dictionary<string, string> _pathConvertDict;
private void CreateAtlasConvert()
{
_pathConvertDict = new Dictionary<string, string>();
@ -743,12 +764,13 @@ public class LoadAssetBundle : MonoBehaviour
else
{
LogModule.ErrorLog(string.Format(
"Try to load asset {0} from {1}, while Asset Load Manager hasn't init path conversion yet!",
assetName, bundleName));
"Try to load asset {0} from {1}, while Asset Load Manager hasn't init path conversion yet!",
assetName, bundleName));
}
return bundleName;
}
#endregion
}
@ -768,7 +790,7 @@ public abstract class LoadAssetRequestBase
protected LoadAssetRequestBase(string bundleName, string assetName)
{
this.bundleName = bundleName;
this.assetName = assetName;
this.assetName = assetName;
}
}
@ -865,7 +887,7 @@ public class LoadSpriteRequest : LoadAssetRequestBase
for (var i = 0; i < callBackList.Count; i++)
if (callBackList[i].first)
{
callBackList[i].first.sprite = sprite;
callBackList[i].first.sprite = sprite;
callBackList[i].first.enabled = true;
if (callBackList[i].second != null)
callBackList[i].second.Invoke(true, callBackList[i].first.gameObject);
@ -926,7 +948,7 @@ public class LoadRawImageRequestList
public void AddCallback(string bundleName, string assetName, RawImage rawImage)
{
var index = _callbackList.GetIndexFromList(bundleName, assetName);
var index = _callbackList.GetIndexFromList(bundleName, assetName);
LoadRawImageRequest request;
if (index < 0)
{
@ -970,7 +992,7 @@ public class LoadRawImageRequestList
item.FinishAsset(asset);
}
}
public void Clear()
{
_callbackList.Clear();
@ -984,7 +1006,7 @@ public class LoadSpriteRequestList
public void AddCallback(string bundleName, string assetName, Image image, LoadSpritesCallback callback)
{
bundleName = LoadAssetBundle.FixBundleName(bundleName);
var index = _callbackList.GetIndexFromList(bundleName, assetName);
var index = _callbackList.GetIndexFromList(bundleName, assetName);
LoadSpriteRequest request;
if (index < 0)
{
@ -1041,10 +1063,10 @@ public class LoadAssetRequestList<T> where T : Object
{
private readonly List<LoadAssetRequest<T>> _callbackList = new List<LoadAssetRequest<T>>();
public void AddCallback(string bundleName, string assetName, LoadBundleAssetCallback<T> callback,
Hashtable hashTable)
public void AddCallback(string bundleName, string assetName, LoadBundleAssetCallback<T> callback,
Hashtable hashTable)
{
var index = _callbackList.GetIndexFromList(bundleName, assetName);
var index = _callbackList.GetIndexFromList(bundleName, assetName);
LoadAssetRequest<T> request;
if (index < 0)
{
@ -1101,7 +1123,7 @@ public class LoadAssetRequestList<T> where T : Object
public class LoadWholeBundleRequest
{
public readonly List<LoadAssetBundleFinish> callbackList = new List<LoadAssetBundleFinish>();
public readonly string name;
public readonly string name;
public LoadWholeBundleRequest(string bundleName)
{