Instantly share code, notes, and snippets.
Created
April 19, 2014 19:52
-
Star
(5)
5
You must be signed in to star a gist -
Fork
(2)
2
You must be signed in to fork a gist
-
Save Minimally/11095497 to your computer and use it in GitHub Desktop.
Unity Editor script for filtering layers via keyboard
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 UnityEditor; | |
using UnityEngine; | |
public static class LayerMenu | |
{ | |
/* PLACE IN EDITOR FOLDER. | |
* Feel free to change hotkeys depending on your preference. | |
* Modifier Keys % (ctrl on Windows, cmd on OS X), # (shift), & (alt), _ (no key modifiers) */ | |
#region Layer Visibility | |
/// <summary> Only the selected layer will be visible in the editor. </summary> | |
[MenuItem("Edit/Selection/Show Only Selected Layer _l", false)] | |
static void LayersShowOnlySelected() | |
{ | |
Transform selected = Selection.activeTransform; | |
int layerNumber = selected.gameObject.layer; | |
LayerMask layerNumberBinary = 1 << layerNumber; // This turns the layer number into the right binary number | |
Tools.visibleLayers = layerNumberBinary; // This lets us set which layers are visible in the scene view. | |
SceneView.RepaintAll(); // We need to repaint the scene | |
} | |
/// <summary> Make all layers visible in the editor. </summary> | |
[MenuItem("Edit/Selection/Show All Layers _;", false)] | |
static void LayersShowEverything() | |
{ | |
Tools.visibleLayers = -1; // This lets us set which layers are visible in the scene view. | |
SceneView.RepaintAll(); // We need to repaint the scene | |
} | |
/// <summary> Make the selected layer visible in the editor. </summary> | |
[MenuItem("Edit/Selection/Show Layer _k", false)] | |
static void LayersShowLayer() | |
{ | |
Transform selected = Selection.activeTransform; | |
int layerNumber = selected.gameObject.layer; | |
LayerMask layerNumberBinary = 1 << layerNumber; // This turns the layer number into the right binary number | |
Tools.visibleLayers = Tools.visibleLayers |= layerNumberBinary; // This lets us set which layers are visible in the scene view. | |
SceneView.RepaintAll(); // We need to repaint the scene | |
} | |
/// <summary> Make the selected layer invisible in the editor. </summary> | |
[MenuItem("Edit/Selection/Hide Layer _j", false)] | |
static void LayersHideLayer() | |
{ | |
Transform selected = Selection.activeTransform; | |
int layerNumber = selected.gameObject.layer; | |
LayerMask layerNumberBinary = 1 << layerNumber; // This turns the layer number into the right binary number | |
LayerMask flippedVisibleLayers = ~Tools.visibleLayers; | |
Tools.visibleLayers = ~(flippedVisibleLayers | layerNumberBinary); // This lets us set which layers are visible in the scene view. | |
SceneView.RepaintAll(); // We need to repaint the scene | |
} | |
/// <summary> Copy layer of selected gameObject to all of its children. </summary> | |
[MenuItem("Edit/Selection/Layer to Children _'", false)] | |
static void LayerToChildren() | |
{ | |
Transform selected = Selection.activeTransform; | |
int layerNumber = selected.gameObject.layer; | |
Transform[] children = selected.GetComponentsInChildren<Transform>(true); | |
foreach (Transform item in children) | |
{ | |
if (item.gameObject.layer == layerNumber) | |
continue; // Skip like tags | |
Undo.RecordObject(item.gameObject, "Layer To Children"); | |
item.gameObject.layer = layerNumber; | |
EditorUtility.SetDirty(item.gameObject); | |
} | |
} | |
#endregion | |
#region Validation | |
[MenuItem("Edit/Selection/Show Only Selected Layer _l", true)] | |
static bool ValidateLayersShowOnlySelected() | |
{ | |
return IsOneGameObjectSelected(); | |
} | |
[MenuItem("Edit/Selection/Show Layer _k", true)] | |
static bool ValidateLayersShowLayer() | |
{ | |
return IsOneGameObjectSelected(); | |
} | |
[MenuItem("Edit/Selection/Hide Layer _j", true)] | |
static bool ValidateLayersHideLayer() | |
{ | |
return IsOneGameObjectSelected(); | |
} | |
[MenuItem("Edit/Selection/Layer to Children _'", true)] | |
static bool ValidateLayerToChildren() | |
{ | |
return IsOneGameObjectSelected(); | |
} | |
/// <summary> Determines if is one game object selected. </summary> | |
/// <returns><c>true</c> if is one game object selected; otherwise, <c>false</c>.</returns> | |
static bool IsOneGameObjectSelected() | |
{ | |
return Selection.activeGameObject != null && Selection.transforms.Length == 1; | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment