Trash maze MVP (#79)
Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com> Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Reviewed-on: #79
This commit is contained in:
148
Assets/Scripts/Utils/Measurements.cs
Normal file
148
Assets/Scripts/Utils/Measurements.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
public class Measurements : MonoBehaviour
|
||||
{
|
||||
public enum MeasurementUnit
|
||||
{
|
||||
UnityUnits,
|
||||
Centimeters,
|
||||
Meters,
|
||||
Kilometers,
|
||||
Inches,
|
||||
Feet,
|
||||
Yards,
|
||||
Miles
|
||||
}
|
||||
|
||||
public enum MeasurementSource
|
||||
{
|
||||
Collider,
|
||||
Renderer,
|
||||
Mesh
|
||||
}
|
||||
|
||||
public MeasurementUnit measurementUnit = MeasurementUnit.Feet;
|
||||
public MeasurementSource measurementSource = MeasurementSource.Collider;
|
||||
public GameObject distanceObject;
|
||||
|
||||
internal Vector3 Dimensions;
|
||||
internal float CenterToCenter;
|
||||
internal float EdgeToEdge;
|
||||
|
||||
void Update()
|
||||
{
|
||||
CalculateDimensions();
|
||||
if (distanceObject != null)
|
||||
{
|
||||
CalculateDistances();
|
||||
}
|
||||
}
|
||||
|
||||
void CalculateDimensions()
|
||||
{
|
||||
Bounds bounds = new Bounds();
|
||||
|
||||
switch (measurementSource)
|
||||
{
|
||||
case MeasurementSource.Collider:
|
||||
Collider objectCollider = GetComponent<Collider>();
|
||||
if (objectCollider != null) bounds = objectCollider.bounds;
|
||||
break;
|
||||
case MeasurementSource.Renderer:
|
||||
Renderer objectRenderer = GetComponent<Renderer>();
|
||||
if (objectRenderer != null) bounds = objectRenderer.bounds;
|
||||
break;
|
||||
case MeasurementSource.Mesh:
|
||||
MeshFilter meshFilter = GetComponent<MeshFilter>();
|
||||
if (meshFilter != null && meshFilter.mesh != null) bounds = meshFilter.mesh.bounds;
|
||||
break;
|
||||
}
|
||||
|
||||
Dimensions = ConvertToSelectedUnit(bounds.size);
|
||||
}
|
||||
|
||||
void CalculateDistances()
|
||||
{
|
||||
Vector3 thisPosition = transform.position;
|
||||
Vector3 otherPosition = distanceObject.transform.position;
|
||||
|
||||
CenterToCenter = ConvertToSelectedUnit(Vector3.Distance(thisPosition, otherPosition));
|
||||
|
||||
Bounds thisBounds = GetComponent<Collider>().bounds;
|
||||
Bounds otherBounds = distanceObject.GetComponent<Collider>().bounds;
|
||||
|
||||
Vector3 closestPoint1 = thisBounds.ClosestPoint(otherPosition);
|
||||
Vector3 closestPoint2 = otherBounds.ClosestPoint(thisPosition);
|
||||
|
||||
EdgeToEdge = ConvertToSelectedUnit(Vector3.Distance(closestPoint1, closestPoint2));
|
||||
}
|
||||
|
||||
float ConvertToSelectedUnit(float unityUnits)
|
||||
{
|
||||
switch (measurementUnit)
|
||||
{
|
||||
case MeasurementUnit.Centimeters:
|
||||
return unityUnits * 100f;
|
||||
case MeasurementUnit.Meters:
|
||||
return unityUnits;
|
||||
case MeasurementUnit.Kilometers:
|
||||
return unityUnits / 1000f;
|
||||
case MeasurementUnit.Inches:
|
||||
return unityUnits * 39.3701f;
|
||||
case MeasurementUnit.Feet:
|
||||
return unityUnits * 3.28084f;
|
||||
case MeasurementUnit.Yards:
|
||||
return unityUnits * 1.09361f;
|
||||
case MeasurementUnit.Miles:
|
||||
return unityUnits * 0.000621371f;
|
||||
default:
|
||||
return unityUnits;
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 ConvertToSelectedUnit(Vector3 unityUnits)
|
||||
{
|
||||
return new Vector3(
|
||||
ConvertToSelectedUnit(unityUnits.x),
|
||||
ConvertToSelectedUnit(unityUnits.y),
|
||||
ConvertToSelectedUnit(unityUnits.z)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[CustomEditor(typeof(Measurements))]
|
||||
public class MeasurementsEditor : Editor
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
DrawDefaultInspector();
|
||||
|
||||
Measurements measurements = (Measurements)target;
|
||||
|
||||
EditorGUILayout.Space();
|
||||
EditorGUILayout.LabelField("Dimensions", EditorStyles.boldLabel);
|
||||
EditorGUILayout.LabelField($"Width\t\t\t<b>{measurements.Dimensions.x:F6} {measurements.measurementUnit}</b>", new GUIStyle(EditorStyles.label) { richText = true });
|
||||
EditorGUILayout.LabelField($"Height\t\t\t<b>{measurements.Dimensions.y:F6} {measurements.measurementUnit}</b>", new GUIStyle(EditorStyles.label) { richText = true });
|
||||
EditorGUILayout.LabelField($"Depth\t\t\t<b>{measurements.Dimensions.z:F6} {measurements.measurementUnit}</b>", new GUIStyle(EditorStyles.label) { richText = true });
|
||||
|
||||
if (measurements.distanceObject != null)
|
||||
{
|
||||
EditorGUILayout.Space();
|
||||
EditorGUILayout.LabelField($"Distance to {measurements.distanceObject.name}", EditorStyles.boldLabel);
|
||||
EditorGUILayout.LabelField($"Center to Center\t\t<b>{measurements.CenterToCenter:F6} {measurements.measurementUnit}</b>", new GUIStyle(EditorStyles.label) { richText = true });
|
||||
EditorGUILayout.LabelField($"Edge to Edge\t\t<b>{measurements.EdgeToEdge:F6} {measurements.measurementUnit}</b>", new GUIStyle(EditorStyles.label) { richText = true });
|
||||
}
|
||||
|
||||
// This will update the inspector view every frame
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
Repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user