Created
April 21, 2015 13:20
-
-
Save romainPechot/a8155b27e5230140ced0 to your computer and use it in GitHub Desktop.
An Unity editor window helper to show gameObjects in scene or project per tag or layer.
This file contains hidden or 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.Collections; | |
using System.Collections.Generic; | |
public class SearchTagLayer : EditorWindow | |
{ | |
//Scene/Project | |
private string[] _as_sceneProject = new string[2]{"Scene", "Project"}; | |
private int _i_sceneProjectIndex = 0; | |
//Tag/Layer | |
private string[] _as_tagLayer = new string[2]{"Tag", "Layer"}; | |
private int _i_tagLayerIndex = 0; | |
//|| TAG || | |
//Genuine tag list | |
private string[] _as_tags; | |
//Main 2D array [tag][object] | |
private GameObject[][] _aaGO_tags; | |
//Foldout boolean for each tag | |
private bool[] _ab_foldout_tags; | |
//Main Scroll | |
private Vector2 _V2_scroll_mainTag = Vector2.zero; | |
//|| LAYER || | |
//Main 2D array | |
public GameObject[][] _aaGO_layers; | |
//Foldout boolean for each layer | |
private bool[] _ab_foldout_layers; | |
//Main scroll | |
private Vector2 _V2_scrollVert_layers = Vector2.zero; | |
//|| MAIN DIRECTORY || | |
public enum eMainDirectory | |
{ | |
Scene = 0, | |
Project = 1, | |
Both = 2 | |
}// eMainDirectory | |
[MenuItem("Window/Show GameObject Tag-Layer")] | |
private static void Init() | |
{ | |
SearchTagLayer _window = GetWindow<SearchTagLayer>(); | |
_window.title = "Tags/Layers"; | |
_window.Show(); | |
}// Init | |
private void OnGUI() | |
{ | |
//Pick from Scene or Project ? | |
_i_sceneProjectIndex = GUILayout.SelectionGrid(_i_sceneProjectIndex, _as_sceneProject, 2); | |
//Pick with Tag or Layers ? | |
_i_tagLayerIndex = GUILayout.SelectionGrid(_i_tagLayerIndex, _as_tagLayer, 2); | |
switch(_i_tagLayerIndex) | |
{ | |
//|| TAG || | |
case 0: | |
{ | |
if(GUILayout.Button("Refresh")) | |
{ | |
//Reset ref | |
_aaGO_tags = null; | |
_ab_foldout_tags = null; | |
_V2_scroll_mainTag = Vector2.zero; | |
if(_i_sceneProjectIndex == 0) GetListByTagsFromScene(); | |
else GetListByTagsFromProject(); | |
} | |
if(_aaGO_tags != null) | |
{ | |
_V2_scroll_mainTag = EditorGUILayout.BeginScrollView(_V2_scroll_mainTag); | |
//Show each tags | |
for(int i = 0; i < _aaGO_tags.Length; i++) | |
{ | |
if(_aaGO_tags[i] != null) | |
{ | |
_ab_foldout_tags[i] = EditorGUILayout.Foldout(_ab_foldout_tags[i], _as_tags[i] + ":" + _aaGO_tags[i].Length.ToString()); | |
if(_ab_foldout_tags[i]) | |
{ | |
EditorGUI.indentLevel++; | |
//Show each GameObject | |
for(int j = 0; j < _aaGO_tags[i].Length; j++) | |
{ | |
if(_aaGO_tags[i][j] != null) | |
{ | |
EditorGUILayout.BeginHorizontal(); | |
//Ping ? | |
if(GUILayout.Button("?", GUILayout.ExpandWidth(false))) | |
{ | |
EditorGUIUtility.PingObject(_aaGO_tags[i][j]); | |
} | |
//Select name ? | |
if(GUILayout.Button(_aaGO_tags[i][j].transform.name)) | |
{ | |
Selection.activeGameObject = _aaGO_tags[i][j]; | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
}//for() | |
EditorGUI.indentLevel--; | |
} | |
} | |
}//for() | |
EditorGUILayout.EndScrollView(); | |
} | |
}break; | |
//|| LAYER || | |
case 1: | |
{ | |
//Refresh button | |
if(GUILayout.Button("Refresh")) | |
{ | |
//Reset ref | |
_aaGO_layers = null; | |
_ab_foldout_layers = null; | |
_V2_scrollVert_layers = Vector2.zero; | |
if(_i_sceneProjectIndex == 0) | |
{ | |
GetListByLayersFromScene(); | |
} | |
else | |
{ | |
GetListByLayersFromProject(); | |
} | |
} | |
if(_aaGO_layers != null) | |
{ | |
_V2_scrollVert_layers = EditorGUILayout.BeginScrollView(_V2_scrollVert_layers); | |
//Show each layers | |
for(int i = 0; i < 32; i++) | |
{ | |
//Not Empty ? | |
if(_aaGO_layers[i] != null) | |
{ | |
//Foldout ? | |
_ab_foldout_layers[i] = EditorGUILayout.Foldout(_ab_foldout_layers[i], i.ToString() + "|" + LayerMask.LayerToName(i) + ":" + _aaGO_layers[i].Length.ToString()); | |
if(_ab_foldout_layers[i]) | |
{ | |
EditorGUI.indentLevel++; | |
//Show each gameObject | |
for(int j = 0; j < _aaGO_layers[i].Length; j++) | |
{ | |
//Safety | |
if(_aaGO_layers[i][j] != null) | |
{ | |
EditorGUILayout.BeginHorizontal(); | |
//Ping ? | |
if(GUILayout.Button("?", GUILayout.ExpandWidth(false))) | |
{ | |
EditorGUIUtility.PingObject(_aaGO_layers[i][j]); | |
} | |
//Select name ? | |
if(GUILayout.Button(_aaGO_layers[i][j].transform.name)) | |
{ | |
//Select in scene | |
Selection.activeGameObject = _aaGO_layers[i][j]; | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
}//for() | |
EditorGUI.indentLevel--; | |
} | |
} | |
}//for() | |
EditorGUILayout.EndScrollView(); | |
} | |
}break; | |
default:break; | |
}//switch(_i_tagLayerIndex) | |
}// OnGUI | |
private void GetListByTagsFromScene() | |
{ | |
//Get genuine tags list | |
_as_tags = UnityEditorInternal.InternalEditorUtility.tags; | |
//Get all gameobjects from scene | |
GameObject[] _aGO_sceneGOs = GetAllGameObjects(eMainDirectory.Scene); | |
//Set temp list/array | |
List<GameObject>[] _aLGO_sortedList = new List<GameObject>[_as_tags.Length]; | |
//Setup main 2D array | |
_aaGO_tags = new GameObject[_as_tags.Length][]; | |
//Setup foldout array | |
_ab_foldout_tags = new bool[_as_tags.Length]; | |
//Init every tag'list | |
for(int o = 0; o < _aLGO_sortedList.Length; o++) | |
{ | |
_aLGO_sortedList[o] = new List<GameObject>(); | |
}//for() | |
//Loop through every gameobjects from scene | |
for(int i = 0; i < _aGO_sceneGOs.Length; i++) | |
{ | |
//Get tag'index | |
int _i_tagIdx = TagToIndex(_aGO_sceneGOs[i].tag); | |
//Not already inside the list ? | |
if(!_aLGO_sortedList[_i_tagIdx].Contains(_aGO_sceneGOs[i])) | |
{ | |
//Ok, add ! | |
_aLGO_sortedList[_i_tagIdx].Add(_aGO_sceneGOs[i]); | |
} | |
}//for() | |
//Done! Now fill-in main 2D array | |
for(int j = 0; j < _aLGO_sortedList.Length; j++) | |
{ | |
_aaGO_tags[j] = _aLGO_sortedList[j].ToArray(); | |
}//for() | |
}// GetListByTagsFromScene | |
private void GetListByTagsFromProject() | |
{ | |
//Get every gameObject from project folder root Assets | |
GameObject[] _aGO_prefabs = GetAllGameObjects(eMainDirectory.Project); | |
//Get genuine tags list | |
_as_tags = UnityEditorInternal.InternalEditorUtility.tags; | |
//Setup temp list | |
List<GameObject>[] _aLGO_GOTag = new List<GameObject>[_as_tags.Length]; | |
//Loop through every gameobjects | |
for(int i = 0; i < _aGO_prefabs.Length; i++) | |
{ | |
//Get gameObject.tag'index | |
int _i_tagIdx = TagToIndex(_aGO_prefabs[i].tag); | |
//Not already a List ? | |
if(_aLGO_GOTag[_i_tagIdx] == null) _aLGO_GOTag[_i_tagIdx] = new List<GameObject>(); | |
//Not already in the list ? | |
if(!_aLGO_GOTag[_i_tagIdx].Contains(_aGO_prefabs[i])) | |
{ | |
//Add gameObject to list | |
_aLGO_GOTag[_i_tagIdx].Add(_aGO_prefabs[i]); | |
} | |
}//for() | |
//Fill-in empty list | |
for(int j = 0; j < _aLGO_GOTag.Length; j++) | |
{ | |
if(_aLGO_GOTag[j] == null) _aLGO_GOTag[j] = new List<GameObject>(); | |
}//for() | |
//Re-init main array | |
_aaGO_tags = new GameObject[_aLGO_GOTag.Length][]; | |
//Re-init bool foldout array | |
_ab_foldout_tags = new bool[_aLGO_GOTag.Length]; | |
//Fill-in main 2D array | |
for(int k = 0; k < _aLGO_GOTag.Length; k++) | |
{ | |
//Setup foldout bool array | |
_ab_foldout_tags[k] = false; | |
//Fill-in main 2D array | |
if(_aLGO_GOTag[k] != null) _aaGO_tags[k] = _aLGO_GOTag[k].ToArray(); | |
}//for() | |
}// GetListByTagsFromProject | |
private void GetListByLayersFromScene() | |
{ | |
//Setup main 2D array | |
_aaGO_layers = new GameObject[32][]; | |
//Setup foldout array | |
_ab_foldout_layers = new bool[32]; | |
//Setup temp. array(static layers number)/list(dynamic fill-in gameobjects) | |
List<GameObject>[] _aLGO = new List<GameObject>[32]; | |
//Get all gameobjects from scene | |
GameObject[] _aGO = GetAllGameObjects(eMainDirectory.Scene); | |
//Fill-in dynamic list | |
for(int j = 0; j < _aGO.Length; j++) | |
{ | |
int _i_idx = _aGO[j].layer; | |
if(_aLGO[_i_idx] == null) _aLGO[_i_idx] = new List<GameObject>(); | |
_aLGO[_i_idx].Add(_aGO[j]); | |
}//for() | |
//Format dynamic list into static array | |
for(int k = 0; k < 32; k++) | |
{ | |
if(_aLGO[k] != null) | |
{ | |
_aaGO_layers[k] = _aLGO[k].ToArray(); | |
} | |
_ab_foldout_layers[k] = false; | |
}//for() | |
}// GetListByLayersFromScene | |
private void GetListByLayersFromProject() | |
{ | |
//Get every gameObject from project folder root Assets | |
GameObject[] _aGO_prefabs = GetAllGameObjects(eMainDirectory.Project); | |
//Setup temp list | |
List<GameObject>[] _aLGO_GOLayer = new List<GameObject>[32]; | |
//Loop through every gameobjects | |
for(int i = 0; i < _aGO_prefabs.Length; i++) | |
{ | |
//Get gameObject.layer'index | |
int _i_tagIdx = _aGO_prefabs[i].layer; | |
//Not already a List ? | |
if(_aLGO_GOLayer[_i_tagIdx] == null) _aLGO_GOLayer[_i_tagIdx] = new List<GameObject>(); | |
//Not already in the list ? | |
if(!_aLGO_GOLayer[_i_tagIdx].Contains(_aGO_prefabs[i])) | |
{ | |
//Add gameObject to list | |
_aLGO_GOLayer[_i_tagIdx].Add(_aGO_prefabs[i]); | |
} | |
}//for() | |
//Re-init main array | |
_aaGO_layers = new GameObject[32][]; | |
//Re-init bool foldout array | |
_ab_foldout_layers = new bool[32]; | |
//Fill-in main 2D array | |
for(int j = 0; j < _aLGO_GOLayer.Length; j++) | |
{ | |
//Setup foldout bool array | |
_ab_foldout_layers[j] = false; | |
if(_aLGO_GOLayer[j] != null) _aaGO_layers[j] = _aLGO_GOLayer[j].ToArray(); | |
}//for() | |
}// GetListByLayersFromProject | |
private GameObject[] GetAllGameObjects(eMainDirectory _eMD_dir) | |
{ | |
GameObject[] _aGO_GOs = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject)); | |
switch(_eMD_dir) | |
{ | |
//|| SCENE || | |
case eMainDirectory.Scene: | |
{ | |
//Trim so we get only gameobjects from scene | |
List<GameObject> _LGO_sceneGOs = new List<GameObject>(); | |
for(int i = 0; i < _aGO_GOs.Length; i++) | |
{ | |
PrefabType _PT_type = PrefabUtility.GetPrefabType(_aGO_GOs[i]); | |
//No genuine Prefab or genuine ModelPrefab | |
if(_PT_type != PrefabType.Prefab && _PT_type != PrefabType.ModelPrefab) | |
{ | |
//OK ! | |
_LGO_sceneGOs.Add(_aGO_GOs[i]); | |
} | |
}//for() | |
_aGO_GOs = _LGO_sceneGOs.ToArray(); | |
}break; | |
//|| PROJECT || | |
case eMainDirectory.Project: | |
{ | |
//Trim so we get only gameobjects from asset folder | |
List<GameObject> _LGO_projectGOs = new List<GameObject>(); | |
for(int i = 0; i < _aGO_GOs.Length; i++) | |
{ | |
PrefabType _PT_type = PrefabUtility.GetPrefabType(_aGO_GOs[i]); | |
//Only genuine Prefab or genuine ModelPrefab | |
if(_PT_type == PrefabType.Prefab || _PT_type == PrefabType.ModelPrefab) | |
{ | |
//OK ! | |
_LGO_projectGOs.Add(_aGO_GOs[i]); | |
} | |
}//for() | |
_aGO_GOs = _LGO_projectGOs.ToArray(); | |
}break; | |
default:break; | |
}//switch(_eMD_dir) | |
return _aGO_GOs; | |
}// GetAllGameObjects | |
private int TagToIndex(string _s_tag) | |
{ | |
int _i_index = -1; | |
string[] _as_tags = UnityEditorInternal.InternalEditorUtility.tags; | |
for(int i = 0; i < _as_tags.Length; i++) | |
{ | |
//Found tag ? | |
if(_i_index < 0 && string.Compare(_s_tag, _as_tags[i]) == 0) | |
{ | |
_i_index = i; | |
} | |
}//for() | |
return _i_index; | |
}// TagToIndex | |
private string IndexToTag(int _i_index) | |
{ | |
string _s_tag = ""; | |
string[] _as_tags = UnityEditorInternal.InternalEditorUtility.tags; | |
if(_i_index >= 0 && _i_index < _as_tags.Length) | |
{ | |
_s_tag = _as_tags[_i_index]; | |
} | |
return _s_tag; | |
}// IndexToTag | |
}// SearchTagLayer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment