Skip to content

Instantly share code, notes, and snippets.

@seobyeongky
Last active November 23, 2019 12:46
Show Gist options
  • Save seobyeongky/e92a7a52916ef47059fcbe17a2436ac7 to your computer and use it in GitHub Desktop.
Save seobyeongky/e92a7a52916ef47059fcbe17a2436ac7 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class ConsoleHelperStartup
{
static ConsoleHelperStartup()
{
ConsoleHelper.Begin();
}
}
public class ConsoleHelper
{
static ConsoleHelper instance;
EditorWindow wnd_;
string cachedLog;
Func<string> GetSelectedLog;
List<PointItem> points = new List<PointItem>();
Regex vec3Regex;
Regex vec2Regex;
PointItemComparer comparer = new PointItemComparer();
struct PointItem
{
public int order;
public Vector3 p;
}
class PointItemComparer : IComparer<PointItem>
{
public int Compare(PointItem x, PointItem y)
{
return x.order.CompareTo(y.order);
}
}
public static void Begin()
{
if (instance == null)
{
instance = new ConsoleHelper();
instance.Begin_();
}
}
void Begin_()
{
EditorApplication.update += OnUpdate;
SceneView.duringSceneGui += OnScene;
var bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
var type = typeof(Editor).Assembly.GetType("UnityEditor.ConsoleWindow");
var fieldInfo = type.GetField("m_ActiveText", bf);
GetSelectedLog = () => wnd == null ? null : (string)fieldInfo.GetValue(wnd);
var lb = Regex.Escape("(");
var rb = Regex.Escape(")");
var dot = Regex.Escape(".");
var num = "[-+]?[0-9]*" + dot + "?[0-9]+";
vec3Regex = new Regex(lb + $"({num}), ({num}), ({num})" + rb);
vec2Regex = new Regex(lb + $"({num}), ({num})" + rb);
}
void OnUpdate()
{
var nowLog = GetSelectedLog();
if (nowLog != cachedLog)
{
cachedLog = nowLog;
UpdatePoints();
SceneView.RepaintAll();
}
}
void UpdatePoints()
{
points.Clear();
if (string.IsNullOrEmpty(cachedLog))
return;
var matches = vec3Regex.Matches(cachedLog);
foreach (Match m in matches)
{
var x = float.Parse(m.Groups[1].Value);
var y = float.Parse(m.Groups[2].Value);
var z = float.Parse(m.Groups[3].Value);
points.Add(new PointItem{order = m.Index, p = new Vector3(x, y, z)});
}
matches = vec2Regex.Matches(cachedLog);
foreach (Match m in matches)
{
var x = float.Parse(m.Groups[1].Value);
var y = float.Parse(m.Groups[2].Value);
var z = 0;
points.Add(new PointItem{order = m.Index, p = new Vector3(x, y, z)});
}
points.Sort(comparer);
}
void OnScene(SceneView sceneview)
{
//Handles.BeginGUI();
for (int i = 0; i < points.Count; i++)
{
var item = points[i];
var color = GetColor(i);
//Handles.DrawLine(p, p + new Vector3(1,0,0));
//Handles.DrawSphere(i+998, item.p, Quaternion.identity, 0.15f);
Handles.color = new Color(color.r, color.g, color.b, 0.8f);
Handles.DrawWireDisc(item.p, new Vector3(0,0,-1), 0.15f);
Handles.color = color;
Handles.DrawLine(item.p + new Vector3(-0.05f, 0, 0), item.p + new Vector3(0.05f, 0, 0));
Handles.DrawLine(item.p + new Vector3(0, -0.05f, 0), item.p + new Vector3(0, 0.05f, 0));
}
//Handles.EndGUI();
}
Color GetColor(int i)
{
if (i == 0)
return Color.red;
else if (i == 1)
return Color.green;
else if (i == 2)
return Color.blue;
else
return Color.black;
}
EditorWindow wnd
{
get
{
if (!wnd_)
{
var windows = Resources.FindObjectsOfTypeAll(typeof(Editor).Assembly.GetType("UnityEditor.ConsoleWindow"));
if (windows.Length == 0)
return null;
wnd_ = windows[0] as EditorWindow;
}
return wnd_;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment