Skip to content

Instantly share code, notes, and snippets.

@fishtopher
Created December 5, 2017 11:21
Show Gist options
  • Save fishtopher/7ca6accabc54383b236632beb063fbfa to your computer and use it in GitHub Desktop.
Save fishtopher/7ca6accabc54383b236632beb063fbfa to your computer and use it in GitHub Desktop.
A little Unity utility to check for shortcut key collisions because it's hard to know if you're duplicating one from anywhere else.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
// Little utility to check for shortcut key collisions because it's hard to
// know if you're duplicating one from anywhere else.
//
// Put this inside a /Editor/ Folder
//
// ## Contact
// Any questions or bug reports - email me!
// Chris McLaughlin - chris@vitei.com
//
public class ShortcutCollisionChecker{
[MenuItem("Custom/Vitei/Code/Check For Shortcut Collisions #%C")]
static void CheckForShortcutCollisions() {
Dictionary<string, string> shortcuts = new Dictionary<string, string>() { //populate with unity default keys, probably missing some
{"_Q", "[Unity Built In] Pan"},
{"_W", "[Unity Built In] Move"},
{"_E", "[Unity Built In] Rotate"},
{"_R", "[Unity Built In] Scale"},
{"_T", "[Unity Built In] Rect Tool"},
{"_Z", "[Unity Built In] Pivot Mode toggle"},
{"_X", "[Unity Built In] Pivot Rotation Toggle"},
{"_V", "[Unity Built In] Vertex Snap"},
{"#%N", "[Unity Built In] New empty game object"},
{"#&N", "[Unity Built In] New empty child to selected game object"},
{"%&F", "[Unity Built In] Move to view"},
{"#%F", "[Unity Built In] Align with view"},
{"#F", "[Unity Built In] or double-F Locks the scene view camera to the selected GameObject"},
{"%1", "[Unity Built In] Scene"},
{"%2", "[Unity Built In] Game"},
{"%3", "[Unity Built In] Inspector"},
{"%4", "[Unity Built In] Hierarchy"},
{"%5", "[Unity Built In] Project"},
{"%6", "[Unity Built In] Animation"},
{"%7", "[Unity Built In] Profiler"},
{"%9", "[Unity Built In] Asset store"},
{"%0", "[Unity Built In] Version Control"},
{"#%C", "[Unity Built In] Console"},
{"%Z", "[Unity Built In] Undo"},
{"%Y", "[Unity Built In] (Windows only) Redo"},
{"#%Z", "[Unity Built In] (Mac only) Redo"},
{"%X", "[Unity Built In] Cut"},
{"%C", "[Unity Built In] Copy"},
{"%V", "[Unity Built In] Paste"},
{"%D", "[Unity Built In] Duplicate"},
{"#Del", "[Unity Built In] Delete"},
{"_F", "[Unity Built In] Frame (centre) selection"},
{"%F", "[Unity Built In] Find"},
{"%A", "[Unity Built In] Select All"},
{"%P", "[Unity Built In] Play"},
{"#%P", "[Unity Built In] Pause"},
{"%&P", "[Unity Built In] Step"},
{"#%1", "[Unity Built In] Load Selection 1"},
{"#%2", "[Unity Built In] Load Selection 2"},
{"#%3", "[Unity Built In] Load Selection 3"},
{"#%4", "[Unity Built In] Load Selection 4"},
{"#%5", "[Unity Built In] Load Selection 5"},
{"#%6", "[Unity Built In] Load Selection 6"},
{"#%7", "[Unity Built In] Load Selection 7"},
{"#%8", "[Unity Built In] Load Selection 8"},
{"#%9", "[Unity Built In] Load Selection 9"},
{"%&1", "[Unity Built In] Save Selection 1"},
{"%&2", "[Unity Built In] Save Selection 2"},
{"%&3", "[Unity Built In] Save Selection 3"},
{"%&4", "[Unity Built In] Save Selection 4"},
{"%&5", "[Unity Built In] Save Selection 5"},
{"%&6", "[Unity Built In] Save Selection 6"},
{"%&7", "[Unity Built In] Save Selection 7"},
{"%&8", "[Unity Built In] Save Selection 8"},
{"%&9", "[Unity Built In] Save Selection 9"},
{"%R", "[Unity Built In] Refresh"},
{"#&A", "[Unity Built In] Toggle Gameobject Active"},
{"%#C", "[Unity Built In] Open Console"},
};
// Get all our c# files
string[] assetPaths = AssetDatabase.GetAllAssetPaths();
List<string> allCsFiles = new List<string>();
foreach (string assetPath in assetPaths) {
if (assetPath.Contains(".cs")) {
allCsFiles.Add(assetPath);
}
}
// Tell the user we're about to do some serious work that might take time.
EditorUtility.DisplayProgressBar("Checking for Shortcut Collisions", "", 0);
// Run through every line in every file looking for "[menuitem]"s with shortcuts, and make a list of them
int shortcutCount = 0;
int collisionCount = 0;
Regex regex = new Regex("\\[MenuItem.*\\(\"(.*) ([&#%_]*[a-z])\".*]");
for (int i = 0; i < allCsFiles.Count; i++) {
// update the progress bar every 10 files.
if (i % 10 == 0) {
EditorUtility.DisplayProgressBar("Checking for Shortcut Collisions", i + "/" + allCsFiles.Count, (float)i / (float)allCsFiles.Count);
}
StreamReader sr = new StreamReader(allCsFiles[i]);
string line;
while ((line = sr.ReadLine()) != null) {
Match m = regex.Match(line);
if (m.Success) {
shortcutCount++;
string sortedKeys = new string(m.Groups[2].ToString().OrderBy(c => c).ToArray());
// if this shortcut already exits, print it out to the console
if (shortcuts.ContainsKey(sortedKeys)) {
string sks = sortedKeys.ToUpper().Replace("#", "Shift ").Replace("%", "Ctrl ").Replace("&", "Alt ").Replace("_", " ");
Debug.Log("Collision: " + sks + " - <b>\"" + m.Groups[1] + "\"</b> vs <b>\"" + shortcuts[sortedKeys] + "\"</b>", AssetDatabase.LoadAssetAtPath(allCsFiles[i], typeof(object)));
collisionCount++;
}
else {
shortcuts.Add(sortedKeys, m.Groups[1].ToString());
}
}
}
sr.Close();
}
EditorUtility.ClearProgressBar();
// Print out overview of how well (bad?) we did
Debug.Log("" + collisionCount + " collisions in " + shortcutCount + " shortcuts in " + allCsFiles.Count + " cs files");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment