Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Generic property in GameManager that applies to all components in scene

Discussion in 'Scripting' started by AlexTemina, May 9, 2016.

  1. AlexTemina

    AlexTemina

    Joined:
    Feb 19, 2014
    Posts:
    44
    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.

    Code (CSharp):
    1. #region Unity
    2.     private void OnEnable( )
    3.     {
    4.         //--
    5.     }
    6.  
    7.     public override void OnInspectorGUI( )
    8.     {
    9.         EditorGUI.BeginChangeCheck( );
    10.  
    11.         DrawDefaultInspector( );
    12.  
    13.         if( !Application.isPlaying && EditorGUI.EndChangeCheck( ) )
    14.         {
    15.             _ChangeTexts( );
    16.             _ChangeTextFonts( );
    17.         }
    18.     }
    19.  
    20.     #endregion Unity
    21.  
    22.     private void _ChangeTexts( )
    23.     {
    24.         LocalizableText[] all_texts = Resources.FindObjectsOfTypeAll<LocalizableText>( );
    25.         foreach( LocalizableText text in all_texts )
    26.         {
    27.             text.SetText( text.key );
    28.         }
    29.     }
    30.  
    31.     private void _ChangeTextFonts( )
    32.     {
    33.         TaleText[] all_tale_texts = Object.FindObjectsOfType<TaleText>( );
    34.         foreach( TaleText tale_text in all_tale_texts )
    35.         {
    36.             TextMesh tm = tale_text.GetComponent<TextMesh>( );
    37.             MeshRenderer mr = tale_text.GetComponent<MeshRenderer>( );
    38.             if( tm && mr )
    39.             {
    40.                 if( !tale_text.overrideFontSelection && GameManager.HasInstance )
    41.                 {
    42.                     tm.font = GameManager.Instance.font;
    43.                     mr.sharedMaterial = tm.font.material;
    44.                     tm.text = "changed";
    45.                 }
    46.             }
    47.         }
    48.     }
    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!
     
  2. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    Firstly, be aware that Resources.FindObjectsOfTypeAll will scan your prefab assets as well, and Object.FindObjectsOfType will return only active objects.

    I'm working on a project with multi language support using events. I have a static settings class, something like

    Code (csharp):
    1.  
    2. public static class Settings
    3. {
    4.     static public event Action<UILanguage> EventChangeLanguage;
    5.  
    6.     static UILanguage languageType = UILanguage.English;
    7.     static public UILanguage LanguageType
    8.     {
    9.         get
    10.         {
    11.             return languageType;
    12.         }
    13.  
    14.         set
    15.         {
    16.             if (languageType == value)
    17.                 return;
    18.  
    19.             EventChangeLanguage(language);
    20.         }
    21.     }  
    22. }
    23.  
    and then a client behaviour attached to each Text UI Control (text, buttons, etc) listening this event

    Code (csharp):
    1.  
    2. public class UIText
    3. {
    4.     protected virtual void OnEnable()
    5.     {
    6.         Settings.EventChangeLanguage += Settings_EventChangeLanguage;
    7.     }
    8.  
    9.     protected virtual void OnDisable()
    10.     {
    11.         Settings.EventChangeLanguage -= Settings_EventChangeLanguage;
    12.     }
    13.  
    14.     void Settings_EventChangeLanguage(UILanguage language)
    15.     {
    16.         // apply changes
    17.     }
    18. }
    19.  
    They never receive any direct text string, but language tokens (enum), mapped by translators into real text strings.
     
  3. AlexTemina

    AlexTemina

    Joined:
    Feb 19, 2014
    Posts:
    44
    Problem here is that text won't change unless you click it, right?