TMP_SubMeshUI_Editor.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. namespace TMPro.EditorUtilities
  5. {
  6. [CustomEditor(typeof(TMP_SubMeshUI)), CanEditMultipleObjects]
  7. public class TMP_SubMeshUI_Editor : Editor
  8. {
  9. private struct m_foldout
  10. { // Track Inspector foldout panel states, globally.
  11. //public static bool textInput = true;
  12. public static bool fontSettings = true;
  13. //public static bool extraSettings = false;
  14. //public static bool shadowSetting = false;
  15. //public static bool materialEditor = true;
  16. }
  17. private SerializedProperty fontAsset_prop;
  18. private SerializedProperty spriteAsset_prop;
  19. private TMP_SubMeshUI m_SubMeshComponent;
  20. private CanvasRenderer m_canvasRenderer;
  21. private Editor m_materialEditor;
  22. private Material m_targetMaterial;
  23. public void OnEnable()
  24. {
  25. fontAsset_prop = serializedObject.FindProperty("m_fontAsset");
  26. spriteAsset_prop = serializedObject.FindProperty("m_spriteAsset");
  27. m_SubMeshComponent = target as TMP_SubMeshUI;
  28. //m_rectTransform = m_SubMeshComponent.rectTransform;
  29. m_canvasRenderer = m_SubMeshComponent.canvasRenderer;
  30. // Create new Material Editor if one does not exists
  31. if (m_canvasRenderer != null && m_canvasRenderer.GetMaterial() != null)
  32. {
  33. m_materialEditor = Editor.CreateEditor(m_canvasRenderer.GetMaterial());
  34. m_targetMaterial = m_canvasRenderer.GetMaterial();
  35. }
  36. }
  37. public void OnDisable()
  38. {
  39. // Destroy material editor if one exists
  40. if (m_materialEditor != null)
  41. {
  42. //Debug.Log("Destroying Inline Material Editor.");
  43. DestroyImmediate(m_materialEditor);
  44. }
  45. }
  46. public override void OnInspectorGUI()
  47. {
  48. GUI.enabled = false;
  49. EditorGUILayout.PropertyField(fontAsset_prop);
  50. EditorGUILayout.PropertyField(spriteAsset_prop);
  51. GUI.enabled = true;
  52. EditorGUILayout.Space();
  53. // If a Custom Material Editor exists, we use it.
  54. if (m_canvasRenderer != null && m_canvasRenderer.GetMaterial() != null)
  55. {
  56. Material mat = m_canvasRenderer.GetMaterial();
  57. //Debug.Log(mat + " " + m_targetMaterial);
  58. if (mat != m_targetMaterial)
  59. {
  60. // Destroy previous Material Instance
  61. //Debug.Log("New Material has been assigned.");
  62. m_targetMaterial = mat;
  63. DestroyImmediate(m_materialEditor);
  64. }
  65. if (m_materialEditor == null)
  66. {
  67. m_materialEditor = Editor.CreateEditor(mat);
  68. }
  69. m_materialEditor.DrawHeader();
  70. m_materialEditor.OnInspectorGUI();
  71. }
  72. }
  73. }
  74. }