You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.6 KiB
58 lines
1.6 KiB
using Newtonsoft.Json; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.IO; |
|
using UnityEngine; |
|
using Object = UnityEngine.Object; |
|
|
|
public class ResourceManager |
|
{ |
|
// 资源池 |
|
private static Dictionary<string, Object> AssetPool = new Dictionary<string, Object>(); |
|
/// <summary> |
|
/// 读取数据 |
|
/// </summary> |
|
/// <typeparam name="T">JSON要转成的类型</typeparam> |
|
/// <param name="fileName">Json文件路径</param> |
|
/// <returns></returns> |
|
public static T LoadJson<T>(string fileName) |
|
{ |
|
string json = null; |
|
string path = Path.Combine(Application.streamingAssetsPath, fileName); |
|
if (File.Exists(path)) |
|
{ |
|
json = File.ReadAllText(path); |
|
} |
|
T data = JsonConvert.DeserializeObject<T>(json); |
|
return data; |
|
} |
|
/// <summary> |
|
/// 初始化 |
|
/// </summary> |
|
public static void LoadAsset<T>(string name,Action<T> loadAssetCallback) |
|
where T : Object |
|
{ |
|
T t = null; |
|
if (AssetPool.ContainsKey(name)) |
|
{ |
|
t = AssetPool[name] as T; |
|
loadAssetCallback(t); |
|
} |
|
else |
|
{ |
|
AssetBundleManager.Instance.StartCoroutine(AssetBundleManager.Instance.Load<T>("equipimage.unity3d", name, loadAssetCallback)); |
|
} |
|
} |
|
public static void AddAsset(string name, Object asset) |
|
{ |
|
if (!AssetPool.ContainsKey(name)) |
|
{ |
|
AssetPool.Add(name, asset); |
|
} |
|
} |
|
public static void Dispose() |
|
{ |
|
AssetPool = null; |
|
GC.Collect(); |
|
} |
|
}
|
|
|