01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /*** * * Title: "SUIFW" UI框架项目 * 主题: 演示Unity 对Json 解析API using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Test { public class TestUnityJson : MonoBehaviour { void Start () { Hero heroObj= new Hero(); heroObj.Name = "郭靖" ; heroObj.MyLevel = new Level() {HeroLevel = 800}; //相当于如下写法 //Level lev=new Level(); //lev.HeroLevel = 800; //heroObj.MyLevel = lev; //方法1: Json 序列化工作(对象--> 文件) string strHeroInfo = JsonUtility.ToJson(heroObj); Debug.Log( "测试1: 得到的序列化后的字符串=" +strHeroInfo); //方法2: 反序列化(Json文件--> 对象) Hero heroInfo2 = JsonUtility.FromJson<Hero>(strHeroInfo); Debug.Log( "测试2:得到反序列化对象数值,名称: " +heroInfo2.Name+ " 等级: " +heroInfo2.MyLevel.HeroLevel); //方法3: 测试覆盖反序列化。 Hero hero= new Hero(); hero.Name = "杨过" ; hero.MyLevel = new Level() {HeroLevel = 500}; //Json 序列化 string heroInfo3 = JsonUtility.ToJson(hero); //测试覆盖反序列化 JsonUtility.FromJsonOverwrite(heroInfo3, heroObj); Debug.Log( "测试3, 得到再次反序列化覆盖的对象信息,名称: " +heroObj.Name+ " 等级: " +heroObj.MyLevel.HeroLevel); } } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /*** * * Title: "SUIFW" UI框架项目 * 主题: 对于Unity中Resource 目录下的Json 文件的解析Demo * Description: * 功能: yyy * * Date: 2017 * Version: 0.1版本 * Modify Recoder: * * */ using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Test { public class TestUnityJson2 : MonoBehaviour { void Start () { //提取文件,得到字符串数据 TextAsset TaObj=Resources.Load<TextAsset>( "People" ); //反序列化 文件-->对象 PersonInfo perInfo=JsonUtility.FromJson<PersonInfo>(TaObj.text); //显示对象中数据 foreach (People per in perInfo.People) { Debug.Log( " " ); Debug.Log( string .Format( "name={0},Age={1}" ,per.Name,per.Age)); } } } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | /*** * * Title: "SUIFW" UI框架项目 * 主题: 通用配置管理器接口 * Description: * 功能: * 基于“键值对”配置文件的通用解析 * * Date: 2017 * Version: 0.1版本 * Modify Recoder: * * */ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace SUIFW { public interface IConfigManager { /// <summary> /// 只读属性: 应用设置 /// 功能: 得到键值对集合数据 /// </summary> Dictionary< string , string > AppSetting { get ; } /// <summary> /// 得到配置文件(AppSeting)最大的数量 /// </summary> /// <returns></returns> int GetAppSettingMaxNumber(); } [Serializable] internal class KeyValuesInfo { //配置信息 public List<KeyValuesNode> ConfigInfo = null ; } [Serializable] internal class KeyValuesNode { //键 public string Key = null ; //值 public string Value = null ; } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /*** * * Title: "SUIFW" UI框架项目 * 主题: Json 解析异常 * Description: * 功能:专门负责对于JSon 由于路径错误,或者Json 格式错误造成的异常,进行捕获。 * * Date: 2017 * Version: 0.1版本 * Modify Recoder: * * */ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace SUIFW { public class JsonAnlysisException : Exception { public JsonAnlysisException() : base (){} public JsonAnlysisException( string exceptionMessage) : base (exceptionMessage){} } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | /*** * * Title: "SUIFW" UI框架项目 * 主题:基于Json 配置文件的“配置管理器” * Description: * 功能: * * Date: 2017 * Version: 0.1版本 * Modify Recoder: * * */ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace SUIFW { public class ConfigManagerByJson : IConfigManager { //保存(键值对)应用设置集合 private static Dictionary< string , string > _AppSetting; /// <summary> /// 只读属性: 得到应用设置(键值对集合) /// </summary> public Dictionary< string , string > AppSetting { get { return _AppSetting; } } /// <summary> /// 构造函数 /// </summary> /// <param name="jsonPath">Json配置文件路径</param> public ConfigManagerByJson( string jsonPath) { _AppSetting= new Dictionary< string , string >(); //初始化解析Json 数据,加载到(_AppSetting)集合。 InitAndAnalysisJson(jsonPath); } /// <summary> /// 得到AppSetting 的最大数值 /// </summary> /// <returns></returns> public int GetAppSettingMaxNumber() { if (_AppSetting!= null && _AppSetting.Count>=1) { return _AppSetting.Count; } else { return 0; } } /// <summary> /// 初始化解析Json 数据,加载到集合众。 /// </summary> /// <param name="jsonPath"></param> private void InitAndAnalysisJson( string jsonPath) { TextAsset configInfo = null ; KeyValuesInfo keyvalueInfoObj = null ; //参数检查 if ( string .IsNullOrEmpty(jsonPath)) return ; //解析Json 配置文件 try { configInfo = Resources.Load<TextAsset>(jsonPath); keyvalueInfoObj=JsonUtility.FromJson<KeyValuesInfo>(configInfo.text); } catch { throw new JsonAnlysisException(GetType() + "/InitAndAnalysisJson()/Json Analysis Exception ! Parameter jsonPath=" + jsonPath); } //数据加载到AppSetting 集合中 foreach (KeyValuesNode nodeInfo in keyvalueInfoObj.ConfigInfo) { _AppSetting.Add(nodeInfo.Key,nodeInfo.Value); } } } } |
联系客服