公司最近的一个项目 需要保持一个模型,该模型由许多模型拼凑,于是使用json保持所有模型的transform信息 以及父子关系 ,只要使用了litjosn处理 类的信息
定义一个类保持所有信息,定义另一个类保持prefab的信息
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using MiniJSON;
using System.IO;
using LitJson;
using System;
using UnityEngine.SceneManagement;
public class ChildPrefab
{
public string parent { get; set; }
public string path { get; set; }
public string childID { get; set; }
public string posx { get; set; }
public string posy { get; set; }
public string posz { get; set; }
public string Roax { get; set; }
public string Roay { get; set; }
public string Roaz { get; set; }
public string Scalx { get; set; }
public string Scaly { get; set; }
public string Scalz { get; set; }
public override string ToString()
{
return string.Format("parent:{0}",parent);
}
}
public class GamePrefab
{
public string title { get; set; }
public string time { get; set; }
public string metarial { get; set; }
public string id { get; set; }
public string tuceng { get; set; }
public string tucengID { get; set; }
private static GamePrefab gp;
public List<ChildPrefab> childList { get; set; }
public static GamePrefab GetGP()
{
if (gp == null)
gp = new GamePrefab();
return gp;
}
public static void SetGP(GamePrefab gpparam)
{
gp = gpparam;
}
public List<ChildPrefab> getList()
{
if(childList==null)
childList = new List<ChildPrefab>();
return childList;
}
public override string ToString()
{
return string.Format("tuceng: {0}, time: {1}, id: {2}, SkillList: {3}", tuceng, time, id , childList[0]);
}
}
public class ConfigLoader : MonoBehaviour
{
//不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
public static readonly string PathURL = Application.dataPath + "/StreamingAssets/";
static FileStream fs;
static StreamWriter sw;
public static ConfigLoader _instance;
public static ConfigLoader Instance() { return _instance; }
// Use this for initialization
void Start()
{
if(_instance==null)
_instance = this;
//ParseJson();
//InsertJson();
// getChildDocument();
//Test();
}
void Test()
{
GamePrefab gp = new GamePrefab();
gp.time = "asdfadf";
gp.id = "123";
gp.metarial = "材质";
gp.title = "提莫";
gp.tuceng = "这个是图层";
gp.childList = new List<ChildPrefab>();
ChildPrefab cp = new ChildPrefab();
cp.parent = "123123";
cp.path = "12323";
gp.childList.Add(cp);
InsertJson(gp);
}
// 解析
public void ParseJson(string filename)
{
StreamReader sr = File.OpenText(PathURL + filename);
string input = sr.ReadToEnd();
sr.Dispose();
sr.Close();
GamePrefab jsondata = JsonMapper.ToObject<GamePrefab>(input);
CakeCtrl.GetSingleton().JsonCreate(jsondata);
}
public void InsertJson(GamePrefab gp)
{
Debug.Log(gp+ " ");
gp.time = DateTime.Now.ToString("MM-dd-HH-mm");
string json = JsonMapper.ToJson(gp);
CreateFile(json);
Debug.Log(json);
}
// 写入json
public void CreateFile(string content)
{
string name = DateTime.Now.ToString("MM-dd-HH-mm") + ".json";
if (File.Exists(PathURL + name))
{
File.Delete(PathURL + name);
}
fs = new FileStream(PathURL + name, FileMode.OpenOrCreate, FileAccess.Write);
sw = new StreamWriter(fs);
sw.Write(content);
fs.Flush();
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
}
// 获取子文件夹
// 传出json类
public void getChildDocumentPrefab(ref GamePrefab[] gp) {
if (Directory.Exists(PathURL))
{
var files = Directory.GetFiles(PathURL, "*.json");
gp = new GamePrefab[files.Length];
for (int i=0;i<files.Length;i++ )
{
StreamReader sr = File.OpenText(files[i]);
string input = sr.ReadToEnd();
sr.Dispose();
sr.Close();
GamePrefab jsondata = JsonMapper.ToObject<GamePrefab>(input);
gp[i] = jsondata;
//Debug.Log(jsondata + " "+ input);
}
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
// ConfigLoader.Instance().InsertJson(GamePrefab.GetGP());
SystemModel.cakeKind = -1;
SystemModel.tuCengId = null;
SystemModel.linMianId = null;
SystemModel.linMianModel = null;
SystemModel.peiZiId = "";
SystemModel.wangWenId = null;
SceneManager.LoadScene(Application.loadedLevel);
Guide.GetSingleton().CheckState();
}
}
}