//$ Copyright 2015-22, Code Respawn Technologies Pvt Ltd - All Rights Reserved $// using UnityEngine; using UnityEditor; using DungeonArchitect.Utils; namespace DungeonArchitect.Editors { /// /// Utility functions for drawing UI in the Inspector window /// public class InspectorUtils { /// /// Draws the translation / rotation / scale widgets for a Matrix4x4 /// /// The caption to display above the widget /// The transform matrix to modify public static void DrawMatrixProperty(string caption, ref Matrix4x4 matrix) { Vector3 position; Quaternion rotation; Vector3 scale; Matrix.DecomposeMatrix(ref matrix, out position, out rotation, out scale); Vector3 rotationEular = rotation.eulerAngles; int precision = 4; RoundVector(ref position, precision); RoundVector(ref rotationEular, precision); RoundVector(ref scale, precision); DrawVectorProperty("Position", ref position); DrawVectorProperty("Rotation", ref rotationEular); DrawVectorProperty("Scale", ref scale); if (GUI.changed) { rotation = Quaternion.Euler(rotationEular); matrix = Matrix4x4.TRS(position, rotation, scale); } } /// /// Rounds the Vector to the nearest precision /// /// The vector to round /// The precision in digits public static void RoundVector(ref Vector3 vector, int precision) { vector.x = Round(vector.x, precision); vector.y = Round(vector.y, precision); vector.z = Round(vector.z, precision); } /// /// rounds a float to the nearest precision /// /// The value to round /// The precision in digits /// public static float Round(float f, int precision) { var multiplier = Mathf.Pow(10, precision); return Mathf.Round(f * multiplier) / multiplier; } /// /// Draws XYZ components of a Vector3 in the inspector window within the same line for better usability and asthetics /// /// The caption to use on the property /// The vector to modify public static void DrawVectorProperty(string caption, ref Vector3 vector) { EditorGUILayout.BeginHorizontal(); GUILayout.Label(caption, EditorStyles.label, GUILayout.Width(60)); GUILayout.Label("X:", EditorStyles.label); vector.x = EditorGUILayout.FloatField(vector.x); GUILayout.Label("Y:", EditorStyles.label); vector.y = EditorGUILayout.FloatField(vector.y); GUILayout.Label("Z:", EditorStyles.label); vector.z = EditorGUILayout.FloatField(vector.z); EditorGUILayout.EndHorizontal(); } } }