TMP_MeshRendererEditor.cs
2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// When enabled, allows setting the material by dropping a material onto the MeshRenderer inspector component.
// 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.
//#define ALLOW_MESHRENDERER_MATERIAL_DRAG_N_DROP
using UnityEngine;
using UnityEditor;
using System.Collections;
namespace TMPro.EditorUtilities
{
// Disabled for compatibility reason as lightprobe setup isn't supported due to inability to inherit from MeshRendererEditor class
#if ALLOW_MESHRENDERER_MATERIAL_DRAG_N_DROP
[CanEditMultipleObjects]
[CustomEditor(typeof(MeshRenderer))]
public class TMP_MeshRendererEditor : Editor
{
private SerializedProperty m_Materials;
void OnEnable()
{
m_Materials = serializedObject.FindProperty("m_Materials");
}
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);
}
}
}
}
#endif
}