Advertisement
Guest User

TransformEditor

a guest
Apr 14th, 2013
1,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. /*
  6.  * Unity Transform Editor Script
  7.  * By Chevy Ray Johnston (happytrash@gmail.com)
  8.  *
  9.  * Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder.
  10.  * Custom editors run automatically, so once you've placed the script in your project, your transforms should look different.
  11.  */
  12. [CustomEditor(typeof(Transform))]
  13. public class TransformEditor : Editor
  14. {
  15.     static bool mode2D;
  16.  
  17.     public override void OnInspectorGUI()
  18.     {
  19.         //Grab target & modifiable values
  20.         var t = (Transform)target;
  21.         var p = t.localPosition;
  22.         var r = t.localEulerAngles;
  23.         var s = t.localScale;
  24.  
  25.         //Toggle 2D mode
  26.         mode2D = GUILayout.Toggle(mode2D, "2D Mode");
  27.        
  28.         if (mode2D)
  29.         {
  30.             //Modify 2D transform
  31.             var p2 = EditorGUILayout.Vector2Field("Position", new Vector2(p.x, p.y));
  32.             var s2 = EditorGUILayout.Vector2Field("Scale", new Vector2(s.x, s.y));
  33.             var r2 = EditorGUILayout.Slider("Rotation", r.z, 0, 359);
  34.             p.Set(p2.x, p2.y, p.z);
  35.             s.Set(s2.x, s2.y, s.z);
  36.             r.Set(r.x, r.y, r2);
  37.         }
  38.         else
  39.         {
  40.             //Modify 3D transform
  41.             p = EditorGUILayout.Vector3Field("Position", t.localPosition);
  42.             r = EditorGUILayout.Vector3Field("Rotation", t.localEulerAngles);
  43.             s = EditorGUILayout.Vector3Field("Scale", t.localScale);
  44.         }
  45.  
  46.         //Reset buttons
  47.         GUILayout.Label("Reset");
  48.         GUILayout.BeginHorizontal();
  49.         if (GUILayout.Button("Position"))
  50.             p.Set(0, 0, 0);
  51.         if (GUILayout.Button("Rotation"))
  52.             r.Set(0, 0, 0);
  53.         if (GUILayout.Button("Scale"))
  54.             s.Set(1, 1, 1);
  55.         if (GUILayout.Button("All"))
  56.         {
  57.             p.Set(0, 0, 0);
  58.             r.Set(0, 0, 0);
  59.             s.Set(1, 1, 1);
  60.         }
  61.         GUILayout.EndHorizontal();
  62.  
  63.         //Apply changes
  64.         if (GUI.changed)
  65.         {
  66.             Undo.RegisterUndo(t, "Transform Change");
  67.             t.localPosition = Validate(p);
  68.             t.localEulerAngles = Validate(r);
  69.             t.localScale = Validate(s);
  70.         }
  71.     }
  72.  
  73.     private Vector3 Validate(Vector3 v)
  74.     {
  75.         if (float.IsNaN(v.x))
  76.             v.x = 0;
  77.         if (float.IsNaN(v.y))
  78.             v.y = 0;
  79.         if (float.IsNaN(v.z))
  80.             v.z = 0;
  81.         return v;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement