Hi,
I try to make a script for showing average bounding box of all mesh inside a prefab with measure.
My problem is when I rotate the mesh. Bounding box didnt follow the mesh and stretch to fit the mesh size.
If anyone have a solution of this problem. Here code for bounding box.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[ExecuteAlways]
public class BoundingBoxShaderUpdate : MonoBehaviour
{
public bool scriptEnabled = true; // Toggle for enabling/disabling the script
public bool useDashedLines = false; // Toggle for using dashed or solid lines for bounding box
public Color boundingBoxColor = Color.red; // Color for the bounding box
public float dashedLineGap = 0.1f; // Gap between dashed lines
public bool showBoundingBox = true;
public bool showMeasurements = true; // Toggle for showing/hiding measurements
public bool useMeters = true; // Toggle for using meters or centimeters in measurements
public float meterToCentimeter = 100f; // Conversion factor from meters to centimeters
public Font measurementFont; // Font for the measurement label
public int fontSize = 12; // Font size for the measurement label
#if UNITY_EDITOR
[InitializeOnLoadMethod]
static void StartEditorUpdate()
{
EditorApplication.update += EditorUpdate;
}
static void EditorUpdate()
{
SceneView.RepaintAll();
}
#endif
void OnDrawGizmos()
{
if (!scriptEnabled) return; // If script is disabled, exit without drawing anything
if (showBoundingBox)
DrawBoundingBox();
if (showMeasurements)
DrawMeasurements();
}
void OnDrawGizmosSelected()
{
if (!scriptEnabled) return; // If script is disabled, exit without drawing anything
if (showBoundingBox)
DrawBoundingBox();
if (showMeasurements)
DrawMeasurements();
}
void DrawBoundingBox()
{
Renderer[] childRenderers = GetComponentsInChildren<Renderer>();
if (childRenderers.Length > 0)
{
Bounds bounds = new Bounds();
bool initialized = false;
foreach (Renderer renderer in childRenderers)
{
if (!initialized)
{
bounds = renderer.bounds;
initialized = true;
}
else
{
bounds.Encapsulate(renderer.bounds);
}
}
Gizmos.color = boundingBoxColor; // Set the color for the bounding box
if (useDashedLines)
DrawDashedWireCube(bounds.center, bounds.size); // Draw the bounding box with dashed lines
else
DrawSolidWireCube(bounds.center, bounds.size); // Draw the bounding box with solid lines
}
else
{
Debug.LogWarning("No child objects with Renderer components found.");
}
}
void DrawMeasurements()
{
Renderer[] childRenderers = GetComponentsInChildren<Renderer>();
if (childRenderers.Length > 0)
{
Bounds bounds = new Bounds();
bool initialized = false;
foreach (Renderer renderer in childRenderers)
{
if (!initialized)
{
bounds = renderer.bounds;
initialized = true;
}
else
{
bounds.Encapsulate(renderer.bounds);
}
}
// Draw measurements for each axis
if (useMeters)
{
DrawAxisMeasurement(bounds.center + new Vector3(0, bounds.size.y / 2, 0), bounds.size.y, "Height", Color.green); // Height (Y-axis) at the top
DrawAxisMeasurement(bounds.center + new Vector3(bounds.size.x / 2, 0, 0), bounds.size.z, "Length", Color.blue); // Width (X-axis) on the right
DrawAxisMeasurement(bounds.center + new Vector3(0, 0, bounds.size.z / 2), bounds.size.x, "Width", Color.red); // Length (Z-axis) at the front
}
else
{
DrawAxisMeasurement(bounds.center + new Vector3(0, bounds.size.y / 2, 0), bounds.size.y * meterToCentimeter, "Height", Color.green); // Height (Y-axis) at the top
DrawAxisMeasurement(bounds.center + new Vector3(bounds.size.x / 2, 0, 0), bounds.size.z * meterToCentimeter, "Length", Color.blue); // Width (X-axis) on the right
DrawAxisMeasurement(bounds.center + new Vector3(0, 0, bounds.size.z / 2), bounds.size.x * meterToCentimeter, "Width", Color.red); // Length (Z-axis) at the front
}
}
else
{
Debug.LogWarning("No child objects with Renderer components found.");
}
}
void DrawSolidWireCube(Vector3 center, Vector3 size)
{
Gizmos.color = boundingBoxColor; // Set the color for the bounding box
Gizmos.DrawWireCube(center, size);
}
void DrawDashedWireCube(Vector3 center, Vector3 size)
{
Vector3[] vertices = new Vector3[]
{
new Vector3(center.x - size.x / 2, center.y + size.y / 2, center.z + size.z / 2),
new Vector3(center.x + size.x / 2, center.y + size.y / 2, center.z + size.z / 2),
new Vector3(center.x + size.x / 2, center.y + size.y / 2, center.z - size.z / 2),
new Vector3(center.x - size.x / 2, center.y + size.y / 2, center.z - size.z / 2),
new Vector3(center.x - size.x / 2, center.y - size.y / 2, center.z + size.z / 2),
new Vector3(center.x + size.x / 2, center.y - size.y / 2, center.z + size.z / 2),
new Vector3(center.x + size.x / 2, center.y - size.y / 2, center.z - size.z / 2),
new Vector3(center.x - size.x / 2, center.y - size.y / 2, center.z - size.z / 2)
};
for (int i = 0; i < 4; i++)
{
DrawDashedLine(vertices[i], vertices[(i + 1) % 4], dashedLineGap); // Top square
DrawDashedLine(vertices[i + 4], vertices[(i + 1) % 4 + 4], dashedLineGap); // Bottom square
DrawDashedLine(vertices[i], vertices[i + 4], dashedLineGap); // Connecting lines
}
}
void DrawDashedLine(Vector3 start, Vector3 end, float gap)
{
float length = (end - start).magnitude;
int segments = Mathf.CeilToInt(length / gap);
Vector3 delta = (end - start) / segments;
for (int i = 0; i < segments; i += 2)
{
Gizmos.DrawLine(start + delta * i, start + delta * (i + 1));
}
}
void DrawAxisMeasurement(Vector3 position, float size, string axis, Color textColor)
{
GUIStyle style = new GUIStyle();
style.normal.textColor = textColor;
style.font = measurementFont; // Set the font
style.fontSize = fontSize; // Set the font size
string measurementText;
if (useMeters)
{
measurementText = $" {axis} = {size:F2} m"; // Display size in meters
}
else
{
measurementText = $" {axis} = {size:F2} cm"; // Display size in centimeters
}
Handles.Label(position, measurementText, style);
}
}