Skip to content

Instantly share code, notes, and snippets.

@jbvrtx
Last active June 12, 2023 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbvrtx/e81908a9be2e5e811bfce99516f022a7 to your computer and use it in GitHub Desktop.
Save jbvrtx/e81908a9be2e5e811bfce99516f022a7 to your computer and use it in GitHub Desktop.
Basis for Editor Tools in Unity Editor. Accumulate Components in selected GameObjects to process them in bulk.
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace TXL
{
/// <summary>
/// Editor script base class for collecting and processing Unity components based on a selection of GameObjects.
/// </summary>
public abstract class ComponentBulkOperationBase : EditorWindow
{
protected static List<GameObject> _selectedGameObjects = new List<GameObject>();
protected static bool _recursive = false;
protected static bool _verbose = false;
protected virtual void OnGUI()
{
// Recursive?
GUILayout.Space(10);
GUILayout.Label("Include children of selected objects", EditorStyles.boldLabel);
_recursive = EditorGUILayout.Toggle("Recursive", _recursive);
// Verbose?
GUILayout.Space(10);
GUILayout.Label("Activate Verbose Output", EditorStyles.boldLabel);
_verbose = EditorGUILayout.Toggle("Verbose Output", _verbose);
}
/// <summary>
/// For all selected GameObjects, get the components of the specified Type. If recursive is specified, all children
/// of the selected GameObjects are searched as well.
/// </summary>
protected List<T> AccumulateComponents<T>() where T : Component
{
_selectedGameObjects = UnityEditor.Selection.gameObjects.ToList();
List<T> componentList = new List<T>();
foreach (GameObject gameObject in _selectedGameObjects)
{
if (_recursive)
{
// Get all LOD Groups Components from the GameObjects and their children
List<Component> gatheredComponents = gameObject.GetComponentsInChildren(typeof(T), true).ToList();
componentList.AddRange(gatheredComponents.Cast<T>());
}
else
{
// Check the direct children of every selected object
foreach (Transform child in gameObject.transform)
{
T childLODGroup = child.GetComponent<T>();
if (childLODGroup != null && !componentList.Contains(childLODGroup))
componentList.Add(childLODGroup);
}
// Also check the selected object itself
T objectLODGroup = gameObject.GetComponent<T>();
if (objectLODGroup != null && !componentList.Contains(objectLODGroup))
componentList.Add(objectLODGroup);
}
}
return componentList.Distinct().ToList();
}
/// <summary>
/// Returns GameObject Path
/// </summary>
protected static string GetGameObjectPath(Transform transform)
{
string path = transform.name;
while (transform.parent != null)
{
transform = transform.parent;
path = transform.name + "/" + path;
}
return path;
}
protected static string GetGameObjectPath(GameObject gameObject)
{
return GetGameObjectPath(gameObject.transform);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment