Skip to content

Instantly share code, notes, and snippets.

@PhilippCh
Last active September 22, 2023 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PhilippCh/af71c00a20786495c4315b3c42e687a2 to your computer and use it in GitHub Desktop.
Save PhilippCh/af71c00a20786495c4315b3c42e687a2 to your computer and use it in GitHub Desktop.
Adds a context (right-click) menu to Unity's scene hierarchy tab that allows for quick grouping of selected GameObjects under a new parent.
using UnityEditor;
using UnityEngine;
#if UNITY_EDITOR
namespace CelestialStatic.Editor {
public class GameObjectGrouping {
[MenuItem("GameObject/Group selected GameObjects", false, 0)]
public static void GroupSelected(MenuCommand menuCommand) {
//Prevent executing multiple times when right-clicking.
if (Time.unscaledTime.Equals(_lastMenuCallTimestamp)) {
return;
}
var parent = new GameObject();
var selected = Selection.gameObjects;
foreach (var gameObject in selected) {
gameObject.transform.SetParent(parent.transform);
}
Selection.activeGameObject = parent;
_lastMenuCallTimestamp = Time.unscaledTime;
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment