コード例 #1
0
 public void DeleteTimeLine(int index)
 {
     if (curData != null)
     {
         curData.DeleteTimeLine(index);
     }
     else
     {
         Debug.Log(" cur data is null");
     }
 }
コード例 #2
0
        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);
        }