using UnityEngine;
using System.Collections;
using LitJson;
using System.IO;
using System.Text;
using UnityEditor;
using System.Collections.Generic;
public class? JsonDcript:MonoBehaviour{
public? TextAsset??? txt;//讀取Asset文件
//創(chuàng)建文件夾路徑
string?? dirpath;
//創(chuàng)建文件路徑
string?? filePath;
voidStart(? ){
dirpath=Application.dataPath+"/JsonFiles";
filePath=dirpath+"/MyJson.txt";
//MergerAndSaveJson( );
//對象的所有屬性(字段),轉(zhuǎn)化為Json格式字符串
//HreoDataModel model=new HreoDataModel( );
//model.Hero_Name="xiaoli";
//model.Hp=100;
//model.Attack=30;
??????? //LitJson
//print(JsonMapper.ToJson(model));
//string?? modelstring=JsonMapper.ToJson(model);
?????? //JsonUtiluty
//print(JsonUtility.ToJson(model));
//將得到的json字符串,依次賦值給指定的數(shù)據(jù)模型對象
//StreamReader?? reader=new?? StreamReader(filePath);
//string?? jsonStr=reader.ReadToEnd( );
//HreoDataModel?? model_2=new? HreoDataModel( );
//model_2=JsonMapper.ToObject(jsonStr);
//model_2=JsonUtility.FromJson(jsonStr);
//foreach( HeroSkillDataModel?? dm? in? model_2.Skill){
//print(dm.passivity);
}
}
//合成Json并寫入文件
public? void?? MergerAndSaveJson( ){
StringBuilder??? strBuilder=new? StringBuilder( );
JsonWriter? writer=new?? JsonWriter(strBuilder);
writer.WriteObjectStart( );//{
writer.WritePropertyName("Hero_Name");
writer.Write("hero");
writer.WritePropertyName("Hp");
writer.Write(100);
writer.WritePropertyName("Attack");
writer.Write(50);
writer.WritePropertyName("Skill");//數(shù)組
writer.WriteArrayStart( );//[
writer.WriteObjectStart( );//{
writer.WritePropertyName("被動技能");
writer.Write("出血");
writer.WriteObjectEnd();//}
writer.WriteObjectStart( );//{
writer.WritePropertyName("Q技能");
writer.Write("大殺四方");
writer.WritePropertyName("冷卻時間");
writer.Write("7/6/5/4/3");
writer.WritePropertyName("消耗");
writer.Write(30);
writer.WriteObjectEnd( );//}
writer.WriteObjectStart( );
writer.WritePropertyName("W技能");
writer.Write("殘忍打擊");
writer.WritePropertyName("冷卻時間");
writer.Write("8/7/6/2/1");
writer.WritePropertyName("消耗");
writer.Write(50);
writer.WriteObjectEnd( );
writer.WriteObjectStart( );
writer.WritePropertyName("E技能");
writer.Write("無情鐵手");
writer.WritePropertyName("冷卻時間");
writer.Write("9/5/3/1");
writer.WritePropertyName("消耗");
writer.Write(60);
writer.WriteObjectEnd( );
writer.WriteArrayEnd( );//]
writer.WriteObjectEnd( );//}
Debug.Log(strBuilder);
DirectoryInfo? dir=new? DirectoryInfo(dirpath);? //創(chuàng)建文件的目錄
if(!dir.Exists){? //檢查文件夾是否存在
Directory.CreateDirectory(dirpath);
//如果當前程序運行在Unity軟件中才會執(zhí)行下面語句
#ifUNITY_EDITOR
AssetDatabase.Refresh( );//刷新
#endif
}
//把Json字符串寫入Txt
StreamWriter? sw;
if(File.Exists(filePath)){
//如果文件存在,就繼續(xù)往文件中添加內(nèi)容
sw=File.AppendText(filePath);
}else{
//如果文件不存在,則創(chuàng)建文件
sw=File.CreateText(filePath);
}
sw.WriteLine(strBuilder);
sw.Close( );//關(guān)閉輸入流
#ifUNITY_EDITOR
AssetDatabase.Refresh( );
#endif
}
public void? ReadJson( ){? //新版本
//從文件讀取Json字符串
if(File.Exists(filePath)){
StreamReader? reader=new? StreamReader(filePath);
string? jsonStr=reader.ReadToEnd( );//從頭讀到尾
//將json格式的字符串轉(zhuǎn)換為json數(shù)據(jù),然后操作json數(shù)據(jù)
//LitJson
JsonData? jd=JsonMapper.ToObject(jsonStr);
foreach(JsonData? josnData? in?? jd["Skill"]){
foreach(string? key? in?? josnData.Keys){
Debug.Log(key+":"+josnData[key]);
}
}
}
}
public?? T?? JsonToModel <T>(string? path)where T:class,new( ){//泛型方法
if(File.Exists(path)){
StreamReader? sr=new? StreamReader(path);
string?? jsonStr=sr.ReadToEnd( );
T?? model=JsonMapper.ToObject (jsonStr) as? T;
return?? model;
}
return?? null;
}
}
usingUnityEngine;
usingSystem.Collections;
usingSystem.Collections.Generic;
public class?? HreoDataModel{
public?? string?? Hero_Name;
public??? int??? Hp;
public??? int???? Attack;
public?? List <HeroSkillDataModel>Skill;
public? override?? string??? ToString( )
{
return? Hero_Name+", "+Hp.ToString( );
}
}
usingUnityEngine;
usingSystem.Collections;
public? class? HeroSkillDataModel{
public? string? passivity="";
public?? string? Q;
public?? string? W;
public??? string? E;
public??? string CoolTime;
public? string??? consume;
}
MVC
usingUnityEngine;?
usingSystem.Collections;
usingSystem.IO;
usingLitJson;
public class UIModel{
public delegate void Data UpdateDelegate( );
public event? DataUpdateDelegate?? dataUpdateEvent;
public string? mPlayer_name;
public? string? mPlayer_money;
public? string? mPlayer_exp;
public? string? mPlayer_race;
public? string? mPlayer_hp;
public? string? mPlayer_attack;
public? string? mPlayer_defense;
//自定義初始化
//重寫toString( )
public? void?? JsonToModel(string? path){
if(File.Exists(path)){
StreamReader? sr=new? StreamReader(path);
string? jsonStr=sr.ReadToEnd( );
UIModel? model=JsonMapper.ToObject<UIModel>(jsonStr);
this.mPlayer_name=model.mPlayer_name;
this.mPlayer_money=model.mPlayer_money;
this.mPlayer_exp=model.mPlayer_exp;
this.mPlayer_race=model.mPlayer_race;
this.mPlayer_hp=model.mPlayer_hp;
this.mPlayer_attack=model.mPlayer_attack;
this.mPlayer_defense=model.mPlayer_defense;
dataUpdateEvent( );
}
}
}
usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI;
publicclassUIManager:MonoBehaviour{
public Text nameText;
public Text moneyText;
public Text expText;
public Text raceText;
public Text hpText;
public Text attackText;
public Text? defenseText;
}
usingUnityEngine;
usingSystem.Collections;
publicclassGameController:MonoBehaviour{
UIManager?? manager;
UIModel??? model;
void Start( ){
manager=GameObject.Find("UIManager").GetComponent<UIManager>( );
model=new UIModel( );
model.dataUpdateEvent+=UpdataView;
}
void? Update( ){
if(Input.GetKeyDown(KeyCode.U)){
model.JsonToModel(Application.dataPath+"/JsonFiles/MyJson.txt");
}
}
void?? UpdataView( ){
manager.nameText.text=model.mPlayer_name;
manager.moneyText.text=model.mPlayer_money;
manager.expText.text=model.mPlayer_exp;
manager.raceText.text=model.mPlayer_race;
manager.hpText.text=model.mPlayer_hp;
manager.attackText.text=model.mPlayer_attack;
manager.defenseText.text=model.mPlayer_defense;
}
}