using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using LuaInterface; using GameCore; using System.IO; using ResUpdate; namespace GameLogic { /// /// 资源管理器 /// public class ResourcesManager : Singleton { public bool isLoading; ResMgr.ResourcesManager resMgr = ResMgr.ResourcesManager.Instance; /// /// 初始化 /// public void Initialize(bool isReleaseVer) { //资源路径配置 ResMgr.ResConfig.PersistentDataPath = AppConst.PersistentDataPath; ResMgr.ResConfig.StreamPath = AppConst.StreamPath; resMgr.Init(isReleaseVer); Debug.LogFormat("================>ResourcesManager.Initialize,isReleaseVer:{0}",isReleaseVer); } /// /// 同步加载资源 /// /// 资源类型 /// 资源名 /// [NoToLua] public T LoadAsset(string assetName) where T : Object { return resMgr.LoadAsset(assetName); } /// /// 同步加载资源 /// /// 资源名 /// public Object LoadAsset(string assetName) { return resMgr.LoadAsset(assetName); } /// /// 同步加载Sprite资源 /// /// 资源名 /// public Sprite LoadSpriteAsset(string assetName) { return resMgr.LoadAsset(assetName); } /// /// 异步加载资源 /// /// /// /// [NoToLua] public void LoadAssetAsync(string assetName, UnityAction action) where T : Object { resMgr.LoadAssetAsync(assetName, action); } /// /// 异步加载资源 /// /// /// [NoToLua] public void LoadAssetAsync(string assetName, UnityAction action) { resMgr.LoadAssetAsync(assetName, action); } /// /// Lua异步加载资源 /// /// /// public void LoadAssetAsync(string assetName, LuaFunction luaFunction) { resMgr.LoadAssetAsync(assetName, (name, obj) => { if (luaFunction != null) { XDebug.Log.l("ResourcesManager LoadAssetAsync luaFunc"); luaFunction.Call(name, obj); } else { XDebug.Log.l("ResourcesManager LoadAssetAsync luaFunc ==null"); } }); } public void PreLoadAssetAsync(string assetName, LuaFunction luaFunc) { //XDebug.Log.l("预加载资源:" + assetName); if (File.Exists(AppConst.PersistentDataPath + assetName) && PlayerPrefs.GetInt(assetName + "_IsPreLoad", 0) == 1) { //XDebug.Log.l("预加载资源已存在"); if (luaFunc != null) { luaFunc.Call(true); } }else { string downLoadURL = VersionManager.Instance.GetVersionInfo("resUrl") + AppConst.PlatformPath + "/"; //XDebug.Log.l("预加载资源路径:"+downLoadURL); ResourceDownloadManager.Instance.StartDownload(assetName, downLoadURL, "", (string name, DownLoadProgress dp) => { //Debug.LogFormat("预加载资源进度:{0}/{1}", dp.Size, dp.TotalSize); }, (string name, bool isOk) => { //XDebug.Log.l("预加载资源完成:" + isOk); PlayerPrefs.SetInt(assetName + "_IsPreLoad", 1); if (luaFunc != null) { luaFunc.Call(isOk); } }); } } /// /// 卸载资源 /// /// public void UnLoadAsset(string assetName) { resMgr.UnLoadAsset(assetName); } /// /// 卸载资源 /// /// public void UnLoadAsset(string assetName, int count) { resMgr.UnLoadAsset(assetName, count); } /// /// 卸载游戏 /// public void UnLoadGame() { resMgr.UnLoadAll(); } /// /// 卸载没有使用的资源和AssetBundle /// public void UnLoadUnUseAssetAndAssetBundle() { resMgr.UnLoadUnUseAssetAndAssetBundle(); } /// /// 是否拥有某个资源 /// /// 资源名 /// public bool HaveAsset(string asset) { return resMgr.HaveAsset(asset); } /// /// 卸载掉所有资源(平台和游戏) /// public void UnLoadAll() { resMgr.UnLoadAll(); } public void Reset() { } public void LoadStreamingTexture(string name, LuaFunction luaFunction) { resMgr.LoadStreamingTexture(name, luaFunction); } public void LoadStreamingText(string name, LuaFunction luaFunction) { resMgr.LoadStreamingText(name, luaFunction, null); } [NoToLua] public void LoadStreamingText(string name, UnityAction action) { resMgr.LoadStreamingText(name, null, action); } } }