Created
August 28, 2026 04:14
-
-
Save OwenTheProgrammer/7aba2426286d628b8378a49d1bc755c9 to your computer and use it in GitHub Desktop.
Unity Reparent To Selection
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 System.Linq; | |
| using UnityEditor; | |
| using UnityEngine; | |
| [InitializeOnLoad] | |
| public class ReparentToSelected | |
| { | |
| private static GameObject[] _lastSelection; | |
| private static GameObject[] _secondLastSelection; | |
| static ReparentToSelected() | |
| { | |
| Selection.selectionChanged += OnSelectionChanged; | |
| } | |
| private static void OnSelectionChanged() | |
| { | |
| _secondLastSelection = _lastSelection; | |
| _lastSelection = Selection.gameObjects; | |
| } | |
| [MenuItem("GameObject/Reparent To Selected", false, 0)] | |
| public static void InvokeAction() | |
| { | |
| GameObject parent = Selection.activeGameObject; | |
| Undo.SetCurrentGroupName("Reparent to Selected"); | |
| int group = Undo.GetCurrentGroup(); | |
| { | |
| foreach (GameObject child in _secondLastSelection) | |
| { | |
| Undo.SetTransformParent( | |
| child.transform, | |
| parent.transform, | |
| "Reparent to Selected" | |
| ); | |
| } | |
| } | |
| Undo.CollapseUndoOperations(group); | |
| Selection.objects = _secondLastSelection.Cast<Object>().ToArray(); | |
| } | |
| [MenuItem("GameObject/Reparent To Selected", true)] | |
| public static bool ValidateInvokeAction() | |
| { | |
| if (_secondLastSelection == null) | |
| return false; | |
| if(_lastSelection == null || _lastSelection.Length != 1) | |
| return false; | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment