using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Framework {
/// <summary>
/// 资源系统
/// </summary>
public sealed class AssetSystem : Singleton<AssetSystem> {
/// <summary>
/// 缓存目录
/// </summary>
public static readonly string PAHT_CACHE = Application.persistentDataPath + "/Cache";
private const string LOG_NULL = "Load complete callback is null ";
/// <summary>
/// 加载队列
/// </summary>
private Queue<LoadInfo> m_LoadQueue = new Queue<LoadInfo> ();
private Dictionary<string, GameObject> m_AssetMap = new Dictionary<string, GameObject> ();
/// <summary>
/// 加载协程
/// </summary>
/// <param name="loadInfo">加载信息</param>
/// <returns></returns>
private static IEnumerator LoadCoroutine (LoadInfo loadInfo) {
ResourceRequest request = null;
request = Resources.LoadAsync (loadInfo.Name, loadInfo.AssetType);
loadInfo.Request = request;
yield return request;
loadInfo.LoadCallback.Invoke (request.asset, loadInfo.Arg);
if (Instance.m_LoadQueue.Count > 0) {
loadInfo = Instance.m_LoadQueue.Dequeue ();
StartLoad (loadInfo);
}
}
/// <summary>
/// 开始加载
/// </summary>
/// <param name="loadInfo">加载信息</param>
private static void StartLoad (LoadInfo loadInfo) {
Core.Instance.StartCoroutine (LoadCoroutine (loadInfo));
}
/// <summary>
/// 加载资源
/// </summary>
/// <param name="assetName">资源文件名</param>
/// <param name="name">资源名字</param>
/// <param name="type">资源类型</param>
/// <returns></returns>
public static UnityEngine.Object Load (string assetName, string name = null, Type type = null) {
return Resources.Load (assetName) as UnityEngine.Object;
}
/// <summary>
/// 加载所有资源
/// </summary>
/// <param name="assetName">资源文件名</param>
/// <param name="type">资源类型</param>
/// <returns>返回资源</returns>
public static UnityEngine.Object[] LoadAll (string assetName, Type type = null) {
return Resources.LoadAll (assetName);
}
/// <summary>
/// 异步加载资源
/// </summary>
/// <param name="loadComplete">加载完成回调</param>
/// <param name="assetName">资源文件名</param>
/// <param name="name">资源名</param>
/// <param name="type">资源类型</param>
/// <param name="arg">加载参数</param>
/// <returns>返回加载请求</returns>
public static LoadInfo LoadAsync (Action<object, object> loadComplete, string assetName, string name = null, Type type = null, object arg = null) {
//回调检测
if (loadComplete == null) {
Debug.LogError (LOG_NULL);
return null;
}
if (name == null) {
name = assetName.Substring (assetName.LastIndexOf ('/') + 1);
name = name.Remove (name.LastIndexOf ('.'));
}
if (type == null) {
type = typeof (GameObject);
}
LoadInfo loadInfo = new LoadInfo () {
LoadCallback = loadComplete,
AssetName = assetName,
Name = name,
AssetType = type,
Arg = arg,
};
if (Instance.m_LoadQueue.Count > 0) {
Instance.m_LoadQueue.Enqueue (loadInfo);
} else {
StartLoad (loadInfo);
}
return loadInfo;
}
/// <summary>
/// 卸载资源
/// </summary>
/// <param name="assetName">资源文件名</param>
/// <param name="unloadAll">是否卸载全部资源</param>
public static void UnLoad (string assetName, bool unloadAll) {
GameObject asset = null;
if (!Instance.m_AssetMap.TryGetValue (assetName, out asset)) {
return;
}
Resources.UnloadAsset (asset);
Instance.m_AssetMap.Remove (assetName);
}
/// <summary>
/// 清除所有资源
/// </summary>
public static void Clear () {
foreach (GameObject gameobject in Instance.m_AssetMap.Values) {
Resources.UnloadAsset (gameobject);
}
Instance.m_AssetMap.Clear ();
}
/// <summary>
/// 加载信息
/// </summary>
public sealed class LoadInfo {
/// <summary>
/// 加载回调
/// </summary>
public Action<object, object> LoadCallback { get; internal set; }
/// <summary>
/// 资源文件名
/// </summary>
public string AssetName { get; internal set; }
/// <summary>
/// 资源名
/// </summary>
public string Name { get; internal set; }
/// <summary>
/// 资源类型
/// </summary>
public Type AssetType { get; internal set; }
/// <summary>
/// 加载参数
/// </summary>
public object Arg { get; internal set; }
/// <summary>
/// 资源文件请求
/// </summary>
public ResourceRequest Request { get; internal set; }
}
}
}