public override void OnInspectorGUI() { serializedObject.Update(); // Get a reference to the current material. SerializedProperty material_prop = m_Materials.GetArrayElementAtIndex(0); Material currentMaterial = material_prop.objectReferenceValue as Material; EditorGUI.BeginChangeCheck(); base.OnInspectorGUI(); if (EditorGUI.EndChangeCheck()) { material_prop = m_Materials.GetArrayElementAtIndex(0); TMP_FontAsset newFontAsset = null; Material newMaterial = null; if (material_prop != null) { newMaterial = material_prop.objectReferenceValue as Material; } // Check if the new material is referencing a different font atlas texture. if (newMaterial != null && currentMaterial.GetInstanceID() != newMaterial.GetInstanceID()) { // Search for the Font Asset matching the new font atlas texture. newFontAsset = TMP_EditorUtility.FindMatchingFontAsset(newMaterial); } GameObject[] objects = Selection.gameObjects; for (int i = 0; i < objects.Length; i++) { // Assign new font asset if (newFontAsset != null) { TMP_Text textComponent = objects[i].GetComponent <TMP_Text>(); if (textComponent != null) { Undo.RecordObject(textComponent, "Font Asset Change"); textComponent.font = newFontAsset; } } TMPro_EventManager.ON_DRAG_AND_DROP_MATERIAL_CHANGED(objects[i], currentMaterial, newMaterial); } } }
void PerformDrop(Material droppedMaterial) { Texture droppedTex = droppedMaterial.GetTexture(ShaderUtilities.ID_MainTex); if (!droppedTex) { return; } Texture currentTex = m_Material.GetTexture(ShaderUtilities.ID_MainTex); TMP_FontAsset requiredFontAsset = null; if (droppedTex != currentTex) { requiredFontAsset = TMP_EditorUtility.FindMatchingFontAsset(droppedMaterial); if (!requiredFontAsset) { return; } } foreach (GameObject o in Selection.gameObjects) { if (requiredFontAsset) { TMP_Text textComponent = o.GetComponent <TMP_Text>(); if (textComponent) { Undo.RecordObject(textComponent, "Font Asset Change"); textComponent.font = requiredFontAsset; } } TMPro_EventManager.ON_DRAG_AND_DROP_MATERIAL_CHANGED(o, m_Material, droppedMaterial); EditorUtility.SetDirty(o); } }