MaximilianPs

ObjectsSelector

Nov 8th, 2025 (edited)
197
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.94 KB | None | 0 0
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. #endif
  6.  
  7. [ExecuteAlways]
  8. public class ObjectsSelector : MonoBehaviour
  9. {
  10.     [SerializeField] private Color selectedColor = Color.green;
  11.     [SerializeField] private Color unselectedColor = new Color(0f, 0.5f, 0.5f, 0.6f); // corretto: verde scuro trasparente
  12.     [SerializeField, Range(0f, 25f)] private float lineThickness = 2f;
  13.  
  14.     [SerializeField, HideInInspector] private Vector3 cubeSize;
  15.     [Header("Use the context menu\n (right mouse button on the title)\n to select or parent the objects inside the cube")]
  16.     [SerializeField] private string parentName = "GroupedObjects";
  17.  
  18.     private BoxCollider _boxCollider;
  19.  
  20.     private void OnEnable()
  21.     {
  22.         EnsureBoxColliderExists();
  23.     }
  24.  
  25.     private void OnValidate()
  26.     {
  27.         EnsureBoxColliderExists();
  28.     }
  29.  
  30.     private void EnsureBoxColliderExists()
  31.     {
  32.         _boxCollider = GetComponent<BoxCollider>();
  33.         if (_boxCollider == null)
  34.         {
  35.             _boxCollider = gameObject.AddComponent<BoxCollider>();
  36.             _boxCollider.isTrigger = true;
  37.         }
  38.     }
  39.  
  40.     private void OnDrawGizmos()
  41.     {
  42.         if (_boxCollider == null)
  43.             EnsureBoxColliderExists();
  44.  
  45.         if (_boxCollider == null) return;
  46.  
  47.         Vector3 worldCenter = transform.TransformPoint(_boxCollider.center);
  48.         Vector3 size = _boxCollider.size;
  49.         cubeSize = size;
  50.  
  51.         Color drawColor = GetDrawColor();
  52.         DrawWireCube(worldCenter, size, drawColor, lineThickness);
  53.     }
  54.  
  55.     private Color GetDrawColor()
  56.     {
  57. #if UNITY_EDITOR
  58.         if (Selection.activeGameObject == gameObject)
  59.             return selectedColor;
  60. #endif
  61.         return unselectedColor;
  62.     }
  63.  
  64.     void DrawWireCube(Vector3 center, Vector3 size, Color color, float thickness)
  65.     {
  66.         Vector3 half = size * 0.5f;
  67.  
  68.         Vector3[] corners = new Vector3[8]
  69.         {
  70.             center + new Vector3(-half.x, -half.y, -half.z),
  71.             center + new Vector3( half.x, -half.y, -half.z),
  72.             center + new Vector3( half.x, -half.y,  half.z),
  73.             center + new Vector3(-half.x, -half.y,  half.z),
  74.             center + new Vector3(-half.x,  half.y, -half.z),
  75.             center + new Vector3( half.x,  half.y, -half.z),
  76.             center + new Vector3( half.x,  half.y,  half.z),
  77.             center + new Vector3(-half.x,  half.y,  half.z)
  78.         };
  79.  
  80.         int[,] edges = new int[,]
  81.         {
  82.             {0,1}, {1,2}, {2,3}, {3,0},
  83.             {4,5}, {5,6}, {6,7}, {7,4},
  84.             {0,4}, {1,5}, {2,6}, {3,7}
  85.         };
  86.  
  87.         for (int i = 0; i < edges.GetLength(0); i++)
  88.         {
  89.             DrawThickLine(corners[edges[i, 0]], corners[edges[i, 1]], color, thickness);
  90.         }
  91.     }
  92.  
  93.     void DrawThickLine(Vector3 start, Vector3 end, Color color, float thickness)
  94.     {
  95. #if UNITY_EDITOR
  96.         Handles.color = color;
  97.         Handles.DrawAAPolyLine(thickness, new Vector3[] { start, end });
  98. #endif
  99.     }
  100.  
  101. #if UNITY_EDITOR
  102.     [ContextMenu("Select Active Objects Inside Trigger")]
  103.     void SelectActiveObjectsInTrigger()
  104.     {
  105.         if (_boxCollider == null)
  106.         {
  107.             Debug.LogWarning("No BoxCollider found!");
  108.             return;
  109.         }
  110.  
  111.         List<GameObject> activeObjects = new List<GameObject>();
  112.  
  113.         Collider[] overlapping = Physics.OverlapBox(transform.TransformPoint(_boxCollider.center),
  114.             _boxCollider.size * 0.5f,
  115.             transform.rotation,
  116.             ~0,
  117.             QueryTriggerInteraction.Collide
  118.         );
  119.  
  120.         foreach (var col in overlapping)
  121.         {
  122.             GameObject go = col.gameObject;
  123.             // Ignora self e oggetti disattivati
  124.             if (go == gameObject) continue;
  125.             if (!go.activeInHierarchy) continue;
  126.  
  127.             activeObjects.Add(go);
  128.         }
  129.  
  130.         if (activeObjects.Count == 0)
  131.         {
  132.             Debug.Log("No active objects found inside the trigger.");
  133.             Selection.objects = new Object[0];
  134.         }
  135.         else
  136.         {
  137.             Selection.objects = activeObjects.ToArray();
  138.             Debug.Log($"Selected {activeObjects.Count} active object(s).");
  139.         }
  140.     }
  141.  
  142.     [ContextMenu("Group Selected Objects Under New Parent")]
  143.     void GroupSelectedObjects()
  144.     {
  145.         GameObject[] selected = Selection.gameObjects;
  146.         if (selected.Length == 0)
  147.         {
  148.             Debug.LogWarning("No objects selected.");
  149.             return;
  150.         }
  151.  
  152.         string name = string.IsNullOrEmpty(parentName) ? "GroupedObjects" : parentName;
  153.  
  154.         GameObject parent = new GameObject(name);
  155.  
  156.         Undo.RegisterCreatedObjectUndo(parent, "Create Group Parent");
  157.  
  158.         foreach (GameObject go in selected)
  159.         {
  160.             Undo.SetTransformParent(go.transform, parent.transform, "Parent Selected Objects");
  161.         }
  162.  
  163.         Selection.activeObject = parent;
  164.     }
  165. #endif
  166. }
Tags: Unity utility
Advertisement
Comments
  • MaximilianPs
    12 days
    # text 0.20 KB | 0 0
    1. Create an empty object and add this class.
    2. resize the box trigger as you wish, then you can use the RMB on the class title to select all the objects inside the trigger or parent it into an empty object.
Add Comment
Please, Sign In to add comment