TMP_MeshRendererEditor.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // When enabled, allows setting the material by dropping a material onto the MeshRenderer inspector component.
  2. // The drawback is that the MeshRenderer inspector will not have properties for light probes, so if you need light probe support, do not enable this.
  3. //#define ALLOW_MESHRENDERER_MATERIAL_DRAG_N_DROP
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System.Collections;
  7. namespace TMPro.EditorUtilities
  8. {
  9. // Disabled for compatibility reason as lightprobe setup isn't supported due to inability to inherit from MeshRendererEditor class
  10. #if ALLOW_MESHRENDERER_MATERIAL_DRAG_N_DROP
  11. [CanEditMultipleObjects]
  12. [CustomEditor(typeof(MeshRenderer))]
  13. public class TMP_MeshRendererEditor : Editor
  14. {
  15. private SerializedProperty m_Materials;
  16. void OnEnable()
  17. {
  18. m_Materials = serializedObject.FindProperty("m_Materials");
  19. }
  20. public override void OnInspectorGUI()
  21. {
  22. serializedObject.Update();
  23. // Get a reference to the current material.
  24. SerializedProperty material_prop = m_Materials.GetArrayElementAtIndex(0);
  25. Material currentMaterial = material_prop.objectReferenceValue as Material;
  26. EditorGUI.BeginChangeCheck();
  27. base.OnInspectorGUI();
  28. if (EditorGUI.EndChangeCheck())
  29. {
  30. material_prop = m_Materials.GetArrayElementAtIndex(0);
  31. TMP_FontAsset newFontAsset = null;
  32. Material newMaterial = null;
  33. if (material_prop != null)
  34. newMaterial = material_prop.objectReferenceValue as Material;
  35. // Check if the new material is referencing a different font atlas texture.
  36. if (newMaterial != null && currentMaterial.GetInstanceID() != newMaterial.GetInstanceID())
  37. {
  38. // Search for the Font Asset matching the new font atlas texture.
  39. newFontAsset = TMP_EditorUtility.FindMatchingFontAsset(newMaterial);
  40. }
  41. GameObject[] objects = Selection.gameObjects;
  42. for (int i = 0; i < objects.Length; i++)
  43. {
  44. // Assign new font asset
  45. if (newFontAsset != null)
  46. {
  47. TMP_Text textComponent = objects[i].GetComponent<TMP_Text>();
  48. if (textComponent != null)
  49. {
  50. Undo.RecordObject(textComponent, "Font Asset Change");
  51. textComponent.font = newFontAsset;
  52. }
  53. }
  54. TMPro_EventManager.ON_DRAG_AND_DROP_MATERIAL_CHANGED(objects[i], currentMaterial, newMaterial);
  55. }
  56. }
  57. }
  58. }
  59. #endif
  60. }