How to make generic options in GameManager that, when changed, will apply to all components of that type?
Hi, I have some options that will be applied to all objects with a certain component in the Scene.
For example, I have several text meshes in the scene and a font selector in GameManager. When that selector changes, all text meshes should change font in the scene view.
Right now I have a GameManagerEditor that will find all components in the scene and set the font (And the font material) but it doesn't work. I have aswell a language selector and it should change the text of all text meshes, but it doesn't work either.
#region Unity
private void OnEnable( )
{
//--
}
public override void OnInspectorGUI( )
{
EditorGUI.BeginChangeCheck( );
DrawDefaultInspector( );
if( !Application.isPlaying && EditorGUI.EndChangeCheck( ) )
{
_ChangeTexts( );
_ChangeTextFonts( );
}
}
#endregion Unity
private void _ChangeTexts( )
{
LocalizableText[] all_texts = Resources.FindObjectsOfTypeAll<LocalizableText>( );
foreach( LocalizableText text in all_texts )
{
text.SetText( text.key );
}
}
private void _ChangeTextFonts( )
{
TaleText[] all_tale_texts = Object.FindObjectsOfType<TaleText>( );
foreach( TaleText tale_text in all_tale_texts )
{
TextMesh tm = tale_text.GetComponent<TextMesh>( );
MeshRenderer mr = tale_text.GetComponent<MeshRenderer>( );
if( tm && mr )
{
if( !tale_text.overrideFontSelection && GameManager.HasInstance )
{
tm.font = GameManager.Instance.font;
mr.sharedMaterial = tm.font.material;
tm.text = "changed";
}
}
}
}
Firstly, what am I doing wrong? Debugging I can see that the code does execute, and it iterates through the text. But something weird happens since, for example, the text of the meshes are empty while they should have text (If I go to the inspector I can see there is texts on them) which make me thik something is not ok.
Secondly, if there is a better way of doing this I would love to know.
Thanks!