{[System.Serializable]//序列化
public class AssetPathConfig{
public AssetNameAndPath[] AssetPathConfiguration;}[System.Serializable]
public class AssetNameAndPath{
public string AssetName;
public string AssetPath;}}
JSON解析类
public class JsonParseManager: Singleton<JsonParseManager>{/// <summary>/// 解析资源路径配置的Json/// </summary>
public AssetPathConfig ParseAssetPath(){//加载Json配置文件
TextAsset jsonText = AssetManager.GetInstance().
GetAssetByPath<TextAsset>(
SystemDefine.AssetPathConfiguration);//解析Json
AssetPathConfig configuration = JsonUtility.FromJson<AssetPathConfig>(jsonText.text);//返回return configuration;}
JSON数据管理类(数据对外API)
using System;
using System.Collections.Generic;
using Frame.DataModel.JsonDataModel;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Frame.Tool
{
public class AssetManager: Singleton<AssetManager>{
private AssetManager(){
assetCache =newDictionary<string, Object>();}//资源路径配置字典
private Dictionary<string, string> assetsPathDic;/// <summary>/// 资源缓存/// </summary>
private Dictionary<string, Object> assetCache;/// <summary>/// 通过资源名称获取资源/// </summary>/// <param name="assetName"></param>/// <typeparam name="T"></typeparam>[Obsolete("请更换到GetAssetByPath方法去获取资源",true)]
public T GetAsset<T>(string assetName) where T : Object
{//通过名称获取到该资源的Resources下的路径
string assetPath = ReflectionTool.GetInstance().
GetStaticFieldValue<string>(typeof(SystemDefine), assetName);//通过路径加载资源return Resources.Load<T>(assetPath);}/// <summary>/// 通过路径加载资源/// </summary>/// <param name="assetPath"></param>/// <typeparam name="T"></typeparam>/// <returns></returns>
public T GetAssetByPath<T>(string assetPath) where T : Object
{if(!assetCache.ContainsKey(assetPath)){//通过路径加载资源
assetCache[assetPath]= Resources.Load<T>(assetPath);}return assetCache[assetPath] as T;}
public T GetAssetByName<T>(string assetName) where T : Object
{//获取路径
string assetPath =GetAssetPathByName(assetName);//通过路径加载资源return GetAssetByPath<T>(assetPath);}/// <summary>/// 加载资源路径配置的字典/// </summary>
private void LoadAssetPathDictionary(){//加载配置文件
AssetPathConfig _assetPathConfig = JsonParseManager.GetInstance().ParseAssetPath();//实例化字典
assetsPathDic =newDictionary<string, string>();//将配置文件转存到字典里for(int i =0; i < _assetPathConfig.AssetPathConfiguration.Length; i++){//逐条添加到字典
assetsPathDic.Add(
_assetPathConfig.AssetPathConfiguration[i].AssetName,
_assetPathConfig.AssetPathConfiguration[i].AssetPath);}}/// <summary>/// 通过资源名称获取资源路径/// </summary>/// <param name="assetName"></param>/// <returns></returns>
public string GetAssetPathByName(string assetName){//如果配置文件不存在if(assetsPathDic ==null){//加载LoadAssetPathDictionary();}//返回结果return assetsPathDic[assetName];}}}
反射工具类
using System;
using System.Reflection;
namespace Frame.Tool
{/// <summary>/// 反射工具/// </summary>
public class ReflectionTool: Singleton<ReflectionTool>{
private ReflectionTool(){}/// <summary>/// 获取静态(或常数)字段的值/// </summary>/// <param name="fieldName"></param>/// <returns></returns>
public T GetStaticFieldValue<T>(Type type, string fieldName){//获取字段信息
FieldInfo fieldInfo = type.GetField(fieldName);//获取字段的值return(T)fieldInfo.GetValue(null);}}}
反射单例模式
using System;
namespace Frame.Tool
{
public class Singleton<T>{//单例
private static T instance;//获取单例
public static T GetInstance(){if(instance ==null){
instance =(T)Activator.CreateInstance(typeof(T),true);}return instance;}}}
序列化数据模型{ [System.Serializable]//序列化 public class AssetPathConfig { public AssetNameAndPath[] AssetPathConfiguration; } [System.Serializable] public class AssetNameAndPath { public string AssetName; publ