Advertisement
MeekoSoup

Centered Parent Menu

Jun 4th, 2025
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class CenteredParentMenu
  5. {
  6.     [MenuItem("GameObject/Create Centered Empty Parent", false, 0)]
  7.     static void CreateCenteredEmptyParent(MenuCommand menuCommand)
  8.     {
  9.         // Run the operation only if it's triggered on the first selected object
  10.         if (menuCommand.context != Selection.activeTransform.gameObject)
  11.         {
  12.             return;
  13.         }
  14.  
  15.         // Get the selected objects in the hierarchy
  16.         GameObject[] selectedObjects = Selection.gameObjects;
  17.  
  18.         if (selectedObjects.Length == 0)
  19.         {
  20.             Debug.LogWarning("No objects selected. Please select at least one object in the hierarchy.");
  21.             return;
  22.         }
  23.  
  24.         // Get the parent of the first selected object (assuming all selected objects share the same parent)
  25.         Transform originalParent = selectedObjects[0].transform.parent;
  26.  
  27.         // Validate that all selected objects share the same parent
  28.         foreach (GameObject go in selectedObjects)
  29.         {
  30.             if (go.transform.parent != originalParent)
  31.             {
  32.                 Debug.LogWarning("Cannot select children from different family trees.");
  33.                 return;
  34.             }
  35.         }
  36.  
  37.         // Create the new parent GameObject and register it in the Undo system
  38.         GameObject parentObject = new GameObject("CenteredParent");
  39.         Undo.RegisterCreatedObjectUndo(parentObject, "Create Centered Empty Parent");
  40.  
  41.         // Calculate the center of all selected objects
  42.         Vector3 center = Vector3.zero;
  43.         foreach (GameObject obj in selectedObjects)
  44.         {
  45.             center += obj.transform.position;
  46.         }
  47.         center /= selectedObjects.Length;
  48.  
  49.         // Set the new parent object's position to the calculated center
  50.         parentObject.transform.position = center;
  51.  
  52.         // Set the new parent object under the original parent
  53.         if (originalParent != null)
  54.         {
  55.             Undo.SetTransformParent(parentObject.transform, originalParent, "Reparent Centered Empty");
  56.         }
  57.  
  58.         // Move all selected objects under the new parent and preserve their world positions
  59.         foreach (GameObject obj in selectedObjects)
  60.         {
  61.             Undo.SetTransformParent(obj.transform, parentObject.transform, "Parent Selected Objects");
  62.         }
  63.  
  64.         // Ensure that after undo, the objects retain their original positions
  65.         foreach (GameObject obj in selectedObjects)
  66.         {
  67.             Undo.RecordObject(obj.transform, "Move Object");
  68.         }
  69.  
  70.         // Select the new parent object in the hierarchy
  71.         Selection.activeGameObject = parentObject;
  72.     }
  73. }
  74.  
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement