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.

143 lines
7.2 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. namespace TMPro.EditorUtilities
  5. {
  6. [CustomPropertyDrawer(typeof(TMP_Sprite))]
  7. public class SpriteInfoDrawer : PropertyDrawer
  8. {
  9. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  10. {
  11. //SerializedProperty prop_fileID = property.FindPropertyRelative("fileID");
  12. SerializedProperty prop_id = property.FindPropertyRelative("id");
  13. SerializedProperty prop_name = property.FindPropertyRelative("name");
  14. SerializedProperty prop_hashCode = property.FindPropertyRelative("hashCode");
  15. SerializedProperty prop_unicode = property.FindPropertyRelative("unicode");
  16. SerializedProperty prop_x = property.FindPropertyRelative("x");
  17. SerializedProperty prop_y = property.FindPropertyRelative("y");
  18. SerializedProperty prop_width = property.FindPropertyRelative("width");
  19. SerializedProperty prop_height = property.FindPropertyRelative("height");
  20. SerializedProperty prop_xOffset = property.FindPropertyRelative("xOffset");
  21. SerializedProperty prop_yOffset = property.FindPropertyRelative("yOffset");
  22. SerializedProperty prop_xAdvance = property.FindPropertyRelative("xAdvance");
  23. SerializedProperty prop_scale = property.FindPropertyRelative("scale");
  24. SerializedProperty prop_sprite = property.FindPropertyRelative("sprite");
  25. // Get a reference to the sprite texture
  26. Texture tex = (property.serializedObject.targetObject as TMP_SpriteAsset).spriteSheet;
  27. // Return if we don't have a texture assigned to the sprite asset.
  28. if (tex == null)
  29. {
  30. Debug.LogWarning("Please assign a valid Sprite Atlas texture to the [" + property.serializedObject.targetObject.name + "] Sprite Asset.", property.serializedObject.targetObject);
  31. return;
  32. }
  33. Vector2 spriteTexPosition = new Vector2(position.x, position.y);
  34. Vector2 spriteSize = new Vector2(65, 65);
  35. if (prop_width.floatValue >= prop_height.floatValue)
  36. {
  37. spriteSize.y = prop_height.floatValue * spriteSize.x / prop_width.floatValue;
  38. spriteTexPosition.y += (spriteSize.x - spriteSize.y) / 2;
  39. }
  40. else
  41. {
  42. spriteSize.x = prop_width.floatValue * spriteSize.y / prop_height.floatValue;
  43. spriteTexPosition.x += (spriteSize.y - spriteSize.x) / 2;
  44. }
  45. // Compute the normalized texture coordinates
  46. Rect texCoords = new Rect(prop_x.floatValue / tex.width, prop_y.floatValue / tex.height, prop_width.floatValue / tex.width, prop_height.floatValue / tex.height);
  47. GUI.DrawTextureWithTexCoords(new Rect(spriteTexPosition.x + 5, spriteTexPosition.y + 2.5f, spriteSize.x, spriteSize.y), tex, texCoords, true);
  48. // We get Rect since a valid position may not be provided by the caller.
  49. Rect rect = new Rect(position.x, position.y, position.width, 49);
  50. rect.x += 70;
  51. bool isEnabled = GUI.enabled;
  52. GUI.enabled = true;
  53. EditorGUIUtility.labelWidth = 30f;
  54. EditorGUI.LabelField(new Rect(rect.x + 5f, rect.y, 65f, 18), new GUIContent("ID:"), new GUIContent(prop_id.intValue.ToString()));
  55. GUI.enabled = isEnabled;
  56. EditorGUI.BeginChangeCheck();
  57. EditorGUIUtility.labelWidth = 55f;
  58. GUI.SetNextControlName("Unicode Input");
  59. string unicode = EditorGUI.TextField(new Rect(rect.x + 75f, rect.y, 105, 18), "Unicode:", prop_unicode.intValue.ToString("X"));
  60. if (GUI.GetNameOfFocusedControl() == "Unicode Input")
  61. {
  62. //Filter out unwanted characters.
  63. char chr = Event.current.character;
  64. if ((chr < '0' || chr > '9') && (chr < 'a' || chr > 'f') && (chr < 'A' || chr > 'F'))
  65. {
  66. Event.current.character = '\0';
  67. }
  68. if (EditorGUI.EndChangeCheck())
  69. {
  70. prop_unicode.intValue = TMP_TextUtilities.StringToInt(unicode);
  71. property.serializedObject.ApplyModifiedProperties();
  72. TMP_SpriteAsset spriteAsset = property.serializedObject.targetObject as TMP_SpriteAsset;
  73. spriteAsset.UpdateLookupTables();
  74. }
  75. }
  76. EditorGUIUtility.labelWidth = 45f;
  77. EditorGUI.BeginChangeCheck();
  78. EditorGUI.PropertyField(new Rect(rect.x + 185f, rect.y, rect.width - 260, 18), prop_name, new GUIContent("Name: " + prop_name.stringValue));
  79. if (EditorGUI.EndChangeCheck())
  80. {
  81. Sprite sprite = prop_sprite.objectReferenceValue as Sprite;
  82. if (sprite != null)
  83. sprite.name = prop_name.stringValue;
  84. // Recompute hashCode for new name
  85. prop_hashCode.intValue = TMP_TextUtilities.GetSimpleHashCode(prop_name.stringValue);
  86. // Check to make sure for duplicates
  87. property.serializedObject.ApplyModifiedProperties();
  88. // Dictionary needs to be updated since HashCode has changed.
  89. //TMP_StyleSheet.Instance.LoadStyleDictionary();
  90. }
  91. EditorGUIUtility.labelWidth = 30f;
  92. EditorGUIUtility.fieldWidth = 10f;
  93. //GUI.enabled = false;
  94. float width = (rect.width - 75f) / 4;
  95. EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 0, rect.y + 22, width - 5f, 18), prop_x, new GUIContent("X:"));
  96. EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 1, rect.y + 22, width - 5f, 18), prop_y, new GUIContent("Y:"));
  97. EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 2, rect.y + 22, width - 5f, 18), prop_width, new GUIContent("W:"));
  98. EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 3, rect.y + 22, width - 5f, 18), prop_height, new GUIContent("H:"));
  99. //GUI.enabled = true;
  100. EditorGUI.BeginChangeCheck();
  101. EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 0, rect.y + 44, width - 5f, 18), prop_xOffset, new GUIContent("OX:"));
  102. EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 1, rect.y + 44, width - 5f, 18), prop_yOffset, new GUIContent("OY:"));
  103. EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 2, rect.y + 44, width - 5f, 18), prop_xAdvance, new GUIContent("Adv."));
  104. EditorGUI.PropertyField(new Rect(rect.x + 5f + width * 3, rect.y + 44, width - 5f, 18), prop_scale, new GUIContent("SF."));
  105. if (EditorGUI.EndChangeCheck())
  106. {
  107. //Sprite sprite = prop_sprite.objectReferenceValue as Sprite;
  108. //sprite = Sprite.Create(sprite.texture, sprite.rect, new Vector2(0.1f, 0.8f));
  109. //prop_sprite.objectReferenceValue = sprite;
  110. //Debug.Log(sprite.bounds);
  111. }
  112. }
  113. //public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  114. //{
  115. // return 40f;
  116. //}
  117. }
  118. }