Howdy folks,
I’m working on a game that uses heaps of asset bundles and addressables. Some of them are built with other versions of Unity/TextMeshPro, which as you may know causes a bug where the material of the text appears ‘broken’ in the editor (Pink squares) but looks fine in build.
In my game we resolve this by running an editor script that refreshes the material on all TextMeshPros every second.
This is basically what the refresh logic looks like:
private static void RefreshTextMaterialsInProject()
{
TextMeshProUGUI[] texts = Resources.FindObjectsOfTypeAll<TextMeshProUGUI>();
foreach (TextMeshProUGUI text in texts)
{
ReplaceShaderForEditor(text.material);
ReplaceShaderForEditor(text.materialForRendering);
}
}
private static void ReplaceShaderForEditor(Material material)
{
if (material == null) return;
var shaderName = material.shader.name;
var shader = Shader.Find(shaderName);
if (shader != null) material.shader = shader;
}
This script works, but is inefficient. This is mainly caused by calling Resources.FindObjectsOfTypeAll<TextMeshProUGUI>()
every second.
I think it would be a huge improvement if I were to run this by calling GetComponentsInChildren<TextMeshProUGUI>()
on all alive root GameObjects, but I’m not sure how to get them.
Does anyone have any advice on this? Would appreciate any help I could get. Thank you.