207 lines
6.3 KiB
C#
207 lines
6.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using LuaInterface;
|
|
using GameCore;
|
|
using System.IO;
|
|
using ResUpdate;
|
|
|
|
namespace GameLogic {
|
|
/// <summary>
|
|
/// 资源管理器
|
|
/// </summary>
|
|
public class ResourcesManager : Singleton<ResourcesManager>
|
|
{
|
|
public bool isLoading;
|
|
ResMgr.ResourcesManager resMgr = ResMgr.ResourcesManager.Instance;
|
|
|
|
/// <summary>
|
|
/// 初始化
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同步加载资源
|
|
/// </summary>
|
|
/// <typeparam name="T">资源类型</typeparam>
|
|
/// <param name="assetName">资源名</param>
|
|
/// <returns></returns>
|
|
[NoToLua]
|
|
public T LoadAsset<T>(string assetName) where T : Object
|
|
{
|
|
return resMgr.LoadAsset<T>(assetName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同步加载资源
|
|
/// </summary>
|
|
/// <param name="assetName">资源名</param>
|
|
/// <returns></returns>
|
|
public Object LoadAsset(string assetName)
|
|
{
|
|
return resMgr.LoadAsset<Object>(assetName);
|
|
}
|
|
/// <summary>
|
|
/// 同步加载Sprite资源
|
|
/// </summary>
|
|
/// <param name="assetName">资源名</param>
|
|
/// <returns></returns>
|
|
public Sprite LoadSpriteAsset(string assetName)
|
|
{
|
|
return resMgr.LoadAsset<Sprite>(assetName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步加载资源
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="assetName"></param>
|
|
/// <param name="action"></param>
|
|
[NoToLua]
|
|
public void LoadAssetAsync<T>(string assetName, UnityAction<string, T> action) where T : Object
|
|
{
|
|
resMgr.LoadAssetAsync<T>(assetName, action);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步加载资源
|
|
/// </summary>
|
|
/// <param name="assetName"></param>
|
|
/// <param name="action"></param>
|
|
[NoToLua]
|
|
public void LoadAssetAsync(string assetName, UnityAction<string, Object> action)
|
|
{
|
|
resMgr.LoadAssetAsync<Object>(assetName, action);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Lua异步加载资源
|
|
/// </summary>
|
|
/// <param name="assetName"></param>
|
|
/// <param name="luaFunction"></param>
|
|
public void LoadAssetAsync(string assetName, LuaFunction luaFunction)
|
|
{
|
|
resMgr.LoadAssetAsync<Object>(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);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 卸载资源
|
|
/// </summary>
|
|
/// <param name="assetName"></param>
|
|
public void UnLoadAsset(string assetName)
|
|
{
|
|
resMgr.UnLoadAsset(assetName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 卸载资源
|
|
/// </summary>
|
|
/// <param name="assetName"></param>
|
|
public void UnLoadAsset(string assetName, int count)
|
|
{
|
|
resMgr.UnLoadAsset(assetName, count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 卸载游戏
|
|
/// </summary>
|
|
public void UnLoadGame()
|
|
{
|
|
resMgr.UnLoadAll();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 卸载没有使用的资源和AssetBundle
|
|
/// </summary>
|
|
public void UnLoadUnUseAssetAndAssetBundle()
|
|
{
|
|
resMgr.UnLoadUnUseAssetAndAssetBundle();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否拥有某个资源
|
|
/// </summary>
|
|
/// <param name="asset">资源名</param>
|
|
/// <returns></returns>
|
|
public bool HaveAsset(string asset)
|
|
{
|
|
return resMgr.HaveAsset(asset);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 卸载掉所有资源(平台和游戏)
|
|
/// </summary>
|
|
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<bool, string> action)
|
|
{
|
|
resMgr.LoadStreamingText(name, null, action);
|
|
}
|
|
}
|
|
|
|
}
|