Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- #if UNITY_EDITOR
- using UnityEditor;
- using System.Collections.Generic;
- #endif
- [ExecuteAlways]
- public class ObjectsSelector : MonoBehaviour
- {
- [SerializeField] private Color selectedColor = Color.green;
- [SerializeField] private Color unselectedColor = new Color(0f, 0.5f, 0.5f, 0.6f); // corretto: verde scuro trasparente
- [SerializeField, Range(0f, 25f)] private float lineThickness = 2f;
- [SerializeField, HideInInspector] private Vector3 cubeSize;
- [Header("Use the context menu\n (right mouse button on the title)\n to select or parent the objects inside the cube")]
- [SerializeField] private string parentName = "GroupedObjects";
- private BoxCollider _boxCollider;
- private void OnEnable()
- {
- EnsureBoxColliderExists();
- }
- private void OnValidate()
- {
- EnsureBoxColliderExists();
- }
- private void EnsureBoxColliderExists()
- {
- _boxCollider = GetComponent<BoxCollider>();
- if (_boxCollider == null)
- {
- _boxCollider = gameObject.AddComponent<BoxCollider>();
- _boxCollider.isTrigger = true;
- }
- }
- private void OnDrawGizmos()
- {
- if (_boxCollider == null)
- EnsureBoxColliderExists();
- if (_boxCollider == null) return;
- Vector3 worldCenter = transform.TransformPoint(_boxCollider.center);
- Vector3 size = _boxCollider.size;
- cubeSize = size;
- Color drawColor = GetDrawColor();
- DrawWireCube(worldCenter, size, drawColor, lineThickness);
- }
- private Color GetDrawColor()
- {
- #if UNITY_EDITOR
- if (Selection.activeGameObject == gameObject)
- return selectedColor;
- #endif
- return unselectedColor;
- }
- void DrawWireCube(Vector3 center, Vector3 size, Color color, float thickness)
- {
- Vector3 half = size * 0.5f;
- Vector3[] corners = new Vector3[8]
- {
- center + new Vector3(-half.x, -half.y, -half.z),
- center + new Vector3( half.x, -half.y, -half.z),
- center + new Vector3( half.x, -half.y, half.z),
- center + new Vector3(-half.x, -half.y, half.z),
- center + new Vector3(-half.x, half.y, -half.z),
- center + new Vector3( half.x, half.y, -half.z),
- center + new Vector3( half.x, half.y, half.z),
- center + new Vector3(-half.x, half.y, half.z)
- };
- int[,] edges = new int[,]
- {
- {0,1}, {1,2}, {2,3}, {3,0},
- {4,5}, {5,6}, {6,7}, {7,4},
- {0,4}, {1,5}, {2,6}, {3,7}
- };
- for (int i = 0; i < edges.GetLength(0); i++)
- {
- DrawThickLine(corners[edges[i, 0]], corners[edges[i, 1]], color, thickness);
- }
- }
- void DrawThickLine(Vector3 start, Vector3 end, Color color, float thickness)
- {
- #if UNITY_EDITOR
- Handles.color = color;
- Handles.DrawAAPolyLine(thickness, new Vector3[] { start, end });
- #endif
- }
- #if UNITY_EDITOR
- [ContextMenu("Select Active Objects Inside Trigger")]
- void SelectActiveObjectsInTrigger()
- {
- if (_boxCollider == null)
- {
- Debug.LogWarning("No BoxCollider found!");
- return;
- }
- List<GameObject> activeObjects = new List<GameObject>();
- Collider[] overlapping = Physics.OverlapBox(transform.TransformPoint(_boxCollider.center),
- _boxCollider.size * 0.5f,
- transform.rotation,
- ~0,
- QueryTriggerInteraction.Collide
- );
- foreach (var col in overlapping)
- {
- GameObject go = col.gameObject;
- // Ignora self e oggetti disattivati
- if (go == gameObject) continue;
- if (!go.activeInHierarchy) continue;
- activeObjects.Add(go);
- }
- if (activeObjects.Count == 0)
- {
- Debug.Log("No active objects found inside the trigger.");
- Selection.objects = new Object[0];
- }
- else
- {
- Selection.objects = activeObjects.ToArray();
- Debug.Log($"Selected {activeObjects.Count} active object(s).");
- }
- }
- [ContextMenu("Group Selected Objects Under New Parent")]
- void GroupSelectedObjects()
- {
- GameObject[] selected = Selection.gameObjects;
- if (selected.Length == 0)
- {
- Debug.LogWarning("No objects selected.");
- return;
- }
- string name = string.IsNullOrEmpty(parentName) ? "GroupedObjects" : parentName;
- GameObject parent = new GameObject(name);
- Undo.RegisterCreatedObjectUndo(parent, "Create Group Parent");
- foreach (GameObject go in selected)
- {
- Undo.SetTransformParent(go.transform, parent.transform, "Parent Selected Objects");
- }
- Selection.activeObject = parent;
- }
- #endif
- }
Advertisement
Comments
-
- Create an empty object and add this class.
- 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