Skip to content

Instantly share code, notes, and snippets.

@tntmeijs
Created February 13, 2018 09:47
Show Gist options
  • Save tntmeijs/28bedfe57c30679361e3123b88a5fcdf to your computer and use it in GitHub Desktop.
Save tntmeijs/28bedfe57c30679361e3123b88a5fcdf to your computer and use it in GitHub Desktop.
A simple tool for the Unity2D editor which allows you to hide sorting layers.
using UnityEditor;
using UnityEngine;
/*
* This creative work (which includes code) is under exclusive copyright by default.
* Unless specified otherwise by Tahar Meijs, nobody else may use, copy, distribute,
* or modify this work without being at risk of take-downs, shake-downs, or litigation.
*
* Testing code does not mean you will are allowed to keep on using this codebase for
* your own personal / commercial gain. However, it is always encouraged to use this
* code for educational purposes
*/
public class Layers2D : EditorWindow
{
private SortingLayer[] m_sortingLayers = null;
private Vector2 m_scrollPosition = Vector2.zero;
[MenuItem("Window/Tahars Tools/2D Layers")]
public static void ShowWindow()
{
GetWindow(typeof(Layers2D));
}
private void OnGUI()
{
m_scrollPosition = EditorGUILayout.BeginScrollView(m_scrollPosition);
EditorGUILayout.BeginVertical();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Refresh Sorting Layers"))
{
RefreshLayers();
}
if (GUILayout.Button("Show All"))
{
ChangeSortingLayerVisibility("", true, true);
}
if (GUILayout.Button("Hide All"))
{
ChangeSortingLayerVisibility("", false, true);
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
ListAllSortingLayers();
EditorGUILayout.EndScrollView();
}
private void ListAllSortingLayers()
{
if (null == m_sortingLayers)
return;
if (0 == m_sortingLayers.Length)
return;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();
foreach (SortingLayer sortingLayer in m_sortingLayers)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label(sortingLayer.value.ToString());
GUILayout.FlexibleSpace();
GUILayout.Label(sortingLayer.name);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Show"))
{
ChangeSortingLayerVisibility(sortingLayer.name, true);
}
if (GUILayout.Button("Hide"))
{
ChangeSortingLayerVisibility(sortingLayer.name, false);
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
private void RefreshLayers()
{
m_sortingLayers = SortingLayer.layers;
}
private void ChangeSortingLayerVisibility(string layer, bool isVisible, bool applyToAll = false)
{
GameObject[] allSceneObjects = FindObjectsOfType<GameObject>();
foreach(GameObject entity in allSceneObjects)
{
SpriteRenderer spriteRenderer = entity.GetComponent<SpriteRenderer>();
// Has no sprite renderer component
if (null == spriteRenderer)
continue;
if (applyToAll)
{
// Just apply the setting without looking at the layer name
spriteRenderer.enabled = isVisible;
continue;
}
if (layer == spriteRenderer.sortingLayerName && entity.activeInHierarchy)
spriteRenderer.enabled = isVisible;
}
}
private void OnDestroy()
{
// Window closes, so show every sprite again
ChangeSortingLayerVisibility("", true, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment