TMPro_TextContainerEditor.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. namespace TMPro.EditorUtilities
  5. {
  6. [CustomEditor(typeof(TextContainer)), CanEditMultipleObjects]
  7. public class TMPro_TextContainerEditor : Editor
  8. {
  9. // Serialized Properties
  10. private SerializedProperty anchorPosition_prop;
  11. private SerializedProperty pivot_prop;
  12. private SerializedProperty rectangle_prop;
  13. private SerializedProperty margins_prop;
  14. private TextContainer m_textContainer;
  15. //private Transform m_transform;
  16. //private Vector3[] m_Rect_handlePoints = new Vector3[4];
  17. //private Vector3[] m_Margin_handlePoints = new Vector3[4];
  18. //private Vector2 m_anchorPosition;
  19. //private Vector3 m_mousePreviousPOS;
  20. //private Vector2 m_previousStartPOS;
  21. //private int m_mouseDragFlag = 0;
  22. //private static Transform m_visualHelper;
  23. void OnEnable()
  24. {
  25. // Serialized Properties
  26. anchorPosition_prop = serializedObject.FindProperty("m_anchorPosition");
  27. pivot_prop = serializedObject.FindProperty("m_pivot");
  28. rectangle_prop = serializedObject.FindProperty("m_rect");
  29. margins_prop = serializedObject.FindProperty("m_margins");
  30. m_textContainer = (TextContainer)target;
  31. //m_transform = m_textContainer.transform;
  32. /*
  33. if (m_visualHelper == null)
  34. {
  35. m_visualHelper = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
  36. m_visualHelper.localScale = new Vector3(0.25f, 0.25f, 0.25f);
  37. }
  38. */
  39. }
  40. void OnDisable()
  41. {
  42. /*
  43. if (m_visualHelper != null)
  44. DestroyImmediate (m_visualHelper.gameObject);
  45. */
  46. }
  47. public override void OnInspectorGUI()
  48. {
  49. serializedObject.Update();
  50. EditorGUI.BeginChangeCheck();
  51. EditorGUILayout.PropertyField(anchorPosition_prop);
  52. if (anchorPosition_prop.enumValueIndex == 9)
  53. {
  54. EditorGUI.indentLevel += 1;
  55. EditorGUILayout.PropertyField(pivot_prop, new GUIContent("Pivot Position"));
  56. EditorGUI.indentLevel -= 1;
  57. }
  58. DrawDimensionProperty(rectangle_prop, "Dimensions");
  59. DrawMaginProperty(margins_prop, "Margins");
  60. if (EditorGUI.EndChangeCheck())
  61. {
  62. // Re-compute pivot position when changes are made.
  63. if (anchorPosition_prop.enumValueIndex != 9)
  64. pivot_prop.vector2Value = GetAnchorPosition(anchorPosition_prop.enumValueIndex);
  65. m_textContainer.hasChanged = true;
  66. }
  67. serializedObject.ApplyModifiedProperties();
  68. EditorGUILayout.Space();
  69. }
  70. private void DrawDimensionProperty(SerializedProperty property, string label)
  71. {
  72. float old_LabelWidth = EditorGUIUtility.labelWidth;
  73. float old_FieldWidth = EditorGUIUtility.fieldWidth;
  74. Rect rect = EditorGUILayout.GetControlRect(false, 18);
  75. Rect pos0 = new Rect(rect.x, rect.y + 2, rect.width, 18);
  76. float width = rect.width + 3;
  77. pos0.width = old_LabelWidth;
  78. GUI.Label(pos0, label);
  79. Rect rectangle = property.rectValue;
  80. float width_B = width - old_LabelWidth;
  81. float fieldWidth = width_B / 4;
  82. pos0.width = fieldWidth - 5;
  83. pos0.x = old_LabelWidth + 15;
  84. GUI.Label(pos0, "Width");
  85. pos0.x += fieldWidth;
  86. rectangle.width = EditorGUI.FloatField(pos0, GUIContent.none, rectangle.width);
  87. pos0.x += fieldWidth;
  88. GUI.Label(pos0, "Height");
  89. pos0.x += fieldWidth;
  90. rectangle.height = EditorGUI.FloatField(pos0, GUIContent.none, rectangle.height);
  91. property.rectValue = rectangle;
  92. EditorGUIUtility.labelWidth = old_LabelWidth;
  93. EditorGUIUtility.fieldWidth = old_FieldWidth;
  94. }
  95. private void DrawMaginProperty(SerializedProperty property, string label)
  96. {
  97. float old_LabelWidth = EditorGUIUtility.labelWidth;
  98. float old_FieldWidth = EditorGUIUtility.fieldWidth;
  99. Rect rect = EditorGUILayout.GetControlRect(false, 2 * 18);
  100. Rect pos0 = new Rect(rect.x, rect.y + 2, rect.width, 18);
  101. float width = rect.width + 3;
  102. pos0.width = old_LabelWidth;
  103. GUI.Label(pos0, label);
  104. //Vector4 vec = property.vector4Value;
  105. Vector4 vec = Vector4.zero;
  106. vec.x = property.FindPropertyRelative("x").floatValue;
  107. vec.y = property.FindPropertyRelative("y").floatValue;
  108. vec.z = property.FindPropertyRelative("z").floatValue;
  109. vec.w = property.FindPropertyRelative("w").floatValue;
  110. float widthB = width - old_LabelWidth;
  111. float fieldWidth = widthB / 4;
  112. pos0.width = fieldWidth - 5;
  113. // Labels
  114. pos0.x = old_LabelWidth + 15;
  115. GUI.Label(pos0, "Left");
  116. pos0.x += fieldWidth;
  117. GUI.Label(pos0, "Top");
  118. pos0.x += fieldWidth;
  119. GUI.Label(pos0, "Right");
  120. pos0.x += fieldWidth;
  121. GUI.Label(pos0, "Bottom");
  122. pos0.y += 18;
  123. pos0.x = old_LabelWidth + 15;
  124. vec.x = EditorGUI.FloatField(pos0, GUIContent.none, vec.x);
  125. pos0.x += fieldWidth;
  126. vec.y = EditorGUI.FloatField(pos0, GUIContent.none, vec.y);
  127. pos0.x += fieldWidth;
  128. vec.z = EditorGUI.FloatField(pos0, GUIContent.none, vec.z);
  129. pos0.x += fieldWidth;
  130. vec.w = EditorGUI.FloatField(pos0, GUIContent.none, vec.w);
  131. //property.vector4Value = vec;
  132. property.FindPropertyRelative("x").floatValue = vec.x;
  133. property.FindPropertyRelative("y").floatValue = vec.y;
  134. property.FindPropertyRelative("z").floatValue = vec.z;
  135. property.FindPropertyRelative("w").floatValue = vec.w;
  136. EditorGUIUtility.labelWidth = old_LabelWidth;
  137. EditorGUIUtility.fieldWidth = old_FieldWidth;
  138. }
  139. Vector2 GetAnchorPosition(int index)
  140. {
  141. Vector2 anchorPosition = Vector2.zero;
  142. switch (index)
  143. {
  144. case 0: // TOP LEFT
  145. anchorPosition = new Vector2(0, 1);
  146. break;
  147. case 1: // TOP
  148. anchorPosition = new Vector2(0.5f, 1);
  149. break;
  150. case 2: // TOP RIGHT
  151. anchorPosition = new Vector2(1, 1);
  152. break;
  153. case 3: // LEFT
  154. anchorPosition = new Vector2(0, 0.5f);
  155. break;
  156. case 4: // MIDDLE
  157. anchorPosition = new Vector2(0.5f, 0.5f);
  158. break;
  159. case 5: // RIGHT
  160. anchorPosition = new Vector2(1, 0.5f);
  161. break;
  162. case 6: // BOTTOM LEFT
  163. anchorPosition = new Vector2(0, 0);
  164. break;
  165. case 7: // BOTTOM
  166. anchorPosition = new Vector2(0.5f, 0);
  167. break;
  168. case 8: // BOTTOM RIGHT
  169. anchorPosition = new Vector2(1, 0);
  170. break;
  171. }
  172. return anchorPosition;
  173. }
  174. }
  175. }