TransformInspectorWithSize

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Wording)
m (Fix class name to match filename)
Line 21: Line 21:
  
 
[CanEditMultipleObjects, CustomEditor(typeof(Transform))]
 
[CanEditMultipleObjects, CustomEditor(typeof(Transform))]
public class TransformInspector : Editor {
+
public class TransformInspectorWithSize : Editor {
  
 
     private const float FIELD_WIDTH = 212.0f;
 
     private const float FIELD_WIDTH = 212.0f;

Revision as of 23:06, 26 September 2015

Description

The following script adds a Size field to the Transform inspector to show the scaled mesh size of an object.

Provides a convenient way to see the actual size of a mesh in the scene or update the scale of an object to define a precise object size after scaling.

Accounts for child objects with meshes as well, showing the total size in the parent.

The Size field can be edited and will update the Scale property in relation the objects mesh size.

Derived from TransformInspector.

Usage

Place TransformInspectorWithSize.cs into an Editor folder inside of your project's Assets folder.

TransformInspectorWithSize.cs

// Reverse engineered UnityEditor.TransformInspector
 
using UnityEngine;
using UnityEditor;
 
[CanEditMultipleObjects, CustomEditor(typeof(Transform))]
public class TransformInspectorWithSize : Editor {
 
    private const float FIELD_WIDTH = 212.0f;
    private const bool WIDE_MODE = true;
 
    private const float POSITION_MAX = 100000.0f;
 
    private static GUIContent positionGUIContent = new GUIContent(LocalString("Position")
                                                                 ,LocalString("The local position of this Game Object relative to the parent."));
    private static GUIContent rotationGUIContent = new GUIContent(LocalString("Rotation")
                                                                 ,LocalString("The local rotation of this Game Object relative to the parent."));
    private static GUIContent scaleGUIContent    = new GUIContent(LocalString("Scale")
                                                                 ,LocalString("The local scaling of this Game Object relative to the parent."));
    private static GUIContent sizeGUIContent     = new GUIContent(LocalString("Size")
                                                                 ,LocalString("The local scaled mesh size of this Game Object relative to the parent."));
 
    private static string positionWarningText = LocalString("Due to floating-point precision limitations, it is recommended to bring the world coordinates of the GameObject within a smaller range.");
 
    private SerializedProperty positionProperty;
    private SerializedProperty rotationProperty;
    private SerializedProperty scaleProperty;
 
    private static string LocalString(string text) {
        return LocalizationDatabase.GetLocalizedString(text);
    }
 
    public void OnEnable() {
        this.positionProperty = this.serializedObject.FindProperty("m_LocalPosition");
        this.rotationProperty = this.serializedObject.FindProperty("m_LocalRotation");
        this.scaleProperty    = this.serializedObject.FindProperty("m_LocalScale");
    }
 
    public override void OnInspectorGUI() {
        EditorGUIUtility.wideMode = TransformInspector.WIDE_MODE;
        EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth - TransformInspector.FIELD_WIDTH; // align field to right of inspector
 
        this.serializedObject.Update();
 
        EditorGUILayout.PropertyField(this.positionProperty, positionGUIContent);
        this.RotationPropertyField(   this.rotationProperty, rotationGUIContent);
        EditorGUILayout.PropertyField(this.scaleProperty,    scaleGUIContent);
        this.ScaledSizePropertyField( this.scaleProperty, sizeGUIContent);
 
        if (!ValidatePosition(((Transform) this.target).position)) {
            EditorGUILayout.HelpBox(positionWarningText, MessageType.Warning);
        }
 
        this.serializedObject.ApplyModifiedProperties();
    }
 
    private bool ValidatePosition(Vector3 position) {
        if (Mathf.Abs(position.x) > TransformInspector.POSITION_MAX) return false;
        if (Mathf.Abs(position.y) > TransformInspector.POSITION_MAX) return false;
        if (Mathf.Abs(position.z) > TransformInspector.POSITION_MAX) return false;
        return true;
    }
 
    private void RotationPropertyField(SerializedProperty rotationProperty, GUIContent content) {
        Transform transform = (Transform) this.targets[0];
        Quaternion localRotation = transform.localRotation;
        foreach (UnityEngine.Object t in (UnityEngine.Object[]) this.targets) {
            if (!SameRotation(localRotation, ((Transform) t).localRotation)) {
                EditorGUI.showMixedValue = true;
                break;
            }
        }
 
        EditorGUI.BeginChangeCheck();
 
        Vector3 eulerAngles = EditorGUILayout.Vector3Field(content, localRotation.eulerAngles);
 
        if (EditorGUI.EndChangeCheck()) {
            Undo.RecordObjects(this.targets, "Rotation Changed");
            foreach (UnityEngine.Object obj in this.targets) {
                Transform t = (Transform) obj;
                t.localEulerAngles = eulerAngles;
            }
            rotationProperty.serializedObject.SetIsDifferentCacheDirty();
        }
 
        EditorGUI.showMixedValue = false;
    }
 
    private bool SameRotation(Quaternion rot1, Quaternion rot2) {
        if (rot1.x != rot2.x) return false;
        if (rot1.y != rot2.y) return false;
        if (rot1.z != rot2.z) return false;
        if (rot1.w != rot2.w) return false;
        return true;
    }
 
    private void ScaledSizePropertyField(SerializedProperty scaleProperty, GUIContent content) {
        Vector3 meshSize = GetMaxMeshSize((Transform) this.targets[0]);
        foreach (UnityEngine.Object obj in this.targets) {
            if (meshSize != GetMaxMeshSize((Transform) obj)) {
                EditorGUI.showMixedValue = true;
                break;
            }
        }
 
        EditorGUI.BeginChangeCheck();
 
        EditorGUILayout.Space();
        Vector3 size = EditorGUILayout.Vector3Field(content, Vector3.Scale(meshSize, scaleProperty.vector3Value));
 
        if (EditorGUI.EndChangeCheck()) {
            Undo.RecordObjects(this.targets, "Scale Changed");
            foreach (UnityEngine.Object obj in this.targets) {
                Transform t = (Transform) obj;
                meshSize = GetMaxMeshSize(t);
                t.localScale = new Vector3(size.x / meshSize.x, size.y / meshSize.y, size.z / meshSize.z);
            }
            scaleProperty.serializedObject.SetIsDifferentCacheDirty();
        }
    }
 
    private Vector3 GetMaxMeshSize(Transform t) {
        Vector3 size = Vector3.zero;
        MeshFilter[] filters = t.GetComponentsInChildren<MeshFilter>();
        foreach (MeshFilter filter in filters) {
            size = Vector3.Max(size, filter.sharedMesh.bounds.size);
        }
        return size;
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox