TMP_SpriteGlyphPropertyDrawer.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using UnityEngine;
  2. using UnityEngine.TextCore;
  3. using UnityEditor;
  4. using System.Collections;
  5. namespace TMPro.EditorUtilities
  6. {
  7. [CustomPropertyDrawer(typeof(TMP_SpriteGlyph))]
  8. public class TMP_SpriteGlyphPropertyDrawer : PropertyDrawer
  9. {
  10. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  11. {
  12. SerializedProperty prop_GlyphIndex = property.FindPropertyRelative("m_Index");
  13. SerializedProperty prop_GlyphMetrics = property.FindPropertyRelative("m_Metrics");
  14. SerializedProperty prop_GlyphRect = property.FindPropertyRelative("m_GlyphRect");
  15. SerializedProperty prop_Scale = property.FindPropertyRelative("m_Scale");
  16. SerializedProperty prop_AtlasIndex = property.FindPropertyRelative("m_AtlasIndex");
  17. GUIStyle style = new GUIStyle(EditorStyles.label);
  18. style.richText = true;
  19. Rect rect = new Rect(position.x + 70, position.y, position.width, 49);
  20. // Draw GlyphRect
  21. EditorGUI.PropertyField(rect, prop_GlyphRect);
  22. // Draw GlyphMetrics
  23. rect.y += 45;
  24. EditorGUI.PropertyField(rect, prop_GlyphMetrics);
  25. EditorGUIUtility.labelWidth = 40f;
  26. EditorGUI.PropertyField(new Rect(rect.x, rect.y + 65, 75, 18), prop_Scale, new GUIContent("Scale:"));
  27. EditorGUIUtility.labelWidth = 74f;
  28. EditorGUI.PropertyField(new Rect(rect.x + 85, rect.y + 65, 95, 18), prop_AtlasIndex, new GUIContent("Atlas Index:"));
  29. DrawGlyph(position, property);
  30. int.TryParse(property.displayName.Split(' ')[1], out int spriteCharacterIndex);
  31. float labelWidthIndex = GUI.skin.label.CalcSize(new GUIContent("#" + spriteCharacterIndex)).x;
  32. EditorGUI.LabelField(new Rect(position.x, position.y + 5, 64f, 18f), new GUIContent("#" + spriteCharacterIndex), style);
  33. float labelWidthID = GUI.skin.label.CalcSize(new GUIContent("ID: " + prop_GlyphIndex.intValue)).x;
  34. EditorGUI.LabelField(new Rect(position.x + (64 - labelWidthID) / 2, position.y + 110, 64f, 18f), new GUIContent("ID: <color=#FFFF80>" + prop_GlyphIndex.intValue + "</color>"), style);
  35. }
  36. void DrawGlyph(Rect position, SerializedProperty property)
  37. {
  38. // Get a reference to the sprite texture
  39. Texture tex = (property.serializedObject.targetObject as TMP_SpriteAsset).spriteSheet;
  40. // Return if we don't have a texture assigned to the sprite asset.
  41. if (tex == null)
  42. {
  43. Debug.LogWarning("Please assign a valid Sprite Atlas texture to the [" + property.serializedObject.targetObject.name + "] Sprite Asset.", property.serializedObject.targetObject);
  44. return;
  45. }
  46. Vector2 spriteTexPosition = new Vector2(position.x, position.y);
  47. Vector2 spriteSize = new Vector2(65, 65);
  48. SerializedProperty prop_GlyphRect = property.FindPropertyRelative("m_GlyphRect");
  49. int spriteImageX = prop_GlyphRect.FindPropertyRelative("m_X").intValue;
  50. int spriteImageY = prop_GlyphRect.FindPropertyRelative("m_Y").intValue;
  51. int spriteImageWidth = prop_GlyphRect.FindPropertyRelative("m_Width").intValue;
  52. int spriteImageHeight = prop_GlyphRect.FindPropertyRelative("m_Height").intValue;
  53. if (spriteImageWidth >= spriteImageHeight)
  54. {
  55. spriteSize.y = spriteImageHeight * spriteSize.x / spriteImageWidth;
  56. spriteTexPosition.y += (spriteSize.x - spriteSize.y) / 2;
  57. }
  58. else
  59. {
  60. spriteSize.x = spriteImageWidth * spriteSize.y / spriteImageHeight;
  61. spriteTexPosition.x += (spriteSize.y - spriteSize.x) / 2;
  62. }
  63. // Compute the normalized texture coordinates
  64. Rect texCoords = new Rect((float)spriteImageX / tex.width, (float)spriteImageY / tex.height, (float)spriteImageWidth / tex.width, (float)spriteImageHeight / tex.height);
  65. GUI.DrawTextureWithTexCoords(new Rect(spriteTexPosition.x + 5, spriteTexPosition.y + 32f, spriteSize.x, spriteSize.y), tex, texCoords, true);
  66. }
  67. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  68. {
  69. return 130f;
  70. }
  71. }
  72. }