From 3b23d391377ece185e7a7de6b848357bc3b940ad Mon Sep 17 00:00:00 2001 From: Sergio Prada Date: Tue, 8 Nov 2022 20:32:35 -0500 Subject: [PATCH] Added 'save as', 'load from' and 'clear' functionalities --- Editor/CompilationAnalysis.cs | 38 ++++++++++++++++++++++++++++- Editor/CompilationTimelineWindow.cs | 11 +++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Editor/CompilationAnalysis.cs b/Editor/CompilationAnalysis.cs index 9f98435..6b04a23 100644 --- a/Editor/CompilationAnalysis.cs +++ b/Editor/CompilationAnalysis.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using System.Text; using UnityEditor; @@ -229,7 +230,36 @@ public static void WriteAll(IterativeCompilationData data) { var json = JsonUtility.ToJson(data, true); EditorPrefs.SetString(EditorPrefStore, json); } - + public static void SaveToFile() + { + var json = EditorPrefs.GetString(EditorPrefStore); + var path = EditorUtility.SaveFilePanel( + "Save data as JSON", + "", + "CompilationData.json", + "json"); + if (path.Length != 0) + { + File.WriteAllText(path, json); + } + } + public static void LoadFromFile() + { + var path = EditorUtility.OpenFilePanel( + "Load data from JSON file", + "", + "json"); + if (path.Length == 0) return; + try + { + tempData = JsonUtility.FromJson(File.ReadAllText(path)); + WriteAll(tempData); + } + catch (Exception ex) + { + Debug.LogError($"Error loading data {ex}"); + } + } public void OnBeforeSerialize() { compilationStarted = CompilationStarted; @@ -252,6 +282,12 @@ public static void Clear() tempData = new IterativeCompilationData(); } + public static void ResetData() + { + tempData = new IterativeCompilationData(); + WriteAll(tempData); + } + public static void Add() { tempData.iterations.Add(new CompilationData()); diff --git a/Editor/CompilationTimelineWindow.cs b/Editor/CompilationTimelineWindow.cs index 490f4ea..78d41ea 100644 --- a/Editor/CompilationTimelineWindow.cs +++ b/Editor/CompilationTimelineWindow.cs @@ -286,6 +286,17 @@ private void OnGUI() compactDrawing = GUILayout.Toggle(compactDrawing, "Compact", EditorStyles.toolbarButton); AllowLogging = GUILayout.Toggle(AllowLogging, new GUIContent("Logging", "Log additional compilation data to the console on compilation"), EditorStyles.toolbarButton); ShowAssemblyReloads = GUILayout.Toggle(ShowAssemblyReloads, new GUIContent("Show Reloads", "Show or hide assembly reloads in the timeline."), EditorStyles.toolbarButton); + if (GUILayout.Button(new GUIContent(UnityEditor.EditorGUIUtility.FindTexture("SaveAs"), "Save current data to a JSON file"), EditorStyles.toolbarButton)) CompilationData.SaveToFile(); + if (GUILayout.Button(new GUIContent(UnityEditor.EditorGUIUtility.FindTexture("Profiler.Open"), "Load data from a JSON file"), EditorStyles.toolbarButton)) + { + CompilationData.LoadFromFile(); + Clear(); + } + if (GUILayout.Button(new GUIContent("Clear", "Clear the captured data"), EditorStyles.toolbarButton)) + { + CompilationData.ResetData(); + Clear(); + } colorMode = (ColorMode) EditorGUILayout.EnumPopup(colorMode, GUILayout.ExpandWidth(false)); var totalSpan = TimeSpan.Zero;