Skip to content

Instantly share code, notes, and snippets.

@simonwittber
Created February 7, 2018 15:55
Show Gist options
  • Save simonwittber/3439b68cd341c1ed8f453a23baef038f to your computer and use it in GitHub Desktop.
Save simonwittber/3439b68cd341c1ed8f453a23baef038f to your computer and use it in GitHub Desktop.
Add an "OnContextClick" message to any open Unity Editors.
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection;
[InitializeOnLoad]
public static class SceneContext
{
static System.Diagnostics.Stopwatch clickClock;
static SceneContext()
{
clickClock = new System.Diagnostics.Stopwatch();
SceneView.onSceneGUIDelegate -= OnSceneGUI;
SceneView.onSceneGUIDelegate += OnSceneGUI;
}
static void OnSceneGUI(SceneView sceneview)
{
if (Event.current.button == 1)
{
if (Event.current.type == EventType.MouseDown)
clickClock.Start();
if (Event.current.type == EventType.MouseUp)
{
clickClock.Stop();
var period = clickClock.ElapsedMilliseconds;
clickClock.Reset();
if (period < 300)
{
EditorApplication.delayCall += OnContextClick;
Event.current.Use();
}
}
}
}
static void OnContextClick()
{
foreach (var i in Resources.FindObjectsOfTypeAll<Editor>())
{
var mi = i.GetType().GetMethod("OnContextClick", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (mi != null)
mi.Invoke(i, null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment