Created
January 9, 2015 10:42
-
-
Save mandarinx/b6e16fd1f9eca122231f to your computer and use it in GitHub Desktop.
Unity scene tools
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 UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class SceneTools : EditorWindow { | |
public delegate void ButtonCallback(); | |
static private int colWidth = 100; | |
static private string error = ""; | |
[MenuItem("My game/Scene Tools", false, 0)] | |
static void InitSceneTools() { | |
SceneTools window = (SceneTools)EditorWindow.GetWindow(typeof(SceneTools)); | |
window.Show(); | |
} | |
public void OnEnable() { | |
title = "Scene Tools"; | |
} | |
void OnGUI() { | |
if (error != "") { | |
EditorGUILayout.BeginHorizontal(); | |
EditorGUILayout.HelpBox(error, MessageType.Error); | |
EditorGUILayout.EndHorizontal(); | |
} else { | |
GUILayout.Space(10.0f); | |
Horizontal(new string[] {"Mono Behaviours", "Lights"}, | |
ToggleRenderers<MonoBehaviour>, | |
ToggleLights); | |
} | |
} | |
void Horizontal(string[] labels, params ButtonCallback[] callbacks) { | |
if (labels.Length != callbacks.Length) { | |
error = "Each button needs a callback"; | |
return; | |
} | |
GUILayout.BeginHorizontal(); | |
for (int i = 0; i < labels.Length; i++) { | |
if (Button(labels[i])) { | |
ButtonCallback bc = callbacks[i]; | |
bc(); | |
} | |
} | |
GUILayout.EndHorizontal(); | |
} | |
bool Button(string label) { | |
if (GUILayout.Button(label, GUILayout.Width(colWidth))) { | |
return true; | |
} | |
return false; | |
} | |
public void ToggleLights() { | |
List<Light> list = GetComps<Light>(); | |
for (int i=0; i<list.Count; i++) { | |
Light comp = list[i]; | |
comp.enabled = !comp.enabled; | |
} | |
} | |
public void ToggleRenderers<T>() where T : Component { | |
List<T> list = GetComps<T>(); | |
for (int i=0; i<list.Count; i++) { | |
T comp = list[i]; | |
comp.renderer.enabled = !comp.renderer.enabled; | |
} | |
} | |
public static List<T> GetComps<T> () where T : Component { | |
List<T> list = new List<T>(); | |
UnityEngine.Object[] gos = GOs; | |
foreach (GameObject go in gos) { | |
T comp = go.GetComponent<T>(); | |
if (comp != null) { | |
list.Add(comp); | |
} | |
} | |
return list; | |
} | |
public static GameObject[] GOs { | |
get { | |
UnityEngine.Object[] objs = UnityEngine.Object.FindObjectsOfType(typeof(GameObject)); | |
return objs as GameObject[]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment