Unity Hierarchy Cleaning/Grouping Tool
For our class Rapid Prototyping we got the assignment to make a tool for unity, based on what we would like from our previous experiences working with others. I personally noticed that in larger projects I worked on the hierarchy becomes quite a mess after a while and so I decided to make a tool to help with that.
Because this was for Rapid Prototyping we could only work on it for 90 minutes a week and we only have about 4 weeks in total to finish our tool. We had to make this tool during the second period of our second year, this was around January/February.
Challenges:
It was my first time working with Unity Tools, I used separate windows and made new hotkeys.
Specifications:
The tool was made in Unity with C#.
[MenuItem("Window/Hierarchy Cleaner")]
static void Init()
{
CleanerWindow window = (CleanerWindow)EditorWindow.GetWindow(typeof(CleanerWindow));
window.Show();
}
private void OnGUI()
{
_UseExistingParent = EditorGUILayout.Toggle("Use existing Parent?", _UseExistingParent);
_TagName = EditorGUILayout.TagField("Tag to clean: ", _TagName);
if (_UseExistingParent == false)
{
_ParentName = EditorGUILayout.TextField("Name of new Parent Object", _ParentName);
if (GUILayout.Button("Spawn Parent"))
{
GameObject obj = new GameObject();
obj.name = _ParentName;
}
}
if (_UseExistingParent == true)
{
_ParentObject = (GameObject)EditorGUILayout.ObjectField("Parent Object", _ParentObject, typeof(GameObject), true);
if(_ParentObject != null)
{
_ParentName = _ParentObject.name;
}
}
if (GUILayout.Button("Clean!"))
{
List<GameObject> gameObjects = new List<GameObject>();
GameObject go = GameObject.Find(_ParentName);
foreach (GameObject obj in GameObject.FindGameObjectsWithTag(_TagName))
{
gameObjects.Add(obj);
}
for(int i = 0; i < gameObjects.Count; i++)
{
gameObjects[i].transform.parent = go.transform;
}
}
}
[MenuItem("Cleaner/Create New Object For Parenting %_#_g")]
private static void ParentToNewObject()
{
List<GameObject> objects = new List<GameObject>();
for(int i = 0; i < Selection.gameObjects.Length; i++)
{
objects.Add(Selection.gameObjects[i]);
}
GameObject go = new GameObject();
for(int i = 0; i < objects.Count; i++)
{
objects[i].transform.parent = go.transform;
}
go.name = objects[0].name + "s";
}
[MenuItem("Cleaner/Use Existing Object For Parenting #_g")]
private static void ParentToExistingObject()
{
List<GameObject> objects = new List<GameObject>();
for (int i = 0; i < Selection.gameObjects.Length; i++)
{
objects.Add(Selection.gameObjects[i]);
}
GameObject go = objects[0];
for(int i = 1; i < objects.Count; i++)
{
objects[i].transform.parent = go.transform;
}
}