Skip to content

Instantly share code, notes, and snippets.

@ThaDewey
Created January 2, 2018 15:23
Show Gist options
  • Save ThaDewey/bc2de418ee2d04998b0fd9965fa053be to your computer and use it in GitHub Desktop.
Save ThaDewey/bc2de418ee2d04998b0fd9965fa053be to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class DimensionsTool : EditorWindow {
Bounds bounds = new Bounds();
long polygons = 0;
int count = 0;
bool drawBox = false;
SelectionMode sm = SelectionMode.Deep | SelectionMode.Editable;
Vector3 centerPoint;
// Add menu item named "My Window" to the Window menu
[MenuItem("Window/Dimension Tool")]
public static void Init() {
//Show existing window instance. If one doesn't exist, make one.
DimensionsTool window = (DimensionsTool)EditorWindow.GetWindow(typeof(DimensionsTool));
SceneView.onSceneGUIDelegate -= window.OnSceneGUI;
SceneView.onSceneGUIDelegate += window.OnSceneGUI;
window.Show();
}
void OnEnable() {
SceneView.onSceneGUIDelegate += OnSceneGUI;
}
void OnDisable() {
SceneView.onSceneGUIDelegate -= OnSceneGUI;
}
void OnSelectionChange() {
MeshFilter[] filters = Selection.GetFiltered<MeshFilter>(sm);
MeshRenderer[] renderers = Selection.GetFiltered<MeshRenderer>(sm);
bounds = new Bounds();
polygons = 0;
count = 0;
centerPoint = Vector3.zero;
/*foreach(MeshRenderer mr in renderers) {
bounds.Encapsulate(mr.bounds);
}*/
if (renderers.Length > 0) {
foreach(MeshFilter mf in filters) {
polygons += mf.sharedMesh.triangles.LongLength;
}
foreach (MeshRenderer r in renderers) {
count++;
if (r.bounds != null) {
centerPoint += r.transform.position;
}
}
centerPoint /= count;
bounds.center = centerPoint;
foreach (MeshRenderer r in renderers) {
if (r.bounds != null) {
Bounds temp = r.bounds;
bounds.Encapsulate(temp);
}
}
}
Repaint();
}
void OnGUI() {
GUILayout.Label("Dimensions", EditorStyles.boldLabel);
GUILayout.Label("Objects Selected " + count, EditorStyles.label);
GUILayout.Label("Triangles " + polygons, EditorStyles.label);
//EditorGUILayout.Vector3Field("Bounds", bounds.size);
sm = (SelectionMode)EditorGUILayout.EnumMaskField("Selection Filter", sm);
color = EditorGUILayout.ColorField(color);
drawBox = EditorGUILayout.Toggle("Draw Bounds?", drawBox);
EditorGUILayout.Vector3Field("Size", bounds.size);
/*groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
myBool = EditorGUILayout.Toggle("Toggle", myBool);
myFloat = EditorGUILayout.Slider("Slider", myFloat, -3, 3);
EditorGUILayout.EndToggleGroup();*/
}
private Color color = Color.magenta;
private Vector3 v3FrontTopLeft;
private Vector3 v3FrontTopRight;
private Vector3 v3FrontBottomLeft;
private Vector3 v3FrontBottomRight;
private Vector3 v3BackTopLeft;
private Vector3 v3BackTopRight;
private Vector3 v3BackBottomLeft;
private Vector3 v3BackBottomRight;
void OnSceneGUI(SceneView sceneView) {
if (drawBox) {
Handles.color = color;
//Debug.Log("DrawingBox");
DrawBox();
}
}
void CalcPositons() {
//Bounds bounds;
//BoxCollider bc = GetComponent<BoxCollider>();
//if (bc != null)
// bounds = bc.bounds;
//else
//return;
Vector3 v3Center = bounds.center;
Vector3 v3Extents = bounds.extents;
v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
}
void DrawBox() {
CalcPositons();
//if (Input.GetKey (KeyCode.S)) {
Handles.DrawLine(v3FrontTopLeft, v3FrontTopRight);
Handles.DrawLine(v3FrontTopRight, v3FrontBottomRight);
Handles.DrawLine(v3FrontBottomRight, v3FrontBottomLeft);
Handles.DrawLine(v3FrontBottomLeft, v3FrontTopLeft);
Handles.DrawLine(v3BackTopLeft, v3BackTopRight);
Handles.DrawLine(v3BackTopRight, v3BackBottomRight);
Handles.DrawLine(v3BackBottomRight, v3BackBottomLeft);
Handles.DrawLine(v3BackBottomLeft, v3BackTopLeft);
Handles.DrawLine(v3FrontTopLeft, v3BackTopLeft);
Handles.DrawLine(v3FrontTopRight, v3BackTopRight);
Handles.DrawLine(v3FrontBottomRight, v3BackBottomRight);
Handles.DrawLine(v3FrontBottomLeft, v3BackBottomLeft);
//}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment