【Unity框架】JSON配置类

序列化数据模型

{
    [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 = new Dictionary<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 = new Dictionary<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;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值