Advertisement
LittleAngel

TransformInspector

Nov 7th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.75 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. [CustomEditor(typeof(Transform))]
  6. public class TransformInspector : Editor {
  7.  
  8. public bool showTools;
  9. public bool copyPosition;
  10. public bool copyRotation;
  11. public bool copyScale;
  12. public bool pastePosition;
  13. public bool pasteRotation;
  14. public bool pasteScale;
  15. public bool selectionNullError;
  16. public float thisTransformScale;
  17. public bool scaleX;
  18. public bool scaleY;
  19. public bool scaleZ;
  20.  
  21. public Transform thisTarget;
  22. private bool listeningForGuiChanges;
  23. private bool guiChanged;
  24.  
  25. void OnEnable () {
  26. thisTarget = (Transform)target;
  27. showTools = EditorPrefs.GetBool ("ShowTools", false);
  28. copyPosition = EditorPrefs.GetBool ("Copy Position", true);
  29. copyRotation = EditorPrefs.GetBool ("Copy Rotation", true);
  30. copyScale = EditorPrefs.GetBool ("Copy Scale", true);
  31. pastePosition = EditorPrefs.GetBool ("Paste Position", true);
  32. pasteRotation = EditorPrefs.GetBool ("Paste Rotation", true);
  33. pasteScale = EditorPrefs.GetBool ("Paste Scale", true);
  34. }
  35.  
  36. public override void OnInspectorGUI() {
  37. #region Header
  38. // Transform thisTarget = (Transform)target;
  39. CheckUndo();
  40.  
  41. // Replicate the standard transform inspector gui
  42. EditorGUIUtility.LookLikeControls();
  43. EditorGUI.indentLevel = 0;
  44. Vector3 position = EditorGUILayout.Vector3Field("Position", thisTarget.localPosition);
  45. Vector3 eulerAngles = EditorGUILayout.Vector3Field("Rotation", thisTarget.localEulerAngles);
  46. Vector3 scale = EditorGUILayout.Vector3Field("Scale", thisTarget.localScale);
  47. EditorGUIUtility.LookLikeInspector();
  48.  
  49. #endregion
  50. #region TRANSFORM TOOLS FOLD DOWN
  51. #region Fold Down Header
  52. if (GUILayout.Button ((showTools) ? "Hide Transform Tools" : "Show Transform Tools")) {
  53. showTools = !showTools;
  54. EditorPrefs.SetBool ("ShowTools", showTools);
  55. }
  56.  
  57. if (showTools) {
  58. if (!copyPosition && !copyRotation && !copyScale) {
  59. selectionNullError = true;
  60. } else {
  61. selectionNullError = false;
  62. }
  63. #endregion
  64.  
  65. #region Copy Paste Transform
  66. GUILayout.Space(10);
  67. GUILayout.Label ("Copy Transform", EditorStyles.boldLabel);
  68. EditorGUILayout.BeginHorizontal ();
  69. if (GUILayout.Button (selectionNullError ? "Nothing Selected" : "Copy Transform")) {
  70. if (copyPosition) {
  71. EditorPrefs.SetFloat("LocalPosX", thisTarget.localPosition.x);
  72. EditorPrefs.SetFloat("LocalPosY", thisTarget.localPosition.y);
  73. EditorPrefs.SetFloat("LocalPosZ", thisTarget.localPosition.z);
  74. }
  75. if (copyRotation) {
  76. EditorPrefs.SetFloat("LocalRotX", thisTarget.localEulerAngles.x);
  77. EditorPrefs.SetFloat("LocalRotY", thisTarget.localEulerAngles.y);
  78. EditorPrefs.SetFloat("LocalRotZ", thisTarget.localEulerAngles.z);
  79. }
  80. if (copyScale) {
  81. EditorPrefs.SetFloat("LocalScaleX", thisTarget.localScale.x);
  82. EditorPrefs.SetFloat("LocalScaleY", thisTarget.localScale.y);
  83. EditorPrefs.SetFloat("LocalScaleZ", thisTarget.localScale.z);
  84. }
  85.  
  86. Debug.Log("LP: " + thisTarget.localPosition + " LT: (" + thisTarget.localEulerAngles.x + ", " + thisTarget.localEulerAngles.y + ", " + thisTarget.localEulerAngles.z + ") LS: " + thisTarget.localScale);
  87. }
  88. if (GUILayout.Button ("Paste Transform")) {
  89. guiChanged = true;
  90. Vector3 tV3 = new Vector3();
  91. if (pastePosition) {
  92. tV3.x = EditorPrefs.GetFloat("LocalPosX", 0.0f);
  93. tV3.y = EditorPrefs.GetFloat("LocalPosY", 0.0f);
  94. tV3.z = EditorPrefs.GetFloat("LocalPosZ", 0.0f);
  95. thisTarget.localPosition = tV3;
  96. }
  97. if (pasteRotation) {
  98. tV3.x = EditorPrefs.GetFloat("LocalRotX", 0.0f);
  99. tV3.y = EditorPrefs.GetFloat("LocalRotY", 0.0f);
  100. tV3.z = EditorPrefs.GetFloat("LocalRotZ", 0.0f);
  101. thisTarget.localEulerAngles = tV3;
  102. }
  103. if (pasteScale) {
  104. tV3.x = EditorPrefs.GetFloat("LocalScaleX", 1.0f);
  105. tV3.y = EditorPrefs.GetFloat("LocalScaleY", 1.0f);
  106. tV3.z = EditorPrefs.GetFloat("LocalScaleZ", 1.0f);
  107. thisTarget.localScale = tV3;
  108. }
  109.  
  110. Debug.Log("LP: " + thisTarget.localPosition + " LT: " + thisTarget.localEulerAngles + " LS: " + thisTarget.localScale);
  111. }
  112. EditorGUILayout.EndHorizontal ();
  113.  
  114. EditorGUIUtility.LookLikeControls();
  115. EditorGUILayout.BeginHorizontal();
  116. GUILayout.Label ("Position", GUILayout.Width (75));
  117. GUILayout.Label ("Rotation", GUILayout.Width (75));
  118. GUILayout.Label ("Scale", GUILayout.Width (50));
  119. if (GUILayout.Button ("All", GUILayout.MaxWidth (40))) TransformCopyAll ();
  120. EditorGUILayout.EndHorizontal();
  121. EditorGUILayout.BeginHorizontal();
  122. GUILayout.Space(20);
  123. copyPosition = EditorGUILayout.Toggle (copyPosition, GUILayout.Width (75));
  124. copyRotation = EditorGUILayout.Toggle (copyRotation, GUILayout.Width (65));
  125. copyScale = EditorGUILayout.Toggle (copyScale, GUILayout.Width (45));
  126. if (GUILayout.Button ("None", GUILayout.MaxWidth (40))) TransformCopyNone ();
  127. EditorGUILayout.EndHorizontal();
  128. EditorGUIUtility.LookLikeInspector();
  129. #endregion
  130.  
  131. #region Reset Transform
  132. GUILayout.Space(10);
  133. GUILayout.Label ("Reset Transform", EditorStyles.boldLabel);
  134. if (GUILayout.Button ("ALL")) {
  135. guiChanged = true;
  136. thisTarget.localPosition = Vector3.zero;
  137. thisTarget.localEulerAngles = Vector3.zero;
  138. thisTarget.localScale = Vector3.one;
  139. if (GUI.changed) Debug.Log ("Changed");
  140. }
  141. EditorGUILayout.BeginHorizontal ();
  142. if (GUILayout.Button ("Position")) {
  143. guiChanged = true;
  144. thisTarget.localPosition = Vector3.zero;
  145. }
  146. if (GUILayout.Button ("Rotation")) {
  147. guiChanged = true;
  148. thisTarget.localEulerAngles = Vector3.zero;
  149. }
  150. if (GUILayout.Button ("Scale")) {
  151. guiChanged = true;
  152. thisTarget.localScale = Vector3.one;
  153. }
  154. EditorGUILayout.EndHorizontal();
  155. #endregion
  156.  
  157. #region Scale Transform
  158. GUILayout.Space(10);
  159. GUILayout.Label ("Scale Transform", EditorStyles.boldLabel);
  160. EditorGUIUtility.LookLikeControls();
  161. EditorGUILayout.BeginHorizontal();
  162. GUILayout.Space (50);
  163. thisTransformScale = EditorGUILayout.FloatField ("Scale Value", thisTransformScale, GUILayout.MaxWidth (150));
  164. // thisTransformScale = EditorGUILayout.Slider (thisTransformScale, -100.0f, 100.0f);
  165. if (GUI.changed)
  166. thisTarget.localScale = new Vector3 (thisTransformScale, thisTransformScale, thisTransformScale);
  167. GUILayout.EndHorizontal();
  168.  
  169. EditorGUIUtility.LookLikeControls();
  170. EditorGUILayout.BeginHorizontal();
  171. GUILayout.Space(41);
  172. GUILayout.Label ("X", GUILayout.Width (57));
  173. GUILayout.Label ("Y", GUILayout.Width (55));
  174. GUILayout.Label ("Z", GUILayout.Width (51));
  175. if (GUILayout.Button ("All", GUILayout.MaxWidth (40))) TransformScaleAll ();
  176. EditorGUILayout.EndHorizontal();
  177. EditorGUILayout.BeginHorizontal();
  178. GUILayout.Space(40);
  179. scaleX = EditorGUILayout.Toggle (scaleX, GUILayout.Width (55));
  180. scaleY = EditorGUILayout.Toggle (scaleY, GUILayout.Width (55));
  181. scaleZ = EditorGUILayout.Toggle (scaleZ, GUILayout.Width (55));
  182. if (GUILayout.Button ("None", GUILayout.MaxWidth (40))) TransformScaleNone ();
  183. EditorGUILayout.EndHorizontal();
  184. EditorGUIUtility.LookLikeInspector();
  185. #endregion
  186. }
  187. #endregion TRANSFORM TOOLS FOLD DOWN
  188.  
  189. #region Undo
  190. if (GUI.changed) {
  191. Debug.Log ("Gui Changed");
  192. SetCopyPasteBools ();
  193. // Undo.RegisterUndo(thisTarget, "Transform Change");
  194.  
  195. thisTarget.localPosition = FixIfNaN(position);
  196. thisTarget.localEulerAngles = FixIfNaN(eulerAngles);
  197. thisTarget.localScale = FixIfNaN(scale);
  198. guiChanged = true;
  199. }
  200. #endregion
  201. }
  202.  
  203. private Vector3 FixIfNaN(Vector3 v) {
  204. if (float.IsNaN(v.x)) {
  205. v.x = 0;
  206. }
  207. if (float.IsNaN(v.y)) {
  208. v.y = 0;
  209. }
  210. if (float.IsNaN(v.z)) {
  211. v.z = 0;
  212. }
  213. return v;
  214. }
  215.  
  216. void TransformCopyAll () {
  217. copyPosition = true;
  218. copyRotation = true;
  219. copyScale = true;
  220. GUI.changed = true;
  221. }
  222.  
  223. void TransformCopyNone () {
  224. copyPosition = false;
  225. copyRotation = false;
  226. copyScale = false;
  227. GUI.changed = true;
  228. }
  229.  
  230. void TransformScaleAll () {
  231. }
  232.  
  233. void TransformScaleNone () {
  234. }
  235.  
  236. void SetCopyPasteBools () {
  237. pastePosition = copyPosition;
  238. pasteRotation = copyRotation;
  239. pasteScale = copyScale;
  240.  
  241. EditorPrefs.SetBool ("Copy Position", copyPosition);
  242. EditorPrefs.SetBool ("Copy Rotation", copyRotation);
  243. EditorPrefs.SetBool ("Copy Scale", copyScale);
  244. EditorPrefs.SetBool ("Paste Position", pastePosition);
  245. EditorPrefs.SetBool ("Paste Rotation", pasteRotation);
  246. EditorPrefs.SetBool ("Paste Scale", pasteScale);
  247. }
  248.  
  249. private void CheckUndo() {
  250. // Modified from code found here:
  251. // http://answers.unity3d.com/questions/50954/how-to-correctly-register-undos-in-custom-inspecto.html
  252. Event e = Event.current;
  253.  
  254. if (e.type == EventType.MouseDown && e.button == 0 || e.type == EventType.KeyUp && (e.keyCode == KeyCode.Tab)) {
  255. // When the LMB is pressed or the TAB key is released,
  256. // store a snapshot, but don't register it as an undo
  257. // (so that if nothing changes we avoid storing a useless undo)
  258. Debug.Log("PREPARE UNDO SNAPSHOT");
  259. Undo.SetSnapshotTarget(thisTarget, "Transform Change");
  260. Undo.CreateSnapshot();
  261. Undo.ClearSnapshotTarget();
  262. listeningForGuiChanges = true;
  263. guiChanged = false;
  264. }
  265.  
  266. if (listeningForGuiChanges && guiChanged) {
  267. // Some GUI value changed after pressing the mouse.
  268. // Register the previous snapshot as a valid undo.
  269. Debug.Log("REGISTER UNDO");
  270. Undo.SetSnapshotTarget(thisTarget, "Transform Change");
  271. Undo.RegisterSnapshot();
  272. Undo.ClearSnapshotTarget();
  273. listeningForGuiChanges = false;
  274. }
  275. }
  276. }
  277.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement