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

Resolved Accessing localized string in editor OnValidate

Discussion in 'Localization Tools' started by chehob, Apr 20, 2021.

  1. chehob

    chehob

    Joined:
    Oct 25, 2017
    Posts:
    8
    Hello,

    I'm trying to get localized string in OnValidate when the values change in editor but end up with "SendMessage cannot be called during OnValidate" warning.

    Code (CSharp):
    1. public AgentData : ScriptableObject
    2. {
    3.     public LocalizedString Name;
    4. }
    5.  
    6. public class Agent
    7. {
    8.     public string _name;
    9.     public AgentData _presetData;
    10.  
    11.     public void OnValidate()
    12.     {
    13.         if(_presetData != null)
    14.         {
    15.             _presetData.Name.GetLocalizedString().Completed += op => _name = op.Result;
    16.         }
    17.     }
    18. }
    19.  
    20. public AgencyData : ScriptableObject
    21. {
    22.     public List<Agent> _agents;
    23.  
    24.     void OnValidate()
    25.     {
    26.         foreach(Agent agent in _agents)
    27.         {
    28.             _agents.OnValidate();
    29.         }
    30.     }
    31. }
    The idea here is to have customizable agent names for realtime new agents and also some predefined localized names for preset agents. Setting up preset data in editor should be automatically setting the name for this agent, but apparently it is not possible to get localized string in editor?
     
    Last edited: Apr 20, 2021
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    That error is coming from a UnityEvent, not localization.
    We do have editor support. You may need to call directly into StringDatabase to avoid triggering the event.
     
  3. chehob

    chehob

    Joined:
    Oct 25, 2017
    Posts:
    8
    Yes, I see now this warning message was only a distraction. The real problem was "SelectedLocale is null" after inspecting the GetLocalizedString operation.
    Now I understand that Localization is not initialized in editor mode by default and wrote this script for it to work:
    Code (CSharp):
    1. [InitializeOnLoad]
    2. public class Startup
    3. {
    4.     static Startup()
    5.     {      
    6.         LocalizationSettings.InitializationOperation.Completed += op =>
    7.         {
    8.             var locale = LocalizationSettings.AvailableLocales.Locales.FirstOrDefault();
    9.             LocalizationSettings.Instance.SetSelectedLocale(locale);
    10.         };
    11.     }
    12. }
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    Calling InitializationOperation in Edit mode should complete immediately however calling it from OnValidate may cause issues as there are some restrictions on what can be done inside of OnValidate as you saw earlier.
     
    chehob likes this.