Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMP_SpriteGlyphPropertyDrawer.cs 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using UnityEngine;
  2. using UnityEngine.TextCore;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  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 spriteCharacterIndex;
  31. int.TryParse(property.displayName.Split(' ')[1], out spriteCharacterIndex);
  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.FindProperty("spriteSheet").objectReferenceValue as Texture;
  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. GlyphRect glyphRect = TMP_PropertyDrawerUtilities.GetGlyphRectFromGlyphSerializedProperty(property);
  49. if (glyphRect.width >= glyphRect.height)
  50. {
  51. spriteSize.y = glyphRect.height * spriteSize.x / glyphRect.width;
  52. spriteTexPosition.y += (spriteSize.x - spriteSize.y) / 2;
  53. }
  54. else
  55. {
  56. spriteSize.x = glyphRect.width * spriteSize.y / glyphRect.height;
  57. spriteTexPosition.x += (spriteSize.y - spriteSize.x) / 2;
  58. }
  59. // Compute the normalized texture coordinates
  60. Rect texCoords = new Rect((float)glyphRect.x / tex.width, (float)glyphRect.y / tex.height, (float)glyphRect.width / tex.width, (float)glyphRect.height / tex.height);
  61. GUI.DrawTextureWithTexCoords(new Rect(spriteTexPosition.x + 5, spriteTexPosition.y + 32f, spriteSize.x, spriteSize.y), tex, texCoords, true);
  62. }
  63. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  64. {
  65. return 130f;
  66. }
  67. }
  68. }