Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System.Collections;
- [CustomEditor(typeof(Transform))]
- public class TransformInspector : Editor {
- public bool showTools;
- public bool copyPosition;
- public bool copyRotation;
- public bool copyScale;
- public bool pastePosition;
- public bool pasteRotation;
- public bool pasteScale;
- public bool selectionNullError;
- public float thisTransformScale;
- public bool scaleX;
- public bool scaleY;
- public bool scaleZ;
- public Transform thisTarget;
- private bool listeningForGuiChanges;
- private bool guiChanged;
- void OnEnable () {
- thisTarget = (Transform)target;
- showTools = EditorPrefs.GetBool ("ShowTools", false);
- copyPosition = EditorPrefs.GetBool ("Copy Position", true);
- copyRotation = EditorPrefs.GetBool ("Copy Rotation", true);
- copyScale = EditorPrefs.GetBool ("Copy Scale", true);
- pastePosition = EditorPrefs.GetBool ("Paste Position", true);
- pasteRotation = EditorPrefs.GetBool ("Paste Rotation", true);
- pasteScale = EditorPrefs.GetBool ("Paste Scale", true);
- }
- public override void OnInspectorGUI() {
- #region Header
- // Transform thisTarget = (Transform)target;
- CheckUndo();
- // Replicate the standard transform inspector gui
- EditorGUIUtility.LookLikeControls();
- EditorGUI.indentLevel = 0;
- Vector3 position = EditorGUILayout.Vector3Field("Position", thisTarget.localPosition);
- Vector3 eulerAngles = EditorGUILayout.Vector3Field("Rotation", thisTarget.localEulerAngles);
- Vector3 scale = EditorGUILayout.Vector3Field("Scale", thisTarget.localScale);
- EditorGUIUtility.LookLikeInspector();
- #endregion
- #region TRANSFORM TOOLS FOLD DOWN
- #region Fold Down Header
- if (GUILayout.Button ((showTools) ? "Hide Transform Tools" : "Show Transform Tools")) {
- showTools = !showTools;
- EditorPrefs.SetBool ("ShowTools", showTools);
- }
- if (showTools) {
- if (!copyPosition && !copyRotation && !copyScale) {
- selectionNullError = true;
- } else {
- selectionNullError = false;
- }
- #endregion
- #region Copy Paste Transform
- GUILayout.Space(10);
- GUILayout.Label ("Copy Transform", EditorStyles.boldLabel);
- EditorGUILayout.BeginHorizontal ();
- if (GUILayout.Button (selectionNullError ? "Nothing Selected" : "Copy Transform")) {
- if (copyPosition) {
- EditorPrefs.SetFloat("LocalPosX", thisTarget.localPosition.x);
- EditorPrefs.SetFloat("LocalPosY", thisTarget.localPosition.y);
- EditorPrefs.SetFloat("LocalPosZ", thisTarget.localPosition.z);
- }
- if (copyRotation) {
- EditorPrefs.SetFloat("LocalRotX", thisTarget.localEulerAngles.x);
- EditorPrefs.SetFloat("LocalRotY", thisTarget.localEulerAngles.y);
- EditorPrefs.SetFloat("LocalRotZ", thisTarget.localEulerAngles.z);
- }
- if (copyScale) {
- EditorPrefs.SetFloat("LocalScaleX", thisTarget.localScale.x);
- EditorPrefs.SetFloat("LocalScaleY", thisTarget.localScale.y);
- EditorPrefs.SetFloat("LocalScaleZ", thisTarget.localScale.z);
- }
- Debug.Log("LP: " + thisTarget.localPosition + " LT: (" + thisTarget.localEulerAngles.x + ", " + thisTarget.localEulerAngles.y + ", " + thisTarget.localEulerAngles.z + ") LS: " + thisTarget.localScale);
- }
- if (GUILayout.Button ("Paste Transform")) {
- guiChanged = true;
- Vector3 tV3 = new Vector3();
- if (pastePosition) {
- tV3.x = EditorPrefs.GetFloat("LocalPosX", 0.0f);
- tV3.y = EditorPrefs.GetFloat("LocalPosY", 0.0f);
- tV3.z = EditorPrefs.GetFloat("LocalPosZ", 0.0f);
- thisTarget.localPosition = tV3;
- }
- if (pasteRotation) {
- tV3.x = EditorPrefs.GetFloat("LocalRotX", 0.0f);
- tV3.y = EditorPrefs.GetFloat("LocalRotY", 0.0f);
- tV3.z = EditorPrefs.GetFloat("LocalRotZ", 0.0f);
- thisTarget.localEulerAngles = tV3;
- }
- if (pasteScale) {
- tV3.x = EditorPrefs.GetFloat("LocalScaleX", 1.0f);
- tV3.y = EditorPrefs.GetFloat("LocalScaleY", 1.0f);
- tV3.z = EditorPrefs.GetFloat("LocalScaleZ", 1.0f);
- thisTarget.localScale = tV3;
- }
- Debug.Log("LP: " + thisTarget.localPosition + " LT: " + thisTarget.localEulerAngles + " LS: " + thisTarget.localScale);
- }
- EditorGUILayout.EndHorizontal ();
- EditorGUIUtility.LookLikeControls();
- EditorGUILayout.BeginHorizontal();
- GUILayout.Label ("Position", GUILayout.Width (75));
- GUILayout.Label ("Rotation", GUILayout.Width (75));
- GUILayout.Label ("Scale", GUILayout.Width (50));
- if (GUILayout.Button ("All", GUILayout.MaxWidth (40))) TransformCopyAll ();
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.BeginHorizontal();
- GUILayout.Space(20);
- copyPosition = EditorGUILayout.Toggle (copyPosition, GUILayout.Width (75));
- copyRotation = EditorGUILayout.Toggle (copyRotation, GUILayout.Width (65));
- copyScale = EditorGUILayout.Toggle (copyScale, GUILayout.Width (45));
- if (GUILayout.Button ("None", GUILayout.MaxWidth (40))) TransformCopyNone ();
- EditorGUILayout.EndHorizontal();
- EditorGUIUtility.LookLikeInspector();
- #endregion
- #region Reset Transform
- GUILayout.Space(10);
- GUILayout.Label ("Reset Transform", EditorStyles.boldLabel);
- if (GUILayout.Button ("ALL")) {
- guiChanged = true;
- thisTarget.localPosition = Vector3.zero;
- thisTarget.localEulerAngles = Vector3.zero;
- thisTarget.localScale = Vector3.one;
- if (GUI.changed) Debug.Log ("Changed");
- }
- EditorGUILayout.BeginHorizontal ();
- if (GUILayout.Button ("Position")) {
- guiChanged = true;
- thisTarget.localPosition = Vector3.zero;
- }
- if (GUILayout.Button ("Rotation")) {
- guiChanged = true;
- thisTarget.localEulerAngles = Vector3.zero;
- }
- if (GUILayout.Button ("Scale")) {
- guiChanged = true;
- thisTarget.localScale = Vector3.one;
- }
- EditorGUILayout.EndHorizontal();
- #endregion
- #region Scale Transform
- GUILayout.Space(10);
- GUILayout.Label ("Scale Transform", EditorStyles.boldLabel);
- EditorGUIUtility.LookLikeControls();
- EditorGUILayout.BeginHorizontal();
- GUILayout.Space (50);
- thisTransformScale = EditorGUILayout.FloatField ("Scale Value", thisTransformScale, GUILayout.MaxWidth (150));
- // thisTransformScale = EditorGUILayout.Slider (thisTransformScale, -100.0f, 100.0f);
- if (GUI.changed)
- thisTarget.localScale = new Vector3 (thisTransformScale, thisTransformScale, thisTransformScale);
- GUILayout.EndHorizontal();
- EditorGUIUtility.LookLikeControls();
- EditorGUILayout.BeginHorizontal();
- GUILayout.Space(41);
- GUILayout.Label ("X", GUILayout.Width (57));
- GUILayout.Label ("Y", GUILayout.Width (55));
- GUILayout.Label ("Z", GUILayout.Width (51));
- if (GUILayout.Button ("All", GUILayout.MaxWidth (40))) TransformScaleAll ();
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.BeginHorizontal();
- GUILayout.Space(40);
- scaleX = EditorGUILayout.Toggle (scaleX, GUILayout.Width (55));
- scaleY = EditorGUILayout.Toggle (scaleY, GUILayout.Width (55));
- scaleZ = EditorGUILayout.Toggle (scaleZ, GUILayout.Width (55));
- if (GUILayout.Button ("None", GUILayout.MaxWidth (40))) TransformScaleNone ();
- EditorGUILayout.EndHorizontal();
- EditorGUIUtility.LookLikeInspector();
- #endregion
- }
- #endregion TRANSFORM TOOLS FOLD DOWN
- #region Undo
- if (GUI.changed) {
- Debug.Log ("Gui Changed");
- SetCopyPasteBools ();
- // Undo.RegisterUndo(thisTarget, "Transform Change");
- thisTarget.localPosition = FixIfNaN(position);
- thisTarget.localEulerAngles = FixIfNaN(eulerAngles);
- thisTarget.localScale = FixIfNaN(scale);
- guiChanged = true;
- }
- #endregion
- }
- private Vector3 FixIfNaN(Vector3 v) {
- if (float.IsNaN(v.x)) {
- v.x = 0;
- }
- if (float.IsNaN(v.y)) {
- v.y = 0;
- }
- if (float.IsNaN(v.z)) {
- v.z = 0;
- }
- return v;
- }
- void TransformCopyAll () {
- copyPosition = true;
- copyRotation = true;
- copyScale = true;
- GUI.changed = true;
- }
- void TransformCopyNone () {
- copyPosition = false;
- copyRotation = false;
- copyScale = false;
- GUI.changed = true;
- }
- void TransformScaleAll () {
- }
- void TransformScaleNone () {
- }
- void SetCopyPasteBools () {
- pastePosition = copyPosition;
- pasteRotation = copyRotation;
- pasteScale = copyScale;
- EditorPrefs.SetBool ("Copy Position", copyPosition);
- EditorPrefs.SetBool ("Copy Rotation", copyRotation);
- EditorPrefs.SetBool ("Copy Scale", copyScale);
- EditorPrefs.SetBool ("Paste Position", pastePosition);
- EditorPrefs.SetBool ("Paste Rotation", pasteRotation);
- EditorPrefs.SetBool ("Paste Scale", pasteScale);
- }
- private void CheckUndo() {
- // Modified from code found here:
- // http://answers.unity3d.com/questions/50954/how-to-correctly-register-undos-in-custom-inspecto.html
- Event e = Event.current;
- if (e.type == EventType.MouseDown && e.button == 0 || e.type == EventType.KeyUp && (e.keyCode == KeyCode.Tab)) {
- // When the LMB is pressed or the TAB key is released,
- // store a snapshot, but don't register it as an undo
- // (so that if nothing changes we avoid storing a useless undo)
- Debug.Log("PREPARE UNDO SNAPSHOT");
- Undo.SetSnapshotTarget(thisTarget, "Transform Change");
- Undo.CreateSnapshot();
- Undo.ClearSnapshotTarget();
- listeningForGuiChanges = true;
- guiChanged = false;
- }
- if (listeningForGuiChanges && guiChanged) {
- // Some GUI value changed after pressing the mouse.
- // Register the previous snapshot as a valid undo.
- Debug.Log("REGISTER UNDO");
- Undo.SetSnapshotTarget(thisTarget, "Transform Change");
- Undo.RegisterSnapshot();
- Undo.ClearSnapshotTarget();
- listeningForGuiChanges = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement