/Assets/NGUI/Scripts/Editor/NGUITransformInspector.cs

https://bitbucket.org/Werring/unity-indusim · C# · 147 lines · 96 code · 20 blank · 31 comment · 20 complexity · 119fc01a0b596a728437aefa52114307 MD5 · raw file

  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2012 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using UnityEditor;
  7. [CustomEditor(typeof(Transform))]
  8. public class NGUITransformInspector : Editor
  9. {
  10. /// <summary>
  11. /// Draw the inspector widget.
  12. /// </summary>
  13. public override void OnInspectorGUI ()
  14. {
  15. Transform trans = target as Transform;
  16. EditorGUIUtility.LookLikeControls(15f);
  17. Vector3 pos;
  18. Vector3 rot;
  19. Vector3 scale;
  20. // Position
  21. EditorGUILayout.BeginHorizontal();
  22. {
  23. if (DrawButton("P", "Reset Position", IsResetPositionValid(trans), 20f))
  24. {
  25. NGUIEditorTools.RegisterUndo("Reset Position", trans);
  26. trans.localPosition = Vector3.zero;
  27. }
  28. pos = DrawVector3(trans.localPosition);
  29. }
  30. EditorGUILayout.EndHorizontal();
  31. // Rotation
  32. EditorGUILayout.BeginHorizontal();
  33. {
  34. if (DrawButton("R", "Reset Rotation", IsResetRotationValid(trans), 20f))
  35. {
  36. NGUIEditorTools.RegisterUndo("Reset Rotation", trans);
  37. trans.localEulerAngles = Vector3.zero;
  38. }
  39. rot = DrawVector3(trans.localEulerAngles);
  40. }
  41. EditorGUILayout.EndHorizontal();
  42. // Scale
  43. EditorGUILayout.BeginHorizontal();
  44. {
  45. if (DrawButton("S", "Reset Scale", IsResetScaleValid(trans), 20f))
  46. {
  47. NGUIEditorTools.RegisterUndo("Reset Scale", trans);
  48. trans.localScale = Vector3.one;
  49. }
  50. scale = DrawVector3(trans.localScale);
  51. }
  52. EditorGUILayout.EndHorizontal();
  53. // If something changes, set the transform values
  54. if (GUI.changed)
  55. {
  56. NGUIEditorTools.RegisterUndo("Transform Change", trans);
  57. trans.localPosition = Validate(pos);
  58. trans.localEulerAngles = Validate(rot);
  59. trans.localScale = Validate(scale);
  60. }
  61. }
  62. /// <summary>
  63. /// Helper function that draws a button in an enabled or disabled state.
  64. /// </summary>
  65. static bool DrawButton (string title, string tooltip, bool enabled, float width)
  66. {
  67. if (enabled)
  68. {
  69. // Draw a regular button
  70. return GUILayout.Button(new GUIContent(title, tooltip), GUILayout.Width(width));
  71. }
  72. else
  73. {
  74. // Button should be disabled -- draw it darkened and ignore its return value
  75. Color color = GUI.color;
  76. GUI.color = new Color(1f, 1f, 1f, 0.25f);
  77. GUILayout.Button(new GUIContent(title, tooltip), GUILayout.Width(width));
  78. GUI.color = color;
  79. return false;
  80. }
  81. }
  82. /// <summary>
  83. /// Helper function that draws a field of 3 floats.
  84. /// </summary>
  85. static Vector3 DrawVector3 (Vector3 value)
  86. {
  87. GUILayoutOption opt = GUILayout.MinWidth(30f);
  88. value.x = EditorGUILayout.FloatField("X", value.x, opt);
  89. value.y = EditorGUILayout.FloatField("Y", value.y, opt);
  90. value.z = EditorGUILayout.FloatField("Z", value.z, opt);
  91. return value;
  92. }
  93. /// <summary>
  94. /// Helper function that determines whether its worth it to show the reset position button.
  95. /// </summary>
  96. static bool IsResetPositionValid (Transform targetTransform)
  97. {
  98. Vector3 v = targetTransform.localPosition;
  99. return (v.x != 0f || v.y != 0f || v.z != 0f);
  100. }
  101. /// <summary>
  102. /// Helper function that determines whether its worth it to show the reset rotation button.
  103. /// </summary>
  104. static bool IsResetRotationValid (Transform targetTransform)
  105. {
  106. Vector3 v = targetTransform.localEulerAngles;
  107. return (v.x != 0f || v.y != 0f || v.z != 0f);
  108. }
  109. /// <summary>
  110. /// Helper function that determines whether its worth it to show the reset scale button.
  111. /// </summary>
  112. static bool IsResetScaleValid (Transform targetTransform)
  113. {
  114. Vector3 v = targetTransform.localScale;
  115. return (v.x != 1f || v.y != 1f || v.z != 1f);
  116. }
  117. /// <summary>
  118. /// Helper function that removes not-a-number values from the vector.
  119. /// </summary>
  120. static Vector3 Validate (Vector3 vector)
  121. {
  122. vector.x = float.IsNaN(vector.x) ? 0f : vector.x;
  123. vector.y = float.IsNaN(vector.y) ? 0f : vector.y;
  124. vector.z = float.IsNaN(vector.z) ? 0f : vector.z;
  125. return vector;
  126. }
  127. }