public static GraphEditorData LoadGraph(GraphEditorWindow graph, int graphId) { GraphEditorData resultData = new GraphEditorData(); string graphEditorConfigFilePath = Path.Combine(Application.dataPath, string.Format("FlatNode/Editor/GraphSavedConfig/{0}.json", graphId)); if (!File.Exists(graphEditorConfigFilePath)) { Debug.LogErrorFormat("无法载入行为图配置文件: {0}", graphEditorConfigFilePath); } string jsonString = File.ReadAllText(graphEditorConfigFilePath); GraphConfigInfo graphConfigInfo = new GraphConfigInfo(); EditorJsonUtility.FromJsonOverwrite(jsonString, graphConfigInfo); //处理注释框 for (int i = 0; i < graphConfigInfo.commentBoxInfoList.Count; i++) { CommentBoxInfo commentBoxInfo = graphConfigInfo.commentBoxInfoList[i]; CommentBoxView commentBoxView = ParseCommentBoxInfo(commentBoxInfo, graph); resultData.commentBoxViewList.Add(commentBoxView); } //变量 for (int i = 0; i < graphConfigInfo.graphVariableInfoList.Count; i++) { if (!graphConfigInfo.graphVariableInfoList[i].Validate()) { continue; } resultData.graphVariableInfoList.Add(graphConfigInfo.graphVariableInfoList[i].OnDeserialized()); } //如果有节点无法解析出来(可能是改了类名称之类的),则需要跳过这些节点 HashSet <int> errorNodeIndexSet = new HashSet <int>(); //首先将所有的节点都生成 for (int i = 0; i < graphConfigInfo.nodesList.Count; i++) { NodeEditorView nodeView = ParseNodeInfo(graphConfigInfo.nodesList[i], graph); if (nodeView == null) { errorNodeIndexSet.Add(i); continue; } resultData.nodeList.Add(nodeView); } //然后再将所有节点的内容写进去,将节点连起来 int nodeIndex = 0; for (int i = 0; i < graphConfigInfo.nodesList.Count; i++) { if (errorNodeIndexSet.Contains(i)) { //skip continue; } UpdateNodeViewData(graphConfigInfo.nodesList[i], resultData.nodeList[nodeIndex], resultData); nodeIndex++; } resultData.graphId = graphConfigInfo.graphId; resultData.graphName = graphConfigInfo.graphName; resultData.graphDescription = graphConfigInfo.graphDescription; return(resultData); }
public static void SaveGraph(GraphEditorData data) { string jsonString = String.Empty; byte[] runtimeConfigBytes = null; bool isSuccess = true; try { //存储技能配置json文件,配置文件使用json是因为可读性好 GraphConfigInfo graphConfigInfo = new GraphConfigInfo { graphId = data.graphId, graphName = data.graphName, graphDescription = data.graphDescription, nodesList = new List <NodeConfigInfo>(), commentBoxInfoList = new List <CommentBoxInfo>(), graphVariableInfoList = new List <GraphVariableInfo>(), }; for (int i = 0; i < data.nodeList.Count; i++) { NodeEditorView nodeView = data.nodeList[i]; graphConfigInfo.nodesList.Add(ConvertToNodeInfo(nodeView)); } for (int i = 0; i < data.commentBoxViewList.Count; i++) { CommentBoxView commentBoxView = data.commentBoxViewList[i]; graphConfigInfo.commentBoxInfoList.Add(ConvertToCommentInfo(commentBoxView)); } for (int i = 0; i < data.graphVariableInfoList.Count; i++) { graphConfigInfo.graphVariableInfoList.Add(data.graphVariableInfoList[i].OnSerialized()); } jsonString = JsonUtility.ToJson(graphConfigInfo, true); runtimeConfigBytes = ConvertToRuntimeInfo(data.nodeList, data.graphVariableInfoList, data.graphId); } catch (Exception e) { isSuccess = false; Debug.LogError(e.Message); throw; } finally { if (isSuccess) { string graphEditorConfigFilePath = Path.Combine(Application.dataPath, string.Format("FlatNode/Editor/GraphSavedConfig/{0}.json", data.graphId)); File.WriteAllText(graphEditorConfigFilePath, jsonString); string graphRuntimeConfigFilePath = Path.Combine(Application.dataPath, string.Format("Resources/GraphRuntime/{0}.bytes", data.graphId)); string parentDirctoryPath = Directory.GetParent(graphRuntimeConfigFilePath).FullName; if (!Directory.Exists(parentDirctoryPath)) { Directory.CreateDirectory(parentDirctoryPath); } if (runtimeConfigBytes != null) { File.WriteAllBytes(graphRuntimeConfigFilePath, runtimeConfigBytes); } //更新所有图的记录文件 AddOrUpdateGraphsRecord(data); AssetDatabase.Refresh(); } } }