Skip to content

Instantly share code, notes, and snippets.

@CheapDevotion
Created January 19, 2015 01:59
Show Gist options
  • Select an option

  • Save CheapDevotion/077f62d253107c6cf8c8 to your computer and use it in GitHub Desktop.

Select an option

Save CheapDevotion/077f62d253107c6cf8c8 to your computer and use it in GitHub Desktop.
A simple wizard for selecting or hiding scene objects based on layer.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class LayerSelectWizard : ScriptableWizard
{
public LayerMask selectedLayers;
[MenuItem("OffTheGrid/Wizards/Layer Select Wizard %l")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard<LayerSelectWizard>("Layer Select Wizard", "Select", "Hide Other");
}
void OnWizardUpdate()
{
helpString = "Choose the layers to select or show.";
}
void OnWizardCreate()
{
List<GameObject> gameObjects = GetInLayer(selectedLayers);
Selection.objects = gameObjects.ToArray();
}
void OnWizardOtherButton()
{
GameObject[] allObjects = Object.FindObjectsOfType<GameObject>();
//Get our gameObjects
List<GameObject> gameObjects = GetInLayer(selectedLayers);
//Hide everything
foreach (GameObject go in allObjects)
{
if (go.activeInHierarchy)
go.SetActive(false);
}
//Reactive the ones we need.
foreach (GameObject activeGo in gameObjects)
{
activeGo.SetActive(true);
}
ScriptableWizard.focusedWindow.Close();
}
private List<GameObject> GetInLayer (LayerMask mask){
List<GameObject> gameObjects = new List<GameObject>();
GameObject[] allObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));
foreach (GameObject go in allObjects)
{
if (mask.IsInLayerMask(go))
{
gameObjects.Add(go);
}
}
return gameObjects;
}
}
public static class LayerMaskExtensions
{
public static bool IsInLayerMask(this LayerMask mask, int layer)
{
return ((mask.value & (1 << layer)) > 0);
}
public static bool IsInLayerMask(this LayerMask mask, GameObject obj)
{
return ((mask.value & (1 << obj.layer)) > 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment