-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxColliderVisualizer.cs
More file actions
46 lines (36 loc) · 1.62 KB
/
BoxColliderVisualizer.cs
File metadata and controls
46 lines (36 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using Sirenix.OdinInspector;
using UnityEngine;
public class BoxColliderVisualizer : MonoBehaviour
{
// Parent GameObject to hold all the generated boxes
public GameObject boxParent;
void Start()
{
}
[Button("Generate Boxes")]
public void GenerateBoxColliderObjects()
{
// Create a parent GameObject to hold all the generated box objects
boxParent = new GameObject("BoxCollidersParent");
// Find all BoxColliders in the scene, including inactive ones
BoxCollider[] allBoxColliders = FindObjectsOfType<BoxCollider>(true);
foreach (BoxCollider boxCollider in allBoxColliders)
{
GenerateBoxFromCollider(boxCollider);
}
}
// This function generates a visual box object from a BoxCollider
void GenerateBoxFromCollider(BoxCollider boxCollider)
{
// Create a new GameObject to represent the BoxCollider
GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube);
// Set the new GameObject's transform to match the BoxCollider
box.transform.position = boxCollider.transform.TransformPoint(boxCollider.center);
box.transform.rotation = boxCollider.transform.rotation;
box.transform.localScale = Vector3.Scale(boxCollider.size, boxCollider.transform.lossyScale);
// Set the new box as a child of the parent object for easy deletion
box.transform.parent = boxParent.transform;
// Disable the BoxCollider on the new GameObject (since we just want the visual)
Destroy(box.GetComponent<BoxCollider>());
}
}