public EditData SaveToJson(string path, TimeLineData data = null) { if (!File.Exists(path)) { curData = CreateNewEditData(); curData.dataPath = path; string jsonStr = JsonWriter.Serialize(curData); Debug.Log(jsonStr); File.WriteAllText(path, jsonStr); } else { string jsonstr = File.ReadAllText(path); if (curData == null) { curData = JsonReader.Deserialize <EditData>(jsonstr) as EditData; if (curData == null) { EditorUtility.DisplayDialog("Error", "读取文件失败", "OK"); return(null); } for (int i = 0; i < curData.GetTimeLineCount(); i++) {//json 反序列化会全部解析成基类 所以做clone操作 比较恶心 TimeLineData tempdata = curData.GetTimeLineByIndex(i); Type t = Type.GetType(tempdata.className); var subdata = Activator.CreateInstance(t) as TimeLineData; subdata.Clone(tempdata); curData.DeleteTimeLine(i); curData.lineDataList.Insert(i, subdata); } } else { if (data != null) { curData.dataPath = path; curData.AddLineData(data); string jsonStr = JsonWriter.Serialize(curData); Debug.Log(jsonStr); File.WriteAllText(path, jsonStr); } } } return(curData); }
public void SaveEditData(string filePath, TimeLineData data) { if (string.IsNullOrEmpty(filePath)) { return; } if (data == null) { Debug.LogError("save linedata is null"); return; } FileStream fs = null; BinaryWriter bw = null; try { fs = File.Open(filePath, FileMode.Create); bw = new BinaryWriter(fs); //文件头 string head = "EditData"; bw.Write(head); if (curData == null) { curData = CreateNewEditData(); curData.dataPath = filePath; } curData.AddLineData(data); curData.WriteToBinary(bw); bw.Close(); fs.Close(); } catch (Exception e) { Debug.LogError(e.Message); if (bw != null) { bw.Close(); } if (fs != null) { fs.Close(); } } }
EditData LoadLineData(string path, bool isJson = true) { if (isJson) { return(SaveToJson(path)); } else { #region binary if (File.Exists(path)) { using (FileStream fs = File.Open(path, FileMode.Open)) { BinaryReader br = new BinaryReader(fs); //文件头 string head = br.ReadString(); if (head != "EditData") { br.Close(); fs.Close(); return(null); } EditData td = CreateNewEditData(); td.dataPath = path; td.ReadFromBinary(br); //结束 br.Close(); fs.Close(); return(td); } } else { FileStream fs = null; BinaryWriter bw = null; try { fs = File.Open(path, FileMode.Create); bw = new BinaryWriter(fs); //文件头 string head = "EditData"; bw.Write(head); EditData ed = CreateNewEditData(); ed.dataPath = path; ed.WriteToBinary(bw); bw.Close(); fs.Close(); } catch (Exception e) { Debug.LogError(e.Message); if (bw != null) { bw.Close(); } if (fs != null) { fs.Close(); } } } #endregion } return(null); }