Skip to content

Instantly share code, notes, and snippets.

@zombience
Created April 13, 2017 23:38
Show Gist options
  • Save zombience/1a5df7259089e25af4538b7a0c77a576 to your computer and use it in GitHub Desktop.
Save zombience/1a5df7259089e25af4538b7a0c77a576 to your computer and use it in GitHub Desktop.
CustomDebugWIndow is an editor script that provides a way to inspect data, objects, and states over time
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections;
using System.Collections.Generic;
using System;
// to get this to work, this class musc exist outside "Editor" folder
// otherwise non-editor classes cannot access this
// therefore, tons of #ifdef T_T
public class CustomDebugWindow
#if UNITY_EDITOR
: EditorWindow
#endif
{
static Dictionary<object, LayoutHelper> debugUIList = new Dictionary<object, LayoutHelper>();
#if UNITY_EDITOR
static void GetWindow()
{
EditorWindow.GetWindow<CustomDebugWindow>().Show();
}
void Update()
{
// force editor window to redraw even when not focused
// otherwise editor window does not refresh action() data until focus
Repaint();
}
static GUIStyle style = new GUIStyle();
Vector2 scrollPos = Vector2.zero;
public static int widthPerPanel = 300;
void OnGUI()
{
if (!Application.isPlaying && debugUIList.Keys.Count > 0) debugUIList = new Dictionary<object, LayoutHelper>();
GUI.skin.label.wordWrap = true;
GUI.skin.button.wordWrap = true;
GUI.color = Color.white;
widthPerPanel = EditorGUILayout.IntField("width per panel", widthPerPanel);
scrollPos = GUILayout.BeginScrollView(scrollPos, false, false);
int idx = 0;
foreach (var kvp in debugUIList)
{
style.active.background = CreateBackGround(1, 1, Color.white);
GUILayout.BeginArea(kvp.Value.rect, style);
GUILayout.Space(20);
//kvp.Value.extraWidth = EditorGUILayout.IntField("extra width for this panel", kvp.Value.extraWidth);
GUI.backgroundColor = Color.magenta;
if(GUILayout.Button("select caling object: " + kvp.Key.GetType()))
{
EditorGUIUtility.PingObject(kvp.Key as UnityEngine.Object);
}
GUI.backgroundColor = Color.cyan;
// reset colors in case called actions have color coding
GUI.color = Color.white;
kvp.Value.width = widthPerPanel ;
kvp.Value.x = widthPerPanel * idx;
idx++;
kvp.Value.scrollPos = GUILayout.BeginScrollView(kvp.Value.scrollPos, false, true);
if (kvp.Key == null)
{
GUILayout.Label("context was null: make sure to remove objects on destroy");
GUILayout.EndScrollView();
GUILayout.EndArea();
continue;
}
if (kvp.Value == null)
{
GUI.color = Color.red;
GUILayout.Label("layout helper became null");
GUILayout.EndScrollView();
GUILayout.EndArea();
continue;
}
if(kvp.Value.actions != null)
foreach (var action in kvp.Value.actions)
{
Color c = GUI.backgroundColor;
GUI.backgroundColor = Color.red;
if (GUILayout.Button("remove from list"))
{
kvp.Value.removeThisAction = action;
}
GUI.backgroundColor = c;
GUILayout.Label(action(), GUILayout.Width(widthPerPanel));
}
GUI.backgroundColor = Color.cyan;
GUILayout.Button("formatted actions: ", EditorStyles.toolbarButton);
GUI.backgroundColor = Color.white;
if(kvp.Value.formattedActions != null)
foreach (var action in kvp.Value.formattedActions)
{
Color c = GUI.backgroundColor;
GUI.backgroundColor = Color.red;
if (GUILayout.Button("remove from list"))
{
kvp.Value.removeThisFormattedAction = action;
}
GUI.backgroundColor = c;
action();
}
kvp.Value.RemoveAction();
kvp.Value.RemoveFormattedAction();
GUI.backgroundColor = Color.grey;
GUILayout.EndScrollView();
GUILayout.EndArea();
}
GUILayout.EndScrollView();
}
#endif
static public void Add(Func<string> act, object context)
{
#if UNITY_EDITOR
if (!debugUIList.ContainsKey(context) || debugUIList[context] == null)
{
debugUIList[context] = new LayoutHelper();
}
if(debugUIList[context].actions == null) debugUIList[context].actions = new List<Func<string>>();
if (!debugUIList[context].actions.Contains(act)) debugUIList[context].actions.Add(act);
SetRects(context);
GetWindow();
#endif
}
static public void AddFormattedAction(Action act, object context)
{
if (!debugUIList.ContainsKey(context) || debugUIList[context] == null)
{
debugUIList[context] = new LayoutHelper();
}
if(debugUIList[context].formattedActions == null) debugUIList[context].formattedActions = new List<Action>();
if (!debugUIList[context].formattedActions.Contains(act)) debugUIList[context].formattedActions.Add(act);
SetRects(context);
GetWindow();
}
static public void Remove(object context)
{
#if UNITY_EDITOR
debugUIList.Remove(context);
#endif
}
static void SetRects(object context)
{
#if UNITY_EDITOR
Rect window = new Rect();
window.width = Screen.width;
window.height = Screen.height;
window.position = Vector2.zero;
int i = 0;
foreach (var kvp in debugUIList)
{
kvp.Value.position = new Vector2(window.position.x + (i * widthPerPanel), window.position.y);
kvp.Value.size = new Vector2(widthPerPanel, window.size.y);
i++;
}
#endif
}
static Texture2D CreateBackGround(int width, int height, Color col)
{
Color[] pix = new Color[width * height];
for (int i = 0; i < pix.Length; i++)
pix[i] = col;
Texture2D result = new Texture2D(width, height);
result.SetPixels(pix);
result.Apply();
return result;
}
class LayoutHelper
{
public Rect rect;
public float width { get { return size.x; } set { size = new Vector2(value + extraWidth, size.y); } }
public float x { get { return position.x; } set { position = new Vector2(value, position.y); } }
public int extraWidth { get; set; }
public Vector2 size { get { return rect.size; } set { rect.size = value; } }
public Vector2 position { get { return rect.position; } set { rect.position = value; } }
public Vector2 scrollPos = Vector2.zero;
public List<Func<string>> actions;
public List<Action> formattedActions;
public Func<string> removeThisAction;
public Action removeThisFormattedAction;
public void RemoveAction()
{
if(removeThisAction != null && actions.Contains(removeThisAction))
{
actions.Remove(removeThisAction);
removeThisAction = null;
}
}
public void RemoveFormattedAction()
{
if (removeThisFormattedAction != null && formattedActions.Contains(removeThisFormattedAction))
{
formattedActions.Remove(removeThisFormattedAction);
removeThisFormattedAction = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment