Created
March 13, 2018 10:22
-
-
Save nicoplv/93729bf42538a24221e39bbbe68041e1 to your computer and use it in GitHub Desktop.
Debug Panel for Unity - To debug your variables more easily
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
namespace SmartUnity.Debugs | |
{ | |
public class DebugPanel | |
{ | |
#region Variables | |
private static DebugPanel debugPanel; | |
public static bool Instanciated { get { return debugPanel != null; } } | |
private Dictionary<string, Dictionary<string, string>> logs = new Dictionary<string, Dictionary<string, string>>(); | |
public static Dictionary<string, Dictionary<string, string>> Logs { get { return debugPanel.logs; } } | |
private bool changed = false; | |
public static bool Changed { get { return debugPanel.changed; } } | |
#endregion | |
#region Instanciate and Clear Methods | |
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | |
public static void OnBeforeSceneLoad() | |
{ | |
if(debugPanel == null) | |
debugPanel = new DebugPanel(); | |
} | |
public static void Clear() | |
{ | |
debugPanel = new DebugPanel(); | |
} | |
#endregion | |
#region Log Methods | |
public static void Log(string _key, object _value, string _group = "") | |
{ | |
#if UNITY_EDITOR | |
if (!debugPanel.logs.ContainsKey(_group)) | |
debugPanel.logs[_group] = new Dictionary<string, string>(); | |
debugPanel.logs[_group][_key] = _value.ToString(); | |
debugPanel.changed = true; | |
#endif | |
} | |
#endregion | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using System.Linq; | |
namespace SmartUnity.Debugs | |
{ | |
public class DebugPanelWindow : EditorWindow | |
{ | |
#region Variables | |
private static DebugPanelWindow debugPanelWindow; | |
private Vector2 scrollViewPosition = Vector2.zero; | |
private bool clearOnStop = true; | |
private Dictionary<string, bool> groupDisplays = new Dictionary<string, bool>(); | |
// Style | |
private bool guiStyleDefined = false; | |
private GUIStyle groupStyle; | |
#endregion | |
#region Menu Item Methods | |
[MenuItem("Window/Debug Panel", priority = 1100)] | |
public static void ShowWindow() | |
{ | |
debugPanelWindow = GetWindow<DebugPanelWindow>("Debug Panel"); | |
debugPanelWindow.titleContent = new GUIContent("Debug Panel", EditorGUIUtility.IconContent("CustomSorting").image); | |
debugPanelWindow.Show(); | |
} | |
#endregion | |
#region Window Methods | |
public void OnEnable() | |
{ | |
} | |
public void Update() | |
{ | |
// Force repaint if it's in play | |
if (EditorApplication.isPlaying && DebugPanel.Instanciated && DebugPanel.Changed) | |
Repaint(); | |
//else if (!EditorApplication.isPlaying && DebugPanel.Instanciated && clearOnStop) | |
// DebugPanel.Clear(); | |
} | |
public void OnGUI() | |
{ | |
if (!EditorApplication.isPlaying && DebugPanel.Instanciated && clearOnStop) | |
DebugPanel.Clear(); | |
// Manage GUIStyle | |
if (!guiStyleDefined) | |
{ | |
groupStyle = new GUIStyle(EditorStyles.foldout); | |
groupStyle.fontStyle = FontStyle.Bold; | |
guiStyleDefined = true; | |
} | |
GUILayout.BeginVertical(); | |
// Toolbar | |
GUILayout.BeginHorizontal(EditorStyles.toolbar); | |
// Clear | |
if (GUILayout.Button("Clear", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false))) | |
ButtonClear(); | |
EditorGUILayout.Space(); | |
// Auto clear on stop | |
clearOnStop = GUILayout.Toggle(clearOnStop, "Clear on Stop", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)); | |
GUILayout.FlexibleSpace(); | |
// Show all | |
if (GUILayout.Button("Show All", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false))) | |
ButtonShowAll(); | |
// Hide all | |
if (GUILayout.Button("Hide All", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false))) | |
ButtonHideAll(); | |
GUILayout.EndHorizontal(); | |
// Scroll view | |
scrollViewPosition = GUILayout.BeginScrollView(scrollViewPosition); | |
if (DebugPanel.Instanciated) | |
{ | |
foreach (KeyValuePair<string, Dictionary<string, string>> group in DebugPanel.Logs.OrderBy(i => i.Key)) | |
{ | |
if (!groupDisplays.ContainsKey(group.Key)) | |
groupDisplays[group.Key] = true; | |
if (group.Key != "") | |
groupDisplays[group.Key] = EditorGUILayout.Foldout(groupDisplays[group.Key], group.Key, true, groupStyle); | |
if (groupDisplays[group.Key]) | |
{ | |
foreach (KeyValuePair<string, string> log in group.Value) | |
EditorGUILayout.LabelField(log.Key, log.Value); | |
} | |
} | |
} | |
GUILayout.EndScrollView(); | |
GUILayout.EndVertical(); | |
} | |
#endregion | |
#region Button Methods | |
private void ButtonClear() | |
{ | |
DebugPanel.Clear(); | |
} | |
private void ButtonShowAll() | |
{ | |
for (int i = 0; i < groupDisplays.Count; i++) | |
groupDisplays[groupDisplays.ElementAt(i).Key] = true; | |
} | |
private void ButtonHideAll() | |
{ | |
for (int i = 0; i < groupDisplays.Count; i++) | |
groupDisplays[groupDisplays.ElementAt(i).Key] = false; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment