SHARE
TWEET

Untitled

a guest Oct 21st, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. namespace Alt3d
  5. {
  6.     [CustomEditor(typeof(Transform), true)]
  7.     public class TransformEditor : Editor
  8.     {
  9.         public override void OnInspectorGUI()
  10.         {
  11.             var transform = target as Transform;
  12.             if (!transform)
  13.             {
  14.                 return;    
  15.             }
  16.            
  17.             EditorGUIUtility.labelWidth = 15f;
  18.  
  19.             Vector3 position;
  20.             Vector3 rotation;
  21.             Vector3 scale;
  22.             bool isResetValid;
  23.  
  24.             EditorGUILayout.BeginHorizontal();
  25.             {
  26.                 isResetValid = IsResetVectorValid(transform.localPosition, Vector3.zero);
  27.                 if (DrawButton("P", "Reset Position", isResetValid, 20f))
  28.                 {
  29.                     Undo.RecordObject(transform, "Reset Position");
  30.                     transform.localPosition = Vector3.zero;
  31.                 }
  32.                 position = DrawVector3(transform.localPosition);
  33.             }
  34.             EditorGUILayout.EndHorizontal();
  35.  
  36.             EditorGUILayout.BeginHorizontal();
  37.             {
  38.                 isResetValid = IsResetVectorValid(transform.localEulerAngles, Vector3.zero);
  39.                 if (DrawButton("R", "Reset Rotation", isResetValid, 20f))
  40.                 {
  41.                     Undo.RecordObject(transform, "Reset Rotation");
  42.                     transform.localEulerAngles = Vector3.zero;
  43.                 }
  44.                 rotation = DrawVector3(transform.localEulerAngles);
  45.             }
  46.             EditorGUILayout.EndHorizontal();
  47.  
  48.             EditorGUILayout.BeginHorizontal();
  49.             {
  50.                 isResetValid = IsResetVectorValid(transform.localScale, Vector3.one);
  51.                 if (DrawButton("S", "Reset Scale", isResetValid, 20f))
  52.                 {
  53.                     Undo.RecordObject(transform, "Reset Scale");
  54.                     transform.localScale = Vector3.one;
  55.                 }
  56.                 scale = DrawVector3(transform.localScale);
  57.             }
  58.             EditorGUILayout.EndHorizontal();
  59.  
  60.             if (GUI.changed)
  61.             {
  62.                 Undo.RecordObject(transform, "Transform Change");
  63.                 transform.localPosition = ValidateVector(position);
  64.                 transform.localEulerAngles = ValidateVector(rotation);
  65.                 transform.localScale = ValidateVector(scale);
  66.             }
  67.         }
  68.  
  69.         private static bool DrawButton(string title, string tooltip, bool enabled, float width)
  70.         {
  71.             if (enabled)
  72.             {
  73.                 return GUILayout.Button(new GUIContent(title, tooltip), GUILayout.Width(width));
  74.             }
  75.            
  76.             var color = GUI.color;
  77.             GUI.color = new Color(1f, 1f, 1f, 0.25f);
  78.             GUILayout.Button(new GUIContent(title, tooltip), GUILayout.Width(width));
  79.             GUI.color = color;
  80.             return false;          
  81.         }
  82.        
  83.         private static Vector3 DrawVector3(Vector3 value)
  84.         {
  85.             var option = GUILayout.MinWidth(30f);
  86.             value.x = EditorGUILayout.FloatField("X", value.x, option);
  87.             value.y = EditorGUILayout.FloatField("Y", value.y, option);
  88.             value.z = EditorGUILayout.FloatField("Z", value.z, option);
  89.             return value;
  90.         }
  91.  
  92.         private static bool IsResetVectorValid(Vector3 vector, Vector3 target)
  93.         {          
  94.             return (vector.x != target.x || vector.y != target.y || vector.z != target.z);
  95.         }
  96.  
  97.         private static Vector3 ValidateVector(Vector3 vector)
  98.         {
  99.             vector.x = float.IsNaN(vector.x) ? 0f : vector.x;
  100.             vector.y = float.IsNaN(vector.y) ? 0f : vector.y;
  101.             vector.z = float.IsNaN(vector.z) ? 0f : vector.z;
  102.             return vector;
  103.         }
  104.     }
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top