Advertisement
napland

CustomtransformInspector part 1

Jul 29th, 2016
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. [CustomEditor(typeof(Transform))]
  5. [CanEditMultipleObjects]
  6. public class CustomTransformComponent : Editor
  7. {
  8.     private Transform _transform;
  9.    
  10.     public override void OnInspectorGUI()
  11.     {
  12.         //We need this for all OnInspectorGUI sub methods
  13.         _transform = (Transform)target;
  14.  
  15.         StandardTransformInspector();
  16.     }
  17.  
  18.     private void StandardTransformInspector()
  19.     {
  20.         bool didPositionChange = false;
  21.         bool didRotationChange = false;
  22.         bool didScaleChange = false;
  23.  
  24.         // Watch for changes.
  25.         //  1)  Float values are imprecise, so floating point error may cause changes
  26.         //      when you've not actually made a change.
  27.         //  2)  This allows us to also record an undo point properly since we're only
  28.         //      recording when something has changed.
  29.  
  30.         // Store current values for checking later
  31.         Vector3 initialLocalPosition = _transform.localPosition;
  32.         Vector3 initialLocalEuler = _transform.localEulerAngles;
  33.         Vector3 initialLocalScale = _transform.localScale;
  34.  
  35.         EditorGUI.BeginChangeCheck();
  36.         Vector3 localPosition = EditorGUILayout.Vector3Field("Position", _transform.localPosition);
  37.         if (EditorGUI.EndChangeCheck())
  38.             didPositionChange = true;
  39.  
  40.         EditorGUI.BeginChangeCheck();
  41.         Vector3 localEulerAngles = EditorGUILayout.Vector3Field("Euler Rotation", _transform.localEulerAngles);
  42.         if (EditorGUI.EndChangeCheck())
  43.             didRotationChange = true;
  44.  
  45.         EditorGUI.BeginChangeCheck();
  46.         Vector3 localScale = EditorGUILayout.Vector3Field("Scale", _transform.localScale);
  47.         if (EditorGUI.EndChangeCheck())
  48.             didScaleChange = true;
  49.  
  50.         // Apply changes with record undo
  51.         if (didPositionChange || didRotationChange || didScaleChange)
  52.         {
  53.             Undo.RecordObject(_transform, _transform.name);
  54.  
  55.             if (didPositionChange)
  56.                 _transform.localPosition = localPosition;
  57.  
  58.             if (didRotationChange)
  59.                 _transform.localEulerAngles = localEulerAngles;
  60.  
  61.             if (didScaleChange)
  62.                 _transform.localScale = localScale;
  63.  
  64.         }
  65.  
  66.         // Since BeginChangeCheck only works on the selected object
  67.         // we need to manually apply transform changes to all selected objects.
  68.         Transform[] selectedTransforms = Selection.transforms;
  69.         if (selectedTransforms.Length > 1)
  70.         {
  71.             foreach (var item in selectedTransforms)
  72.             {
  73.                 if (didPositionChange || didRotationChange || didScaleChange)
  74.                     Undo.RecordObject(item, item.name);
  75.  
  76.                 if (didPositionChange)
  77.                 {
  78.                     item.localPosition = ApplyChangesOnly(
  79.                         item.localPosition, initialLocalPosition, _transform.localPosition);
  80.                 }
  81.  
  82.                 if (didRotationChange)
  83.                 {
  84.                     item.localEulerAngles = ApplyChangesOnly(
  85.                         item.localEulerAngles, initialLocalEuler, _transform.localEulerAngles);
  86.                 }
  87.  
  88.                 if (didScaleChange)
  89.                 {
  90.                     item.localScale = ApplyChangesOnly(
  91.                         item.localScale, initialLocalScale, _transform.localScale);
  92.                 }
  93.                
  94.             }
  95.         }
  96.     }
  97.  
  98.     private Vector3 ApplyChangesOnly(Vector3 toApply, Vector3 initial, Vector3 changed)
  99.     {
  100.         if (!Mathf.Approximately(initial.x, changed.x))
  101.             toApply.x = _transform.localPosition.x;
  102.  
  103.         if (!Mathf.Approximately(initial.y, changed.y))
  104.             toApply.y = _transform.localPosition.y;
  105.  
  106.         if (!Mathf.Approximately(initial.z, changed.z))
  107.             toApply.z = _transform.localPosition.z;
  108.  
  109.         return toApply;
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement