1.AssetLoader修改
/****************************************************
文件:AssetLoader.cs
作者:赵深圳
邮箱: 1329346609@qq.com
日期:2022/5/9 10:39:33
功能:资源加载器
*****************************************************/
using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// 资源加载器
/// </summary>
public class AssetLoader : Singleton<AssetLoader>
{
/// <summary>
/// 加载模块对应的全局AssetBundle资源管理文件
/// </summary>
/// <param name="moduleConfig">模块名字</param>
/// <param name="onCompelte">加载完成后的回调</param>
public async Task<ModuleABConfig> LoadAssetBundleConfig(string moduleName)
{
#if UNITY_EDITOR
if (GlobalConfig.BundleMode == false)
{
return null;
}
else
{
return await LoadAssetBundleConfig_Runtime(moduleName);
}
#else
return await LoadAssetBundleConfig_Runtime(moduleName);
#endif
}
/// <summary>
/// 加载本地的模块的AB配置文件
/// </summary>
/// <param name="moudleName">模块的名字</param>
/// <returns></returns>
public async Task<ModuleABConfig> LoadAssetBundleConfig_Runtime(string moudleName)
{
string url = Application.streamingAssetsPath + "/" + moudleName + "/" + moudleName.ToLower() + ".json";
UnityWebRequest request = UnityWebRequest.Get(url);
await request.SendWebRequest();
if (string.IsNullOrEmpty(request.error))
{
return LitJson.JsonMapper.ToObject<ModuleABConfig>(request.downloadHandler.text);
}
return null;
}
}
2.Downloader修改
/****************************************************
文件:Downloader.cs
作者:赵深圳
邮箱: 1329346609@qq.com
日期:2022/5/9 10:39:59
功能:资源下载器
*****************************************************/
using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// 资源下载器
/// </summary>
public class Downloader : Singleton<Downloader>
{
/// <summary>
/// 根据模块的配置,下载对应模块
/// </summary>
/// <param name="moduleConfig">模块的配置对象</param>
public async Task<bool> Download(ModuleConfig moduleConfig)
{
UnityWebRequest request = UnityWebRequest.Get("");
await request.SendWebRequest();
if (string.IsNullOrEmpty(request.error))
{
return true;
}
return false;
}
}
3.ModuleManager修改
/****************************************************
文件:ModuleManager.cs
作者:赵深圳
邮箱: 1329346609@qq.com
日期:2022/5/9 10:40:6
功能:模块管理器
*****************************************************/
using System;
using System.Threading.Tasks;
using UnityEngine;
/// <summary>
/// 模块管理器
/// </summary>
public class ModuleManager : Singleton<ModuleManager>
{
/// <summary>
/// 加载一个模块 唯一对外API函数
/// </summary>
/// <param name="mooduleConfig">模块的配置文件</param>
public async Task<bool> Load(ModuleConfig moduleConfig)
{
if (GlobalConfig.HotUpdate == false)
{
if (GlobalConfig.BundleMode == false)
{
return true;
}
else
{
ModuleABConfig moduleABConfig = await AssetLoader.Instance.LoadAssetBundleConfig(moduleConfig.moduleName);
if (moduleABConfig != null)
{
Log.ZSZLog(LogCategory.Resource,"模块包含的AB包总数量:"+ moduleABConfig.BundleInfoDic.Count);
return true;
}
else
{
return false;
}
}
}
else
{
return await Downloader.Instance.Download(moduleConfig);
}
}
}
4.GameEntry修改
/****************************************************
文件:GameEntry.cs
作者:赵深圳
邮箱: 1329346609@qq.com
日期:2022/5/9 10:43:14
功能:游戏启动入口
*****************************************************/
using UnityEngine;
public class GameEntry : MonoBehaviour
{
public static GameEntry Instance;
private async void Awake()
{
InitGlobal();
//启动模块
ModuleConfig launchModule = new ModuleConfig
{
moduleName="launch",
moduleVersion="202205091051",
moduleUrl="127.0.0.1"
};
bool result= await ModuleManager.Instance.Load(launchModule);
if (result)
{
Log.ZSZLog(LogCategory.Resource,"Lua 代码开始");
}
}
private void InitGlobal()
{
Instance = this;
GlobalConfig.HotUpdate = false;
GlobalConfig.BundleMode = false;
DontDestroyOnLoad(this.gameObject);
}
}